67

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

1
  • 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). Commented Jul 2, 2017 at 19:48

4 Answers 4

113

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

Sign up to request clarification or add additional context in comments.

Comments

37

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.

1 Comment

Quotes are the only option when using /D instead of /F (only /F takes delims).
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

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.