Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


And I'm typing the following command at the prompt:

for /F %p in (temp.txt) do echo Testing %p

Nothing comes back. If I remove the /F parameter, the output is Testing temp.txt.

As far as I can determine, I'm using the command exactly as it is documented by Microsoft. I've checked my registry to make sure Command Extensions are on, and even started a new shell with cmd /e:on to be doubly sure.

What am I doing wrong?

Yes, I was doing something wrong.

The file temp.txt wasn't created from scratch, I just edited it to put in my test content. Unfortunately when I created the file the first time, I saved it with a UTF-8 marker at the front. Recreating the file as plain text solved the problem.

share|improve this question
hate to ask the stupid question, but are you sure you're in the same directory as temp.txt in the command line? Can't think of any reason it wouldn't work outside of that. – Agent_9191 Dec 22 '09 at 20:03
Yes, I am in the directory. I also tested with a full path to the file, but I'm trying to eliminate as many irrelevant details as possible. – Mark Ransom Dec 22 '09 at 21:20
use echo ... > or copy con to quickly create a file for testing, then you'd have seen it work, then you could use xxd or od and examine the differences. – barlop Sep 25 '11 at 18:32
up vote 4 down vote accepted

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.

share|improve this answer
I figured out the problem before I saw this answer, but since it includes the actual solution (non-printable characters in the front) I'm selecting this as the accepted answer. – Mark Ransom Dec 22 '09 at 22:19

Instead of testing with echo, which simply takes the names in the set as-is (even if the file doesn't exist), try something like type:

for %p in (temp.txt) do type %p

That will show you what the shell "sees". (Note that /F with a nonexistent file will complain about not finding the file, but maybe you have more than one temp.txt around?)

share|improve this answer
Yes, that command shows me the contents of temp.txt. Thanks for the suggestion, but I'm unclear what I was supposed to learn by trying it. – Mark Ransom Dec 22 '09 at 21:27
Just establishing a baseline, that the file is there, readable, and the right one. Next I was going to ask how you created the file -- I used good old copy con -- but I see you've figured it out. The BOM strikes again! I will have to kick it up a notch in the psychic debugging list. – Ken Dec 22 '09 at 22:30

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


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
share|improve this answer

I've spend hours on this. You need to replace temp.txt with 'type temp.txt':

for /F %p in ('type temp.txt') do echo Testing %p
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .