Reputation: 58682
I couldn't find details about how to use file mask in a batch file. My requirement,
forfiles -p "C:\what\ever" -s -m *.log -c "cmd /c somecommmand"
instead of selecting all the log files (*.log), how to select all the log files which has an integer suffix at the end. Eg, Among the following files,
test.log, test1.log, test2.log, test3.log..
I need a file mask to select all except test.log
I tried test*.log
, but that slects test.log as well. It is preferrable to not include the file name part (test). something like, *<0-9d>.log
.
thanks.
Upvotes: 8
Views: 7873
Reputation: 65831
To match file names against a regular expression in a batch file you can use the following for loop as a template:
for /F "delims=" %%I in ('"dir /B /S | findstr /E "\\test[0-9][0-9]*\.log""') do (
echo %%I
)
This simply prints the full paths of all files in the current directory and its sub directories, whose name matches test1.log, test2.log, test3.log, ...
The dir
command produces a listing of the directory tree from the current directory. Each line contains a full path. The listing is piped through the findstr
command, which matches the full paths against the given regular expression. On each iteration the for variable %%I
contains the full path of a file that has matched the regular expression.
Upvotes: 2
Reputation: 84765
You could try the following, which I personally think is terrible, but it may just be what you need:
FOR %i IN (1 2 3 4 5 6 7 8 9 10 11 12 13 ...) DO IF EXIST test%i.log (somecommand)
If the suffix is really any integer number in a potentially very large range, this won't work — unless you nest several FOR
loops, where each takes care of only one digit, and you change the IF EXIST test%i.log
to something like IF EXIST test%a%b%c%d.log
(for four digits).
Also, there's no need to execute somecommand
in a separate shell (CMD /C
), unless of course that is in fact what you need to do.
Upvotes: 2
Reputation: 49719
As test?.log
and even test??.log
will find test.log
, too, the only thing to get what you want would be some type of workaround, for example:
if exist test.log ren test.log test.tmp
REM Do your stuff here, you can now use test*.log
if exist text.tmp ren test.tmp test.log
I don't know if this unexpected behaviour of ? (not meaning exactly one character, but at most one character) is something Windows specific or was like this since DOS, but it can be very annoying.
Upvotes: 1