For Loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

For Loop

#1 Post by MLGsuperGame414 » 15 Jul 2013 18:36

I was wondering how to use the for loop command, I tried yet my variable is "unexpected"

Code: Select all

For %a... 


Is what i was using, I was trying to create a loop for X amount of times or seconds that opens a file.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: For Loop

#2 Post by penpen » 15 Jul 2013 19:01

This is not much code to test, so i only can assume the following happens:

Within a batch file every line is expanded before it is parssed, ... and executed.
This means, that all variables within this line will be expanded to their values when the variable name is within % signs.
A single % sign will not survive this process and then the parser processes a for loop without the % sign so this is a syntax error as the parser expects this character prior to a variable.

The fix is, to escape the % for the expansion process and so use two % characters within a batch file:

Code: Select all

for %%a in (x y z) do echo %%a

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop

#3 Post by foxidrive » 15 Jul 2013 22:09

MLGsuperGame414 wrote:I was trying to create a loop for X amount of times or seconds that opens a file.


Give a real example or you will get just as crap replies.

This opens google in your browser 10 times.

Code: Select all

@echo off
for /L %%a in (1,1,10) do (
start www.google.com
)

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: For Loop

#4 Post by Squashman » 16 Jul 2013 05:35

Also helps to read the help.
H:\>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.

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: For Loop

#5 Post by MLGsuperGame414 » 16 Jul 2013 18:52

Squashman wrote:Also helps to read the help.


I did read the help but didnt understand the syntax, when I typed for /?, I was told that I need to provide a variable. But does it matter what variable? I know some do certain things in some commands so I wasn't sure what to use.


EDIT:
foxidrive wrote:

Code: Select all

@echo off
for /L %%a in (1,1,10) do (
start www.google.com
)

As for foxidrive what do the numbers represent in the parameters after for, (1,1,10) Would the middle number be how many at a time? As for the last number being the final number, and perhaps the first being the start value?

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: For Loop

#6 Post by Squashman » 17 Jul 2013 06:39

MLGsuperGame414 wrote:As for foxidrive what do the numbers represent in the parameters after for, (1,1,10) Would the middle number be how many at a time? As for the last number being the final number, and perhaps the first being the start value?

Again, what is wrong with reading the HELP.

Code: Select all

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)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: For Loop

#7 Post by Samir » 17 Jul 2013 10:34

I think a dead simple example will help a bit:

Code: Select all

for %f in (1 2 3 4) do dir %f
What this will do is the following:

Code: Select all

dir 1
dir 2
dir 3
dir 4
Make sense?

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: For Loop

#8 Post by MLGsuperGame414 » 20 Jul 2013 09:49

Okay so I could have 4 possible dirs to choose from? Or x amount?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: For Loop

#9 Post by Samir » 20 Jul 2013 11:33

MLGsuperGame414 wrote:Okay so I could have 4 possible dirs to choose from? Or x amount?
In this particular example the command 'dir x' is run x times. If I would have put in 1-20 there it would have been dir 1-20.

Post Reply