Loops in Batch Scripting Language
Loops in Batch Scripting Language
MENU
Standard Delimiters
Space
Comma ( , )
Semi-colon ( ; )
TAB
Syntax:
?
1 @rem set_of_files - Set of Files
2 @rem Files separated by standard delimiters.
3 @rem The parameter name 'variable' must be 1 character
4
5 FOR %%variable IN ( set_of_files ) DO command
6
7 @rem Hoặc:
8
9 FOR %%parameter IN ( set_of_files ) DO (
10
11 command
12 )
Example: copy some files into a directory (Note: the files to be copied into the target directory need to be in the same disk drive).
copyFiles.bat
?
1 @echo off
2
https://o7planning.org/en/11623/loops-in-batch-scripting-language 1/11
6/23/2020 Loops in Batch Scripting Language
3 @rem Copy to same Disk
4
5 FOR %%f IN (E:\test\file1.data E:\test\file2.txt) DO (
6
7 echo Copying %%f
8 copy %%f E:\backup
9 )
10
11
12 pause
Other example:
?
1 @rem The delimiter is a semicolon (;)
2 FOR %%f IN ("E:\my dir\file1.data" ; E:\test\file2.txt) DO copy %%f E:\backup
3
4 @rem The delimiter is a comma ( , )
5 FOR %%f IN ("E:\my dir\file1.data" , E:\test\file2.txt) DO copy %%f E:\backup
6
7 @rem The delimiter is a space.
8 FOR %%f IN ("E:\my dir\file1.data" E:\test\file2.txt) DO copy %%f E:\backup
3- For /R
The FOR /R loop is used to iterate over the list of files, including files in subdirectories, grandchildren .. It is called Recurse loop.
Syntax:
?
1 FOR /R [path] %%variable IN ( set_of_file_filters ) DO command
2
3 @rem Or:
4
5 FOR /R [path] %%variable IN ( set_of_file_filters ) DO (
6
7 command
8 )
[path] : This parameter is a root folder. If there is not this parameter "the folder containing executable script file" hoặc
"current folder" will be considered as root folder .
set_of_file_filters : List of file filters, for example *.txt , *.bat, ... or dot ( . ) means all.
variable: means name of variable and must have one unique characte.
The following example prints the list of all *.txt or *.log files in the C:/Windows/System32 directory (Includes search in
subdirectories, grandchildren ..)
forR_example1.bat
?
1 @echo off
2
3 FOR /R "C:\Windows\System32" %%f IN (*.txt *.log) DO (
4
https://o7planning.org/en/11623/loops-in-batch-scripting-language 2/11
6/23/2020 Loops in Batch Scripting Language
5 echo %%f
6 )
7
8 pause
9
Example of listing all files in the C:/Windows/System32 directory (including files in subdirectories, grandchildren ...)
forR_example2.bat
?
1 @echo off
2
3
4 FOR /R "C:\Windows\System32" %%f IN ( . ) DO (
5
6 echo %%f
7 )
8
9 pause
4- For /D
The FOR /D loop is used to iterate over the list of directories which are subdirectories of current directory.
Syntax:
?
1 FOR /D [/r] %%variable IN ( set_of_directory_filters ) DO command
2
3 @rem Or:
4
5 FOR /D [/r] %%parameter IN ( set_of_directory_filters ) DO (
6
7 command
8 )
set_of_directory_filters : List of directory filters for example, en*, fr*,.. separated by a standard delimiter.
[/r]: This is a recurse parameter and not mandatory. If this parameter is available, subdirectories, grandchildren .. will be
envolved in the loop.
variable: means name of variable and must have a unique character.
forD_example1.bat
https://o7planning.org/en/11623/loops-in-batch-scripting-language 3/11
6/23/2020 Loops in Batch Scripting Language
?
1 @echo off
2
3 C:
4 cd C:/Windows
5
6 FOR /D %%d IN ( * ) DO (
7
8 echo %%d
9 )
10
11 pause
Example: List subdirectories, grandchildren ... of the C:/Windows with the name started by "en" or "fr"
forD_example2.bat
?
1 @echo off
2
3 C:
4 cd C:/Windows
5
6 FOR /D /r %%d IN (en* fr*) DO (
7
8 echo %%d
9 )
10
11 pause
5- For /L
For /R loop is used to iterate over a range of numbers.
Syntax:
?
1 FOR /L %%variable IN (start, step, end) DO command
2
3 @rem Or:
4
5 FOR /L %%variable IN (start, step, end) DO (
6
7 command
8 )
Example:
https://o7planning.org/en/11623/loops-in-batch-scripting-language 4/11
6/23/2020 Loops in Batch Scripting Language
forL_example1.bat
?
1 @echo off
2
3
4 FOR /L %%d IN (1 2 8 ) DO (
5
6 echo %%d
7 )
8
9 pause
forL_example2.bat
?
1 @echo off
2
3
4 FOR /L %%d IN (20 -2 5 ) DO (
5
6 echo %%d
7 )
8
9 pause
10
6- For /F
The For /F loop is a complex but powerful loop. It reads a file or a few files and then analyzes the contents of files. The content of
a file is a text; it is split into several small pieces of text, each of which is called a Token. The default rule for separating a text is
based on white space. However, you can customize the delimiter rule by ["delims = xxx"] parameter.
The For / F loop is also used to analyze the contents of a string, or to execute a set of commands.
https://o7planning.org/en/11623/loops-in-batch-scripting-language 5/11
6/23/2020 Loops in Batch Scripting Language
Syntax:
?
1 FOR /F ["options"] %%variable IN ( set_of_filenames ) DO command
2
3 FOR /F ["options"] %%variable IN ( set_of_filenames ) DO (
4
5 command
6 )
7
8
9 FOR /F ["options"] %%variable IN ("Text string to process") DO command
10
11 FOR /F ["options"] %%variable IN ("Text string to process") DO (
12
13 command
14 )
Option Description
eol (End of Line): Specifies a special character, which is put at the beginning of a line to mark this line as
eol=;
comment line. Comment lines will be ignored by the program, defaulted semicolon character (;)
tokens=n1,n2,n3 defines the positions cared about (n1, n2, n3, ..), defaulted tokens=1
usebackq (See explanation in examples)
https://o7planning.org/en/11623/loops-in-batch-scripting-language 6/11
6/23/2020 Loops in Batch Scripting Language
eol: Used to specify a special character, which is by default a semicolon character (;). It is placed at the beginning of a line to mark
that such line is a comment. The program will ignore this line.
skip=n
skip: Declare the number of first lines of the file will be ignored, the program will not analyze these lines. Default skip = 0
https://o7planning.org/en/11623/loops-in-batch-scripting-language 7/11
6/23/2020 Loops in Batch Scripting Language
delims: Defines delimiter characters, helping the program to delimit each line of text into sub-paragraphs, each of which is called a
Token. Tokens are marked indices: 1, 2, 3, ...
tokens=n1,n2,n3
tokens: Declare a list of indices that are interested in, separated by commas. For example, tokens = 1,2,4, defaulted: tokens = 1
Tokens Description
tokens=1,2,4 The indices such as 1, 2, 4 are interested.
tokens=2-8 The indices of 2 to 8 are interested
For example, "tokens=1,2,4", means that Tokens at the position of 1, 2, 4 are interested and other Tokens will be ignored.
https://o7planning.org/en/11623/loops-in-batch-scripting-language 8/11
6/23/2020 Loops in Batch Scripting Language
persons.txt
?
1 List of celebrities
2 Creation date: 2017/09/11
3
4 ; (Comment) Male List
5 Bill,Gates,1955/10/28,Male
6 Steve,Jobs,1955/02/24,Male
7 Mark,Zuckerberg,1984/05/16,Male
8 Sundar,Pichai,1972/07/12,Male
9 ; (Comment) Female List
10 Hillary,Clinton,1947/10/26,Female
forF_example1.bat
?
1 @echo off
2
3
4 FOR /F "delims=, skip=3 eol=; tokens=1,2,4" %%i IN ( persons.txt ) DO (
5
6 echo Full Name: %%i %%j Gender: %%k
7
8 )
9
10 pause
11
12
Biến %%i variable is explicitly declared on the loop. %% j, %% k variables are implicitly declared (names of implicit
variables are the next characters of explicit variable names).
https://o7planning.org/en/11623/loops-in-batch-scripting-language 9/11
6/23/2020 Loops in Batch Scripting Language
TODO
Newest Documents
Android CharacterPickerDialog
Android Dialog
Android TimePickerDialog
Create a simple File Chooser in Android
Create a simple File Dialog Finder in Android
Android Wifi Scanning
Upgrade Mac Operating System
How do I take a MacOS Retina screenshot and get the image at its actual size?
iOS Tutorial for Beginners - Hello iOS
Android Phone Call
o7planning.org
https://o7planning.org/en/11623/loops-in-batch-scripting-language 10/11
6/23/2020 Loops in Batch Scripting Language
https://o7planning.org/en/11623/loops-in-batch-scripting-language 11/11