Take the 2-minute tour ×

I want to have a .bat file get a list of all of the *.XML files in a Visual Studio project What I tried so far is to first use an old version of GREP:

SET TempName=%temp%\TEMP%RANDOM%.bat
c:\bin\grep.exe -i \.xml\^" MyProject.vcxproj  >> %TempName%

but then I need to get the filenames from that temp file

I've tried things like

FOR /F "tokens=2 delims="" %%F IN (%TempName%) DO ECHO %%F
FOR /F "tokens=2 delims=^"" %%F IN (%TempName%) DO ECHO %%F
FOR /F 'tokens=2 delims="' %%F IN (%TempName%) DO ECHO %%F

but none of them work. It seems like the delimiter character " is being treated as the closing quote of the options list, which is why I tried to escape it with ^ on the 2nd try.

share|improve this question
What about backslash \"? –  week Dec 3 '12 at 21:03
Backslash is the directory delimiter in Windows. Try to use a forward slash to escape the quote symbol. –  Ian Atkin Dec 3 '12 at 21:14
Not generally e.g. findstr \. –  week Dec 3 '12 at 21:59

2 Answers 2

Normally FOR options are enclosed within quotes. If you want to use a quote as part of an option, then the enclosing quotes must be ditched. That means all characters that the CMD interpreter uses as token delimiters must be escaped, including space and equal sign. Also the quote needs to be escaped.

for /f tokens^=2^ delims^=^" %%F in ...

This same technique is used to solve another common problem - how to use FOR /F and preserve the entire line, regardless what character starts a given line. The problem is the EOL option (semicolon by default). To guarantee preservation of the entire line, the EOL must be disabled. Here again the enclosing quotes are dropped and token delimiters must be escaped.

for /f delims^=^ eol^= %%F in ....

DELIMS is set to nothing because a space is never included in the DELIMS option unless it is the last option specified. The next option string character after EOL= is taken as the EOL character. But in this case there does not exist a next character because the space is not escaped, nor is it quoted. So the above syntax disables both DELIMS and EOL.

share|improve this answer

Quotes mark the boundaries of literal strings in which metacharacters are not parsed. This includes escape characters, typically the caret, so the second occurence of the double quotes cannot be escaped. Generally, the symbol that would mark the end of a literal string will always end the literal string.

If you want to prevent parsing of the "-character, you will have to escape it with an unquoted caret. Unfortunately, for's options string often has quite a lot of characters that need escaping and the legibility clearly suffers. Without using temporary files, the command would expand to:

for /F usebackq^ tokens^=2^ delims^=^" %i in (`findstr /I /R \.cpp\^" MyProj.vcxproj`) do @echo %i

Note that this will not produce correct results if the desired token is not the second. Further nesting of a for loop and a findstr command could easily correct that, but the syntax would be extremely dirty and may be beyond the scope of this question.

share|improve this answer

Your Answer


