63

How do I modify this:

for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"

to work when the path contains spaces?

For example, if this is run from

c:\my folder with spaces

it will echo:

c:\my

Thanks

Ross Ridge
  • 37,034
  • 7
  • 72
  • 110
Andrew Bullock
  • 35,549
  • 33
  • 151
  • 228
  • Try not to use parameter extensions letters for variables, if you later need to add them the code will be hard to read and prone to errors. They are f d p n x s a t z. The easiest and safest way is to use upper-case letters (i.e. %%a and %%A are different variables). – Ricardo Zorio Jul 02 '17 at 19:48

4 Answers4

101

You need to use:

for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"

This overrides the default delimiters which are TAB and SPACE

Jan Zyka
  • 16,741
  • 16
  • 69
  • 113
35

I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause

FOR /F %%A IN ('type "c:\A Path With Spaces\A File.txt"') DO (
    ECHO %%A
)

This article gave me the idea to use "type" in the IN clause.

Jason
  • 8,250
  • 9
  • 56
  • 67
7

If you don't want to deal with "quotes" you can use the "s" switch in %~dpnx[]... this will output the short filenames that are easy to work with.

from the Command line...

for /f "delims=" %f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %~sdpnxf

inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]

for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf
metadings
  • 3,738
  • 2
  • 26
  • 37
0

The problem is there's a wildcard in the string that gets interpreted as a filename. You also need double quotes for a string with spaces. I'm not sure if there's a way to escape the wildcard.

for %a IN ("dir /b /s build\release\.dll") do echo %a
"dir /b /s build\release\.dll"
js2010
  • 17,785
  • 4
  • 45
  • 50