2

The question seems to be pretty easy, but I haven't found solution myself.

I have some folder, with 1 jpg-file inside it (foo bar.jpg) and this bat-file:

for /f %%f in ('dir /b /a:-d "*.jpg"') do echo "%%f" pause 

For some reason instead of something like this:

C:\Test>echo foo bar.jpg foo bar.jpg 

I see this:

C:\Test>echo "foo" "foo" 

Despite I already put %%f inside quotes.

I.e. command prompt doesn't understand the space in file name.

How to fix it?

(In my real code I will use copy instead of echo).

2 Answers 2

3

See help for.

for /f splits the results into tokens, based on the default or supplied delims. You need something like:

for /f "delims=" .... 

Should capture the entire output. Another option is:

for /f "tokens=*" %%G ... do echo %%G 
Sign up to request clarification or add additional context in comments.

2 Comments

For the second approach, why use two variables? How about for /f "tokens=*" %%f in ('dir /b /a:-d "*.jpg"') do (echo %%f)
@lit, never actually tried that until just now actually. Thanks for the tip!
1

For me works:

for /f "delims=" %%f in ('dir /b /A:-D "*.jpg"') do @echo %%f 

and returns:

foo bar.jpg 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.