Batch For Command Explanation
The FOR command is basically used to process text, but there are also some other useful functions!
Take a look at its basic format (here I am citing the format in batch processing; in the command line, only one % sign is needed)
FOR parameter %% variable name IN (related files or commands) DO executed command
Parameters: FOR has 4 parameters /d /l /r /f, and their functions are explained below with examples
%% variable name: This variable name can be lowercase a-z or uppercase A-Z, they are case-sensitive, and FOR will assign each read value to it;
IN: The format of the command, just write it as it is;
(related files or commands): What FOR reads and assigns to the variable, see the example below
do: The format of the command, just write it as it is!
The executed command: Write what operation to perform on each variable value here.
You can enter for /? in CMD to see the help provided by the system! Compare
FOR %%variable IN (set) DO command [command-parameters]
%%variable specifies a single letter replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specifies parameters or command line switches for a specific command.
Now start to explain the meaning of each parameter
/d
Only for directories
If Set (that is, the "related files or commands" I wrote above) contains wildcards (* and ?), it will execute the specified Command for each directory that matches Set (instead of the group of files in the specified directory).
System help format: FOR /D %%variable IN (set) DO command
It is mainly used for directory search and will not search for files. Look at such an example
@echo off
for /d %%i in (*) do @echo %%i
pause
Save it in the root directory of drive C and execute it, and it will print out all the directory names under the C drive, and none of the file names will be displayed!
Another one, for example, we want to print out the names of folders in the current path that have only 1-3 letters
@echo off
for /d %%i in (???) do @echo %%i
pause
In this way, if there are directories in your current directory with only 1-3 letters, they will be displayed; if not, nothing will be displayed.
Thinking question:
@echo off
for /d %%i in (window?) do @echo %%i
pause
Save it to the C drive and execute it, what will be displayed? Just see for yourself!
The /D parameter can only display the directory names in the current path, everyone should pay attention to this!
/R
Recursive
Enter the root directory tree [Drive:]Path, and execute the for statement in each directory of the tree. If no directory is specified after /R, the current directory is considered. If Set is just a period (.), only the directory tree is enumerated.
System help format: FOR /R [[drive:]path] %%variable IN (set) DO command
We know above that /D can only display the directory names in the current path, so now this /R is also related to directories, what can it do? Don't worry, it is much stronger than /D!
It can read all the file names under the current or the specified path, note that it is the file name, see the example for what it is used for!
@echo off
for /r c:\ %%i in (*.exe) do @echo %%i
pause
Let's save this BAT in any place on drive D and then execute it, and we will see that it lists all the EXE files under the root directory of drive C and in each subdirectory of the directory. Here, c:\ is the directory.
Another one
@echo off
for /r %%i in (*.exe) do @echo %%i
pause
The parameters are different. This command does not add that C:\ in front, that is, the search path, so it will use the current directory as the search path. For example, if you put this BAT in the d:\test directory and execute it, then it will list all the EXE files in the D:\test directory and its subdirectories!!!
/L
Iterate over a range of numbers
Use the iteration variable to set the starting value (Start#), then gradually execute a group of values in the range until the value exceeds the set end value (End#). /L will perform the iteration variable by comparing Start# with End#. If Start# is less than End#, the command will be executed. If the iteration variable exceeds End#, the command interpreter exits this loop. You can also use a negative Step# to gradually execute the values in this range in a decreasing numerical way. For example, (1,1,5) generates the sequence 1 2 3 4 5, and (5,-1,1) generates the sequence (5 4 3 2 1). The syntax is:
System help format: for /L %% Variable in (Start#,Step#,End#) do Command
For example:
@echo off
for /l %%i in (1,1,5) do @echo %%i
pause
Save and execute to see the effect, it will print the numbers from 1 2 3 4 5 like this.
The parameter (1,1,5) means starting from 1, adding 1 each time until 5 ends!
Look at this example again
@echo off
for /l %%i in (1,1,5) do start cmd
pause
Did you get a shock after execution? How come there are 5 more CMD windows, hehe! If you change that (1,1,5) to (1,1,65535), what will happen? I'll tell you first, 65535 CMD windows will be opened.... If you don't crash, you are strong!
Of course, we can also change that start cmd to md %%i, and then it will create the specified number of directories... with names from 1 to 65535.
After watching this destructive parameter given by me, let's look at the last parameter
/f
Detailed explanation of FOR with /F
The FOR with /F has great use and is used the most in batch processing. The usage is as follows:
Format:
FOR /F ["options"] %%i IN (file) DO command
FOR /F ["options"] %%i IN ("string") DO command
FOR /F ["options"] %%i IN ('command') DO command
This is probably the most commonly used and most powerful command, mainly used to process the output results of files and some commands.
file represents one or more files
string represents a string
command represents a command
["options"] is optional
For FOR /F %%i IN (file) DO command
file is the file name. According to the official statement, for will open the file in file one by one and read each file into memory before proceeding to the next file, divide it into elements one by one according to each line, and ignore blank lines. Let's take an example.
Suppose there is the following content in file a.txt:
First line, first column First line, second column First line, third column
Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
What command would you use to display the content in a.txt? Of course it is type, type a.txt
for can also complete the same command:
for /f %%i in (a.txt) do echo %%i
Still start from the parentheses execution, because there is the parameter /f, so for will first open a.txt, then read all the content in a.txt, treat it as a set, and take each line as an element, so this set will be generated,
{"First line, first column First line, second column First line, third column", // first element
"Second line, first column Second line, second column Second line, third column", // second element
"Third line, first column Third line, second column Third line, third column"} // third element
There are only 3 elements in the set, and %%i is used to replace each element in turn, and then the command after do is executed.
Specific process:
Replace %%i with "First line, first column First line, second column First line, third column", execute the echo %%i after do, and display "First line, first column First line, second column First line, third column",
Replace %%i with "Second line, first column Second line, second column Second line, third column", execute echo %%i, and display "Second line, first column Second line, second column Second line, third column",
And so on until each element is replaced.
To strengthen the understanding of the role of /f, please execute the following two commands and compare to understand:
for /f %%i in (a.txt) do echo %%i // This will display the content in a.txt because of the role of /f, which will read the content in a.txt.
for %%i in (a.txt) do echo %%i // And this will only display the name a.txt and will not read its content.
Through the above study, we find that for /f will default to taking each line as an element, but if we also want to decompose each line into smaller content, what should we do? Don't worry, the for command also provides us with more detailed parameters, which makes it possible for us to decompose each line into smaller elements.
They are: delims and tokens
delims is used to tell for what to use as the delimiter for each line. The default delimiters are spaces and tab keys.
For example, for the above file, we execute the following command:
for /f "delims= " %%i in (a.txt) do echo %%i
The result displayed is:
First line, first column
Second line, first column
Third line, first column
Why is this? Because there is the delims parameter here, and there is a space after =, which means to split each element with a space again, and it defaults to only taking the first element after splitting.
The execution process is:
Split the first element "First line, first column First line, second column First line, third column" into three elements: "First line, first column" "First line, second column" "First line, third column", it defaults to only taking the first one, that is, "First line, first column", then execute the command after do, and so on.
But this is still limited. If we want the second column element of each line, how can we do it?
At this time, tokens comes out and says that I can do it.
Its role is when you divide each line into smaller elements through delims, it controls which one or several to take.
Still in the above example, execute the following command:
for /f "tokens=2 delims= " %%i in (a.txt) do echo %%i
Execution result:
First line, second column
Second line, second column
Third line, second column
If you want to display the third column, just change it to tokens=3.
At the same time, tokens supports wildcards *, as well as limited ranges.
If you want to display the second column and the third column, then change it to tokens=2,3 or tokens=2-3, and if there are more, it is tokens=2-10 and so on.
The command at this time is:
for /f "tokens=2,3 delims= " %%i in (a.txt) do echo %%i %%j
Why is there an extra %%j?
This is because you need to take two columns of each line behind tokens, use %%i to replace the second column, and use %%j to replace the third column.
And it must be arranged in alphabetical order, %%j cannot be replaced with %%k, because i is followed by j.
The execution result is:
First line, second column First line, third column
Second line, second column Second line, third column
Third line, second column Third line, third column
For the wildcard *, it is to treat this line as a whole or the remaining part of this line as an element.
For example:
for /f "tokens=* delims= " %%i in (a.txt) do echo %%i
The execution result is:
First line, first column First line, second column First line, third column
Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
In fact, it is the same as the execution result of for /f %%i in (a.txt) do echo %%i.
Another example:
for /f "tokens=2,* delims= " %%i in (a.txt) do echo %%i %%j
The execution result is:
First line, second column First line, third column
Second line, second column Second line, third column
Third line, second column Third line, third column
Replace %%i with the second column and %%j with the remaining all.
Finally, there are skip and eol, these two are simple. skip is to ignore the first few lines of the file, and eol is used to specify that if a line starts with what symbol, it will be ignored.
For example:
for /f "skip=2 tokens=*" %%i in (a.txt) do echo %%i
The result is:
Third line, first column Third line, second column Third line, third column
Use skip to tell for to skip the first two lines.
If you don't add tokens=* in front, the execution result is:
Third line, first column
I don't know why.
Again, when the content of a.txt becomes:
. First line, first column First line, second column First line, third column
. Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
Execute for /f "eol=. tokens=*" %%i in (a.txt) do echo %%i, and the result is:
Third line, first column Third line, second column Third line, third column
Use eol to tell for to ignore the lines starting with ".".
Also, tokens=* must be added, otherwise only "Third line, first column" will be displayed.
The FOR command is basically used to process text, but there are also some other useful functions!
Take a look at its basic format (here I am citing the format in batch processing; in the command line, only one % sign is needed)
FOR parameter %% variable name IN (related files or commands) DO executed command
Parameters: FOR has 4 parameters /d /l /r /f, and their functions are explained below with examples
%% variable name: This variable name can be lowercase a-z or uppercase A-Z, they are case-sensitive, and FOR will assign each read value to it;
IN: The format of the command, just write it as it is;
(related files or commands): What FOR reads and assigns to the variable, see the example below
do: The format of the command, just write it as it is!
The executed command: Write what operation to perform on each variable value here.
You can enter for /? in CMD to see the help provided by the system! Compare
FOR %%variable IN (set) DO command [command-parameters]
%%variable specifies a single letter replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specifies parameters or command line switches for a specific command.
Now start to explain the meaning of each parameter
/d
Only for directories
If Set (that is, the "related files or commands" I wrote above) contains wildcards (* and ?), it will execute the specified Command for each directory that matches Set (instead of the group of files in the specified directory).
System help format: FOR /D %%variable IN (set) DO command
It is mainly used for directory search and will not search for files. Look at such an example
@echo off
for /d %%i in (*) do @echo %%i
pause
Save it in the root directory of drive C and execute it, and it will print out all the directory names under the C drive, and none of the file names will be displayed!
Another one, for example, we want to print out the names of folders in the current path that have only 1-3 letters
@echo off
for /d %%i in (???) do @echo %%i
pause
In this way, if there are directories in your current directory with only 1-3 letters, they will be displayed; if not, nothing will be displayed.
Thinking question:
@echo off
for /d %%i in (window?) do @echo %%i
pause
Save it to the C drive and execute it, what will be displayed? Just see for yourself!
The /D parameter can only display the directory names in the current path, everyone should pay attention to this!
/R
Recursive
Enter the root directory tree [Drive:]Path, and execute the for statement in each directory of the tree. If no directory is specified after /R, the current directory is considered. If Set is just a period (.), only the directory tree is enumerated.
System help format: FOR /R [[drive:]path] %%variable IN (set) DO command
We know above that /D can only display the directory names in the current path, so now this /R is also related to directories, what can it do? Don't worry, it is much stronger than /D!
It can read all the file names under the current or the specified path, note that it is the file name, see the example for what it is used for!
@echo off
for /r c:\ %%i in (*.exe) do @echo %%i
pause
Let's save this BAT in any place on drive D and then execute it, and we will see that it lists all the EXE files under the root directory of drive C and in each subdirectory of the directory. Here, c:\ is the directory.
Another one
@echo off
for /r %%i in (*.exe) do @echo %%i
pause
The parameters are different. This command does not add that C:\ in front, that is, the search path, so it will use the current directory as the search path. For example, if you put this BAT in the d:\test directory and execute it, then it will list all the EXE files in the D:\test directory and its subdirectories!!!
/L
Iterate over a range of numbers
Use the iteration variable to set the starting value (Start#), then gradually execute a group of values in the range until the value exceeds the set end value (End#). /L will perform the iteration variable by comparing Start# with End#. If Start# is less than End#, the command will be executed. If the iteration variable exceeds End#, the command interpreter exits this loop. You can also use a negative Step# to gradually execute the values in this range in a decreasing numerical way. For example, (1,1,5) generates the sequence 1 2 3 4 5, and (5,-1,1) generates the sequence (5 4 3 2 1). The syntax is:
System help format: for /L %% Variable in (Start#,Step#,End#) do Command
For example:
@echo off
for /l %%i in (1,1,5) do @echo %%i
pause
Save and execute to see the effect, it will print the numbers from 1 2 3 4 5 like this.
The parameter (1,1,5) means starting from 1, adding 1 each time until 5 ends!
Look at this example again
@echo off
for /l %%i in (1,1,5) do start cmd
pause
Did you get a shock after execution? How come there are 5 more CMD windows, hehe! If you change that (1,1,5) to (1,1,65535), what will happen? I'll tell you first, 65535 CMD windows will be opened.... If you don't crash, you are strong!
Of course, we can also change that start cmd to md %%i, and then it will create the specified number of directories... with names from 1 to 65535.
After watching this destructive parameter given by me, let's look at the last parameter
/f
Detailed explanation of FOR with /F
The FOR with /F has great use and is used the most in batch processing. The usage is as follows:
Format:
FOR /F ["options"] %%i IN (file) DO command
FOR /F ["options"] %%i IN ("string") DO command
FOR /F ["options"] %%i IN ('command') DO command
This is probably the most commonly used and most powerful command, mainly used to process the output results of files and some commands.
file represents one or more files
string represents a string
command represents a command
["options"] is optional
For FOR /F %%i IN (file) DO command
file is the file name. According to the official statement, for will open the file in file one by one and read each file into memory before proceeding to the next file, divide it into elements one by one according to each line, and ignore blank lines. Let's take an example.
Suppose there is the following content in file a.txt:
First line, first column First line, second column First line, third column
Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
What command would you use to display the content in a.txt? Of course it is type, type a.txt
for can also complete the same command:
for /f %%i in (a.txt) do echo %%i
Still start from the parentheses execution, because there is the parameter /f, so for will first open a.txt, then read all the content in a.txt, treat it as a set, and take each line as an element, so this set will be generated,
{"First line, first column First line, second column First line, third column", // first element
"Second line, first column Second line, second column Second line, third column", // second element
"Third line, first column Third line, second column Third line, third column"} // third element
There are only 3 elements in the set, and %%i is used to replace each element in turn, and then the command after do is executed.
Specific process:
Replace %%i with "First line, first column First line, second column First line, third column", execute the echo %%i after do, and display "First line, first column First line, second column First line, third column",
Replace %%i with "Second line, first column Second line, second column Second line, third column", execute echo %%i, and display "Second line, first column Second line, second column Second line, third column",
And so on until each element is replaced.
To strengthen the understanding of the role of /f, please execute the following two commands and compare to understand:
for /f %%i in (a.txt) do echo %%i // This will display the content in a.txt because of the role of /f, which will read the content in a.txt.
for %%i in (a.txt) do echo %%i // And this will only display the name a.txt and will not read its content.
Through the above study, we find that for /f will default to taking each line as an element, but if we also want to decompose each line into smaller content, what should we do? Don't worry, the for command also provides us with more detailed parameters, which makes it possible for us to decompose each line into smaller elements.
They are: delims and tokens
delims is used to tell for what to use as the delimiter for each line. The default delimiters are spaces and tab keys.
For example, for the above file, we execute the following command:
for /f "delims= " %%i in (a.txt) do echo %%i
The result displayed is:
First line, first column
Second line, first column
Third line, first column
Why is this? Because there is the delims parameter here, and there is a space after =, which means to split each element with a space again, and it defaults to only taking the first element after splitting.
The execution process is:
Split the first element "First line, first column First line, second column First line, third column" into three elements: "First line, first column" "First line, second column" "First line, third column", it defaults to only taking the first one, that is, "First line, first column", then execute the command after do, and so on.
But this is still limited. If we want the second column element of each line, how can we do it?
At this time, tokens comes out and says that I can do it.
Its role is when you divide each line into smaller elements through delims, it controls which one or several to take.
Still in the above example, execute the following command:
for /f "tokens=2 delims= " %%i in (a.txt) do echo %%i
Execution result:
First line, second column
Second line, second column
Third line, second column
If you want to display the third column, just change it to tokens=3.
At the same time, tokens supports wildcards *, as well as limited ranges.
If you want to display the second column and the third column, then change it to tokens=2,3 or tokens=2-3, and if there are more, it is tokens=2-10 and so on.
The command at this time is:
for /f "tokens=2,3 delims= " %%i in (a.txt) do echo %%i %%j
Why is there an extra %%j?
This is because you need to take two columns of each line behind tokens, use %%i to replace the second column, and use %%j to replace the third column.
And it must be arranged in alphabetical order, %%j cannot be replaced with %%k, because i is followed by j.
The execution result is:
First line, second column First line, third column
Second line, second column Second line, third column
Third line, second column Third line, third column
For the wildcard *, it is to treat this line as a whole or the remaining part of this line as an element.
For example:
for /f "tokens=* delims= " %%i in (a.txt) do echo %%i
The execution result is:
First line, first column First line, second column First line, third column
Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
In fact, it is the same as the execution result of for /f %%i in (a.txt) do echo %%i.
Another example:
for /f "tokens=2,* delims= " %%i in (a.txt) do echo %%i %%j
The execution result is:
First line, second column First line, third column
Second line, second column Second line, third column
Third line, second column Third line, third column
Replace %%i with the second column and %%j with the remaining all.
Finally, there are skip and eol, these two are simple. skip is to ignore the first few lines of the file, and eol is used to specify that if a line starts with what symbol, it will be ignored.
For example:
for /f "skip=2 tokens=*" %%i in (a.txt) do echo %%i
The result is:
Third line, first column Third line, second column Third line, third column
Use skip to tell for to skip the first two lines.
If you don't add tokens=* in front, the execution result is:
Third line, first column
I don't know why.
Again, when the content of a.txt becomes:
. First line, first column First line, second column First line, third column
. Second line, first column Second line, second column Second line, third column
Third line, first column Third line, second column Third line, third column
Execute for /f "eol=. tokens=*" %%i in (a.txt) do echo %%i, and the result is:
Third line, first column Third line, second column Third line, third column
Use eol to tell for to ignore the lines starting with ".".
Also, tokens=* must be added, otherwise only "Third line, first column" will be displayed.
