In Windows, a batch file with a recursive for loop and a file name including blanks

Posted by uvts_cvs on Super User See other posts from Super User or by uvts_cvs
Published on 2011-03-11T14:33:28Z Indexed on 2011/03/11 16:12 UTC
Read the original article Hit count: 287

Filed under:
|
|

Hello,
I have a folder tree, like this (it's only an example, it will be deeper in my real case):

C:\test
|   
+---folder1
|       foo bar.txt
|       foobar.txt
|       
+---folder2
|       foo bar.txt
|       foobar.txt
|       
\---folder3
        foo bar.txt
        foobar.txt

My files have one or more spaces in the name and I need to perform a command on them, so I am interested in foo bar.txt but not in foobar.txt.

I tried (inside a batch file):

for /r test %%f in (foo bar.txt) do if exist %%f echo %%f

where the command is the simple echo.
It does not work because the space is skipped and I get no output.

This works but it is not what I need:

for /r test %%f in (foobar.txt) do if exist %%f echo %%f

It prints:

C:\test\folder1\foobar.txt
C:\test\folder2\foobar.txt
C:\test\folder3\foobar.txt

I tried using the quotation mark (") but it does not work:

for /r test %%f in ("foo bar.txt") do if exist %%f echo %%f

It does not work because the quotation mark is still included in the output:

C:\test\folder1\"foo bar.txt"
C:\test\folder2\"foo bar.txt"
C:\test\folder3\"foo bar.txt"

© Super User or respective owner

Related posts about Windows

Related posts about batch