FOR command for doing loops in DOS?

Posted by: tfabris

FOR command for doing loops in DOS? - 09/08/2007 19:55

If I type:

Quote:
FOR /?

at the command prompt, it says (among other things):

Quote:
FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)

Which is exactly what I want to do. But if I create a batch file that looks like this:

Quote:
@echo off
FOR /L %variable IN (1,1,5) DO echo hi
pause

I get "variable was unexpected at this time", instead of printing the word "hi" five times, which is what I expect it to do.

I have tried changing the number of percent signs before the word "variable", changing "variable" to "i" or some other value, and a bunch of other tweaks, but I can't figure out why this thing isn't working as the DOS help says it should.

What am I doing wrong?

Google isn't helping, strangely.

PS: Same results on Vista and XP.
Posted by: AndrewT

Re: FOR command for doing loops in DOS? - 09/08/2007 20:12

Quote:
%variable Specifies a single letter replaceable parameter


Change %variable to a single character and it'll work.
Posted by: tfabris

Re: FOR command for doing loops in DOS? - 09/08/2007 20:16

I tried changing it to %i and it did not work.

I ended up changing it to %%1 and that worked. I have no idea why.
Posted by: AndrewT

Re: FOR command for doing loops in DOS? - 09/08/2007 20:20

Ah, good point. I merely tested it immediately at the CMD prompt. It's documented within XP's "HELP for" as:

To use the FOR command in a batch program, specify %%variable instead of %variable.
Posted by: tman

Re: FOR command for doing loops in DOS? - 09/08/2007 20:37

Quote:
To use the FOR command in a batch program, specify %%variable instead of %variable.

Inside a batch file, %variable gets replaced by the contents of the environment variable "variable". However, %%variable gets replaced by the text "%variable" itself.
Posted by: tfabris

Re: FOR command for doing loops in DOS? - 09/08/2007 20:40

okay. I still don't understand why it has to be one character. And why %i didn't work when %1 did work.
Posted by: tman

Re: FOR command for doing loops in DOS? - 09/08/2007 21:01

Quote:
okay. I still don't understand why it has to be one character. And why %i didn't work when %1 did work.

The %% is important. %%i and %%1 should both work. %i or %1 won't in a batch file.

As for single character variable names:

Code:

C:\>help for
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
<CHOP>

Posted by: AndrewT

Re: FOR command for doing loops in DOS? - 09/08/2007 21:22

Quote:
And why %i didn't work when %1 did work.

You probably have command extensions turned off somehow. Try opening a new CMD window using "cmd /e:on" and see if your batch file works with %i. The CMD options are sparsely documented here.
Posted by: canuckInOR

Re: FOR command for doing loops in DOS? - 10/08/2007 15:44

Quote:
What am I doing wrong?

"You still haven't installed cygwin" seems the obvious answer...