Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#301408 - 09/08/2007 19:55 FOR command for doing loops in DOS?
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
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.
_________________________
Tony Fabris

Top
#301409 - 09/08/2007 20:12 Re: FOR command for doing loops in DOS? [Re: tfabris]
AndrewT
old hand

Registered: 16/02/2002
Posts: 867
Loc: Oxford, UK
Quote:
%variable Specifies a single letter replaceable parameter


Change %variable to a single character and it'll work.

Top
#301410 - 09/08/2007 20:16 Re: FOR command for doing loops in DOS? [Re: AndrewT]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
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.
_________________________
Tony Fabris

Top
#301411 - 09/08/2007 20:20 Re: FOR command for doing loops in DOS? [Re: tfabris]
AndrewT
old hand

Registered: 16/02/2002
Posts: 867
Loc: Oxford, UK
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.

Top
#301412 - 09/08/2007 20:37 Re: FOR command for doing loops in DOS? [Re: AndrewT]
tman
carpal tunnel

Registered: 24/12/2001
Posts: 5528
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.

Top
#301413 - 09/08/2007 20:40 Re: FOR command for doing loops in DOS? [Re: tman]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
okay. I still don't understand why it has to be one character. And why %i didn't work when %1 did work.
_________________________
Tony Fabris

Top
#301414 - 09/08/2007 21:01 Re: FOR command for doing loops in DOS? [Re: tfabris]
tman
carpal tunnel

Registered: 24/12/2001
Posts: 5528
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>


Top
#301415 - 09/08/2007 21:22 Re: FOR command for doing loops in DOS? [Re: tfabris]
AndrewT
old hand

Registered: 16/02/2002
Posts: 867
Loc: Oxford, UK
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.

Top
#301416 - 10/08/2007 15:44 Re: FOR command for doing loops in DOS? [Re: tfabris]
canuckInOR
carpal tunnel

Registered: 13/02/2002
Posts: 3212
Loc: Portland, OR
Quote:
What am I doing wrong?

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

Top