help with 'for' command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

help with 'for' command

#1 Post by Rileyh » 05 Sep 2011 21:45

Can someone please give me an easy to understand overview of the 'for' command

Thanks,
Rileyh

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: help with 'for' command

#2 Post by nitt » 05 Sep 2011 22:22

Rileyh wrote:Can someone please give me an easy to understand overview of the 'for' command

Thanks,
Rileyh


No one's going to explain this huge command to you. You can understand the "for /f" part here.

I'll also quickly explain the "for /l" part. This one is easy.

Code: Select all

for /l %%a in (0;5;10) do (
echo %%a
)


This is much similar to the C-type FOR loop.

"%%a" is your output variable. Every time your code loops, it sets "%%a" to something. "for /l" uses the whole "(begin;increment;stop)" thing. In C-type languages, it's usually "(being;stop;increment)", so "for /l" is similar, but not the same.

"0" is where we are starting. So we are setting "%%a" to "0". Now every time the code loops (a loop is done once all lines of code in between the do() is finished) it will add on to %%a by the increment. The increment is "5". So every time it loops, it add 5 to %%a. Now we are stopping once it's greater than or equal to "10". That is the "stop". So when we say "0;5;10", we are saying "set %%a to 0;add 5 to it for each loop;stop when %%a >= 10".

This is what it will output:

Code: Select all

0
5
10

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help with 'for' command

#3 Post by Rileyh » 06 Sep 2011 22:38

Thanks nitt,
This has been helpful to me.
After looking it up on "cmd.exe" i saw it was extremely long and conveluted to explain.
Sorry for such an ask.

THanks,
Rileyh

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: help with 'for' command

#4 Post by Rileyh » 06 Sep 2011 23:20

Hi nitt,
I tested the code you posted and it didn't work.
I placed a pause after each line and it still didn't turn up as anything.
I'm not sure why though

Rileyh

Post Reply