For Statement

Date/Time:


Gen

The for loop is similar to the summation function in mathematics, the summation symbol in mathematics is the Greek symbol of sigma. In the sigma symbol, we have a start number, incremental number, and a final number. In side of that, we have a function. We also have a sequence of numbers, where you have n and a function. The function being the sequence itself, for example 2 n^2. When n=1 2, n=2 8, n=3 18, etc. In the summation sequence, we have to add all the numbers in the sequence.

Gen
A For loop has a set of conditions which must be met in order for the program to stop. In the number sequence if we set the number to 10, it will not stop until 10 is reached. It will look for the conditions, and it will execute the command. Then, the command will finish, and that is how a for loop works. Just like in the sequence of numbers, we have a set parameters, and we go through the sequence from when n=1 to 5. When it reaches 5, we have our last sequence of numbers.
@Echo off
:start
for /l %%a in (1;1;10) do (
echo %%a
)
pause
In this line of code, /L means loop, so (start number, increment, final number). In this code, the start number will be 1, the increment is 1, and the last number is 10.

Output
1
2
3
4
5
6
7
8
9
10

We can list other things as well, like a letters as well.

@echo off

for %%x in (a b c d e f g h) do (
echo %%x
)
pause

Output
a
b
c
d
e
f
g
h

We can do other things like look for directories in a drive:

@echo off
set back=%cd%
for /d %%i in (C:\temp\*) do (
cd “%%i”
echo current directory:
cd
pause
)
cd %back%

[In this code, I am looking for the directory c:\temp, so if the c:\temp directory is found it will show us what sub directories are in that directory]

output:
current directory:
C:\Temp\NVIDIA
Press and key to continue

We can search for multiple things.

@echo off

for /D %%y IN (d:\test\* d:\test2\* d:\test3\) Do (
xcopy d:\system.txt %%y
)
echo All files sent to the following directories.
pause

Output:
d:\system.txt
1 file(s) copied [copied to d:\test\A]
d:\system.txt
1 file(s) copied [copied to d:\test2\A]
d:\system.txt
1 file(s) copied [copied to d:\test2\B]
d:\system.txt
1 file(s) copied [copied to d:\test3

d:\test contains A or d:\test\A
d:\test2 contains A and B d:\test2\A d:\test2\B
In this script, it the for loop the directories d:\test, d:\test2, and d:\test3. If these directories are found, it will copy d:\system.txt file to all the subdirectories of d:\test, and to all the sub directories of test2. It will also copy system.txt file to d:\test3 directory. The \* means it will copy to all sub directories in the directory. If you leave out the *, it will copy all the files to the selected directory, like d:\test3. In side the do parenthesis, you can add more than one command. In the IN parenthesis, you can do a number of for statements as well.

The for loop is very useful to use when you want to be selective. If none of the conditions were found in the script will just finish or exit. You can do a lot of things with this. For example, let’s say you want to make a directory for all the sub directories in c:\customers. You don’t have to do it one at a time, you can just use the for loop to do it.
@echo off

for /D %%y IN (c:\customers) Do (
cd %%y
mkdir “billing Info” “%%y”
)
echo Billing directory was made for all the customers in the customers echo folder
pause

In this script, notice how I put cd %%y. I did this so I can go into the customers directory, there I can make billing for all the customers in that folder. Also notice how I put double quotes “%%y”, I did this so billing Info would go into the c:\customers directory. If I didn’t add the double quotations, it will only make the billing folder, and not the billing info folder.

For Loop Video