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"
up vote 9 down vote accepted

How about this?

for /f "tokens=* delims=" %%a in ('dir "c:\test\foo bar.txt" /s /b') do (
echo %%a
)
  • good idea to move the recursion in dir! Thank you. – Alessandro Jacopson Mar 14 '11 at 12:29

Without the recursive switch, you can tell FOR not to print the quotes:

for %%f in ("foo bar.txt") do @if exist %%f echo %~dpnxf

You might be able to do nested FOR statements. The outer would walk the directory tree and the inner would be the one above.

  • Hello, thank you for your answer. I edited the question, the tree can be deeper than just only one level and so I need the recursive for. – Alessandro Jacopson Mar 11 '11 at 15:54
  • 2
    @uvts_cvs: FOR /R %%d in (.) DO for %%f ... is recursive, but there would be some things to work out to make it work. You may have to use delayed expansion, for example. SETLOCAL ENABLEDELAYEDEXPANSION and !varname!, etc. – Dennis Williamson Mar 11 '11 at 16:06
  • %~dpnf will omit the file extension. – dolmen Mar 27 '11 at 10:17
  • @dolmen: Fixed the typo. – Dennis Williamson Mar 27 '11 at 11:35

You might want to look at the forfiles command.

forfiles /S /M "* *" /C "cmd /C echo @PATH"

'/S' -- Recursion. Search all sub directories

'/M "* *"' -- File mask. Only search for files with a space in their name.

'/C "cmd /C echo @PATH"' -- Command. Issue this command on all files found.

Example output:

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

The flag f is the one you need to get an absolute filename witout quotes (so you can put quotes around if you need). But as it conflicts with the variable name you choose, you have to use another one:

for /r test %%i in ("foo bar.txt") do echo %%~fi
  • That's completely incorrect. There's no variable name conflict and quotes are printed by the command you show. – Dennis Williamson Mar 27 '11 at 11:41

Thanks, Dennis! I too was working on an almost identical problem. I wanted a bat file that I could pass in an input parm as a search string for filenames - recursively through directories. Just in case this is useful to someone else here it is.

::+++++++++
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /R %%d in (.) do (
   for /f "usebackq" %%i in (`dir /b %%d ^| findstr /i %1`) do (
      echo Processing %%i
   )
)
endlocal

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.