Batch file for loops with spaces in dir name

How can I change 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 done from

 c:\my folder with spaces 

he will echo:

 c:\my 

thank

+45
dos batch-file
Apr 05 2018-11-11T00:
source share
3 answers

You need to use:

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

This overrides the default delimiters that are TAB and SPACE.

+66
Apr 05 2018-11-11T00:
source share
β€” -

I got around this by adding a β€œtype” and adding 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 ) 

In this article, I gave me the idea of ​​using a "type" in an IN clause.

+26
Mar 27 '12 at 19:29
source share

If you do not want to deal with quotes, you can use the switch "s" in% ~ dpnx [] ... this will result in short file names 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 the .CMD / .BAT file you need to "run away" from [%], for example, double [%%]

 for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf 
+5
Apr 6 2018-11-11T00: 00Z
source share



All Articles