I have the following command in a CMD.EXE batch file executing in a Windows 7 environment:
FOR %%f IN ("C:\TEMP\FILE.TXT" "C:\TEMP\FILE2.TXT") DO (
ECHO %%f
)
- C:\TEMP\FILE.TXT does not exist
- C:\TEMP\FILE2.TXT does exist
This produces the following output:
"C:\TEMP\FILE.TXT"
"C:\TEMP\FILE2.TXT"
However, I have found that if I change the FOR
command to include the presence of a wild card such as ? or * in the file names, I get the results I would expect, which is only the echo of C:\TEMP\FILE2.TXT
IE:
FOR %%f IN ("C:\TEMP\FILE.TXT?" "C:\TEMP\FILE2.TXT?") DO (
ECHO %%f
)
Produces:
C:\TEMP\file2.txt
The documentation for the FOR
command states that:
FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file. command-parameters
It seems that if no wild card is present in the (set)
specifier, the FOR
command treats the file paths as string literals and just iterates over them. Am I misunderstanding the documentation? It seems to me that the presence (or lack of) a wild card in the file (set)
specifier should not impact behavior.