Summary of delims and tokens in batch processing for

Time:2021-2-25

In the parameter f of the for command statement, the most difficult to understand is the options delims and tokens. This paper makes a simple comparison and summary.
“For / F” is often used to parse text and read strings. In the division of labor, delims is responsible for cutting strings, while tokens is responsible for extracting strings. If the string is regarded as a cake, delims is like a knife, which is used to cut the cake, and tokens is like a fork, which is used to take the cut cake. Let’s understand it with examples.
Save the following as a text file “song list. TXT”. Note that the extension is “. TXT”:
Serial number, singer name – song name. Suffix
1. Rao Tianliang – Rose lover.wma
2. High one – I don’t want to miss. Mp3
3. Huang Kaiqin – sentimental lover
4. Huang can – Huang rose.lrc
5. Lizi – such a love story.mp3
Code 1: show all
@echo off
For / F%% I in (song list. Txt) do echo%% I
pause>nul
Results of operation:
Serial number, singer name – song name. Suffix
1. Rao Tianliang – Rose lover.wma
2. High one – I don’t want to miss. Mp3
3. Huang Kaiqin – sentimental lover
4. Huang can – Huang rose.lrc
5. Lizi – such a love story.mp3
Explanation:
If the parameter “F” is not used, the running result will only display the text character “song list. TXT” in brackets, but cannot read the content of the text file “song list. TXT”. It can be seen that “/ F” is a good tool for parsing text strings.
1、 Delims
If only serial number, not singer name, song name and suffix, how to do it?
Code 2: the first column is extracted by default
@echo off
For / F “delims =,”%% I in (song list. Txt) do echo%% I “
pause>nul
Results of operation:
Serial number
1
2
3
4
5
Explanation:
“Delims =,” means to define the Dun sign “and” as the separator, and use the separator “and” to segment the text string. String is the content of “song list. TXT”, that is, the text and punctuation in the file.
The donut is in the original. In addition to the “don” and “don”, there are also “minus” and “dot” in the original text, so you can also use them as separators.
Code 3: use the minus sign “-” as the separator
@echo off
For / F “delims = -“%% I in (song list. Txt) do echo%% I
pause>nul
Results of operation:
Serial number, singer name
1. Rao Tianliang
2. Senior one
3. Huang Kaiqin
4. Huang can
5. Lizi
Explanation:
Because when the minus sign “-” is used as a separator, the content of each line is divided into two parts by the minus sign “-” and only the first part is displayed by default. The latter part and the separator minus sign “-” are ignored (omitted).
Code 4: use the dot “.” as the separator
@echo off
For / F “delims =.%% I in (song list. Txt) do echo%% I
pause>nul
Results of operation:
Serial number, singer name song name
1. Rao Tianliang – rose lover
2. Senior one – I don’t want to miss it
3. Huang Kaiqin – sentimental lover
4. Huang can – Huang rose
5. Lizi – such a love story
Explanation:
By default, when using delims instead of tokens, only the contents before the first separator will be displayed, and the contents after the first separator and the first separator will be ignored.
Code 5: define multiple separators
@echo off
For / F “delims =, -.%% I in (song list. Txt) do echo%% I
pause>nul
Results of operation:
Serial number
1
2
3
4
5
Explanation:
The reason is that the original text is divided into four parts when the punctuation symbols of “Dun”, “Dun”, “minus” and “dot” are defined as separators.
For example, the second line “1, Rao Tianliang – rose lover. Wma” will be divided into four parts: “1”, “Rao Tianliang”, “rose lover” and “wma”.
From the first row to the last row, each part of each row corresponds to a vertical column. Therefore, the original text has four columns of “serial number”, “singer name”, “song name” and “suffix name”.
Generally, only the contents of the first column are read. The following content needs to be extracted with the tokens option.