Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file: file mask

Tags:

batch-file

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.

like image 654
bsr Avatar asked Jul 24 '10 16:07

bsr


1 Answers

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.

like image 141
stakx - no longer contributing Avatar answered Sep 22 '22 09:09

stakx - no longer contributing