For with/F
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 common and strongest command, mainly used to process files and output results of some commands.
File indicates one or more files.
String represents a string
Command represents the command
["Options"] Optional
For/F % I in (File) do command
File is the file name. According to the official statement, for will open the files in the file in sequence, and read each file to the memory before proceeding to the next file, and divide each row into one element, ignore blank rows. Let's look at an example.
Assume that the.txt file contains the following content:
1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns
What Command do you want to use to display a.txt content? Of course, it is type, type a.txt
For can also complete the same command:
For/F % I in (a.txt) Do echo % I
We should execute the statement in parentheses first. Because the statement contains the parameter/F, we will open a.txt first. Then we will read all the content in a.txt, use it as a set, and use each row as an element, so we will generate such a set,
{"1st rows, 1st columns, 1st rows, 2nd columns, 1st columns, 3rd Columns", // The first element
"2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd columns, 3rd columns", // second element
"3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd columns"} // third element
There are only three elements in the Set, and % I is also used to replace each element in sequence, and then the command after do is executed.
Specific process:
Replace "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, and 3rd columns" with % I, and execute echo % I after do, "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd columns ",
Replace "2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, and 3rd columns" with % I, "2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd columns ",
Until each element is replaced.
To enhance understanding of the role of/F, execute the following two commands for comparison:
For/F % I in (a.txt) Do echo % I // This will display the content in a.txt, because/F will read a.txt
.
For % I in (a.txt) Do echo % I // This only displays the.txt name and does not read the content.
Through the above learning, we found that for/F will use each row as an element by default, but what if we want to break down each row into smaller content? Don't worry, the for command also provides us with more detailed parameters so that we can divide each row into smaller elements.
They are:Delims and tokens
Delims is used to tell each line for what should be used as a separator. The default Delimiter is space and Tab key.
For example, if the above file is used, we can execute the following command:
For/F "delims =" % I in (a.txt) Do echo % I
The result is as follows:
1st rows and 1st Columns
2nd rows and 1st Columns
3rd rows and 1st Columns
Why. The delims parameter = is followed by a space, which means that each element is separated by a space. By default, only the first element after the separation is obtained.
The execution process is:
The first element "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, and 3rd columns" is divided into three elements: "1st rows and 1st columns" "1st rows and 2nd columns" and "1st rows and 3rd columns", it takes only the first one by default, that is, "1st rows and 1st columns ", then execute the command after do, and so on.
But there are limitations. What if we want the second column of each row?
At this time,TokensIn other words, I can do it.
It is used to control which one or more elements to take when each row is divided into smaller elements through delims.
In the preceding example, run the following command:
For/F "tokens = 2 delims =" % I in (a.txt) Do echo % I
Execution result:
1st rows and 2nd Columns
2nd rows and 2nd Columns
3rd rows and 2nd Columns
To display the third column, replace it with tokens = 3.
At the same time, tokens supports wildcard * and a limited range.
If you want to display the second and third columns, replace them with tokens = 2, 3 or tokens = 2-3. If there are more columns, such as tokens = 2-10.
The command is as follows:
For/F "tokens = 2, 3 delims =" % I in (a.txt) Do echo % I % J
How to add one more % J?
This is because your tokens needs to take the two columns of each row, replace the second column with % I, and replace the third column with % J.
And it must be in alphabetical order. % J cannot be changed to % K, Because I is followed by J
The execution result is:
1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 2nd columns, 3rd rows, 3rd Columns
If the wildcard * is used, the entire line or the rest of the line is treated as an element.
For example:
For/F "tokens = * delims =" % I in (a.txt) Do echo % I
The execution result is:
1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns
In fact, it is the same as the execution result of do echo % I for/F % I in (a.txt.
Another example is:
For/F "tokens = 2, * delims =" % I in (a.txt) Do echo % I % J
The execution result is:
1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 2nd columns, 3rd rows, 3rd Columns
Replace the second column with % I, and replace the remaining columns with % J.
Finally, there is a combination of skip and EOL. The two are simple. Skip is to ignore the first number of lines of the file, and EOL is used to specify the symbols starting with a line, and ignore it.
For example:
For/F "Skip = 2 tokens = *" % I in (a.txt) Do echo % I
Result:
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns
Skip is used to tell for to skip the first two rows.
If tokens = * is not added, the execution result is:
3rd rows and 1st Columns
I don't know what's going on.
For example, when the.txt content is changed:
. 1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
. 2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns
RunFor/F "EOL =. tokens = *" % I in (a.txt) Do echo % IThe result is:
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns
EOL is used to tell for to ignore rows starting.
You must also add tokens = *. Otherwise, only "3rd rows and 1st columns" are displayed"