Page 1 of 1
For Loop
Posted: 15 Jul 2013 18:36
by MLGsuperGame414
I was wondering how to use the for loop command, I tried yet my variable is "unexpected"
Is what i was using, I was trying to create a loop for X amount of times or seconds that opens a file.
Re: For Loop
Posted: 15 Jul 2013 19:01
by penpen
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:
penpen
Re: For Loop
Posted: 15 Jul 2013 22:09
by foxidrive
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
)
Re: For Loop
Posted: 16 Jul 2013 05:35
by Squashman
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.
Re: For Loop
Posted: 16 Jul 2013 18:52
by MLGsuperGame414
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?
Re: For Loop
Posted: 17 Jul 2013 06:39
by Squashman
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)
Re: For Loop
Posted: 17 Jul 2013 10:34
by Samir
I think a dead simple example will help a bit:
What this will do is the following:
Make sense?
Re: For Loop
Posted: 20 Jul 2013 09:49
by MLGsuperGame414
Okay so I could have 4 possible dirs to choose from? Or x amount?
Re: For Loop
Posted: 20 Jul 2013 11:33
by Samir
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.