john c. j.
john c. j.

Reputation: 1175

"For" for files with spaces

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).

Upvotes: 2

Views: 848

Answers (2)

jwdonahue
jwdonahue

Reputation: 6659

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

Upvotes: 3

karol
karol

Reputation: 430

For me works:

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

and returns:

foo bar.jpg

Upvotes: 1

Related Questions