tags:

views:

53

answers:

4

Hello,

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.

A: 

try test?.log

Vizay Soni
Won't work - you can try it in a shell: `echo > test.log; echo > test1.log; dir test?.log` will find 2 files.
schnaader
+1  A: 

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.

schnaader
thanks.. I could make it work this way, also you confirmed there is no regex kind of stuff in batch script.
bsreekanth
+1  A: 
stakx
thanks.. +1 for the idea.
bsreekanth
What about `for /l`?
Joey
*@Johannes Rössel:* That, kind Sir, would actually be waaaay smarter than what I suggested. ;-) (I've learnt something new today...)
stakx
A: 

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.

sakra
I'll try this out.. thanks..
bsreekanth