Home » Questions » Computers [ Ask a new question ]

Windows command FOR /F isn't working?

Windows command FOR /F isn't working?

I'm trying to use the FOR command in Windows XP's command line. I have a file temp.txt with 3 lines:

Asked by: Guest | Views: 212
Total answers/comments: 2
Guest [Entry]

"I'm guessing that this is the desired result:

H:\>for /F %p in (temp.txt) do echo Testing %p

H:\>echo Testing temp1
Testing temp1

H:\>echo Testing temp2
Testing temp2

H:\>echo Testing temp3
Testing temp3

If so, have you been testing other batch files, or have you redirected input/output? If you're doing this as part of a larger project, might you have changed delimiters or the skip parameter to the FOR command? Is it possible that there are non-printable characters in the front of your temp.txt file? I've gotten this to work on a couple of different computers, including a 2k3 Server Std. box...I can't help but think there's either trouble with your input file or defaults being set without your knowledge."
Guest [Entry]

"The proper approach is this:

for %%a in (temp1 temp2 temp3) do (
echo Testing %%a
for /f %%b in (%%a) do echo %%b
)

Or you could do this:

for /l %%n in (1,1,3) do (
for %%a in (temp%%n) do (
echo Testing %%a
for /f %%b in (%%a) do echo %%b
)
)

Another variantion is:

for %%a in (*.txt) do (
echo Testing %%a
for /f %%b in (%%a) do echo %%b
)

Etc...

If there are spaces in the filenames then you'll have to use 'usebackq' with 'FOR/F' and wrap the filenames in double-quotes like this:

...
for /f ""usebackq"" %%b in (""%%a"") do echo %%b
..."