I want an example of a batch file that uses a regular expression to find files with a digit in the name or a certain range of numbers.
Is there a way to do this? A simple example?
I want an example of a batch file that uses a regular expression to find files with a digit in the name or a certain range of numbers.
Is there a way to do this? A simple example?
Some of this credit goes to Y.A.P.'s answer.
The following code will get you every file in the directory with at least one digit in the file name:
@Echo Off
CD C:\Folder\To\Process
Dir /B>Dir.temp
FindStr /R "[0-9]" "Dir.temp">FindStr.temp
Del Dir.temp
For /F "tokens=*" %%a In (FindStr.temp) Do Call :WorkIt "%%a"
Del FindStr.temp
Exit /B
:WorkIt
:: Insert code here. Use %1 to get the file name with quotes. For example:
Echo Processing %1...
Exit /B
The FindStr
line contains the regex expression. The Command Line version of regex is limited. What exact range are you after and what format are the file names in?
If, for example, you knew all files had 3 digit numbers in them, you could limit it to all items from 000 to 299 with the expression [0-2][0-9][0-9]
.
Dir.temp
by piping the output of dir /B
to findstr
. Like this dir /B | findstr /R "[0-9]" > FindStr.temp
. Otherwise, a very good post, helped me a lot. Cheers! –
Hookah I'm assuming your on about Command Prompt / MS-Dos batch files here?
If you are then sadly the answer is NO, the ONLY wildcards that plain old batch files support are:
* = match all
and ? = Match 1
so:
mytune*.mp?
would match:
mytune01.mp3, mytune01.mpg, mytune-the-best.mpe
There is however an alternative...
If your using a modern windows version, then chances are you'll have power-shell installed, click on
start->run then type in power-shell and press return, if you get what looks like a command prompt window open then you have it installed, If not then look on the MS site for a download.
Once you have this, if you want to dive straight in, you can find all you need on Reg-ex is PS here:
http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-13-text-and-regular-expressions.aspx
I would however, recommend spending an hour or so learning the basics first.
There is in findstr. Check out THIS
findstr
. –
Anana © 2022 - 2024 — McMap. All rights reserved.