中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 21:25
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Seeking help on the use of the tokens parameter in the for command?
Printable Version  4,963 / 29
Floor1 lgaint Posted 2006-10-25 04:16
初级用户 Posts 7 Credits 22
Because I need to create folders in batches, I learned about the use of FOR, but the task is done, but the study of FOR can't be completed, and I was tripped up by TOKEN~~~

When I type for /? in the command line, the explanation is not understandable. Please ask everyone:

eol=c - refers to the end of a line comment character (just one) ________I understand this, it indicates what is a comment line and is ignored
skip=n - refers to the number of lines to ignore at the beginning of the file. ________This is also clear
delims=xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs. ________This seems to be clear. In the experiment, it seems to refer to starting to work from the specified character
tokens=x,y,m-n - refers to which symbol of each line is passed to the for itself in each iteration. This leads to the format of additional variable names as a range. Specify the m symbol through the nth symbol. If the asterisk is the last character in the string, then the additional variable will be assigned and accept the reserved text of the line after the last symbol is parsed. ________This is completely confused. The more I test, the more confused I am, a complete mess.


tokens=x,y,m-n

I don't understand the functions of x, y, m-n in the usage parameters of tokens. Which expert can explain it?

If you have time, please continue to read below. If you don't have time, just give two pointers. Thank you first.

This is the content of the text file t1.txt that I used for the experiment

In order to hold back the fleeting feelings,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe alternative,
But no one can deny the truest kindness and selflessness of the characters in it.
May everyone accompany them to the destined end.



The following is the test process
E:\>for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1
Have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe alternative,
No one can deny the truest kindness and selflessness of the characters in it.
Everyone accompanies them to the destined end.

E:\>for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1
I
Write
Maybe
But
May

E:\>for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1

E:\>
Floor2 trickster Posted 2007-01-01 05:08
初级用户 Posts 33 Credits 63
tokens=x,y,m-n - refers to which symbol in each line is passed to the for itself in each iteration. This leads to the assignment of additional variable names. The m-n format is a range. The m-th symbol is specified by the nth. If the last character in the symbol string is an asterisk, then the additional variable will be assigned and accept the remaining text of the line after the last symbol is parsed.

When multiple symbols are specified after tokens, the subsequent variables are also expanded, for example in the following example:

for /f "tokens=1-4 delims=- " %a in ('date /t') do echo %a:%b:%c:%d

After running, we get "2006:12:29:Friday". In this for program body statement, %a is used to get the first symbol (2006), %b is used to get the second symbol (12), %c is used to get the third symbol (29), and %d is used to get the fourth symbol (Friday). Because you specified "tokens" to get the first to fourth symbols in the output of the "date /t" command, then %b; %c; %d are introduced. Of course, the subsequent variables are not given randomly; they must be adjacent to the first variable name given. But the order between them in the command executed after do can be reversed, which will only affect the final output result. If "tokens" is not specified in for /f, the default is tokens=1
Floor3 trickster Posted 2007-01-01 05:19
初级用户 Posts 33 Credits 63
This is the content of the text file t1.txt that I use for testing.

Quote:
In order to hold onto that fleeting feeling,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe unconventional,
But no one can deny the truest kindness and selflessness of the characters in it.
May everyone accompany them to the destined end.

for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1

Note: Because eol=为, so all lines starting with "为" will be ignored. Since "delims=a", each line is divided into two parts with "a" as the delimiter, and "tokens=2" means taking the second part.

for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1

The same as above, only finally you take the first part divided by "a"

for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1

The same as above, because each line in the t1.txt file has only one "a", so actually each line is only divided into two parts, and "tokens=3" to take the third part, of course, there will be no display.
Floor4 rochan Posted 2007-01-04 04:09
初级用户 Posts 14 Credits 40
The letter "a" of the thread starter can be regarded as a separator (the default separators are space or tab). And that "tokens=" refers to the number of columns... 1 is the first column; 2 is the second column; 3 is the third column... This is much easier to understand...
Floor5 rochan Posted 2007-01-04 04:15
初级用户 Posts 14 Credits 40

In order to hold onto those fleeting feelings,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe unusual,
But no one can deny the truest kindness and selflessness of the characters in it.
I hope everyone can accompany them to the destined end.


The building owner's text can be形象成 this with a table..
First column Second column Third column ......

For To hold onto those fleeting feelings,
I Have to pick up the pen,
Write Down the bits and pieces of this forbidden love.
Or Maybe dark, maybe cruel, maybe unusual,
But No one can deny the truest kindness and selflessness of the characters in it.


Several columns are all ...The third column is added according to the command you wrote.

Originally, the third column was empty..

After reading, I hope you understand ..
Wish Everyone can accompany them to the destined end.
Floor6 Climbing Posted 2007-01-05 23:01
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
First of all, you need to understand that the main function of for /f is to extract specific strings from text lines. Therefore, each loop of it processes one line of text. Since you need to extract specific strings from this line of text, you naturally need to separate this line of text. The result of the separation is to divide this line of text into several fields. For example, the following line of text:

Dog*Cat*Monkey*Donkey

Obviously, if we use the * sign as the delimiter, then this line will be divided into the following four fields:

Dog Cat Monkey Donkey

OK, now we need to take out one or all fields for processing. Then in the for /f statement, the fields are represented by tokens.

For example, we write like this: for /f "tokens=1,2,3,4 delims=*" %i in ('...') do ....

Taking the above line of text as an example: 1 in tokens means Dog, 2 means Cat, and so on. In the Do statement, %i represents the value of tokens(1), that is, Dog, and %j represents the value of tokens(2), that is, Cat, and so on.

If we write: for /f "tokens=2,4 delims=*" %i in ('...') do ...

Then %i = tokens(2) which is Cat, and %j = tokens(4) which is Donkey.

In addition, the * sign can be used in tokens to represent all remaining fields, for example: tokens=1,*

Then this * sign means all the contents of tokens(2,3,4), that is, %j = Cat Monkey Donkey.

That's roughly all about tokens. There may be inaccuracies in the above description, and it is subject to practice.
Floor7 bigcarp Posted 2007-01-15 05:00
初级用户 Posts 6 Credits 21
Floor8 oilio Posted 2007-01-16 09:53
高级用户 Posts 303 Credits 641
Well, after reading, it has deepened some impressions. for needs to be studied well.
Floor9 tyrbl Posted 2007-01-22 11:56
初级用户 Posts 9 Credits 22
Floor10 hangyug Posted 2007-01-22 22:42
初级用户 Posts 43 Credits 99
Originally, I also didn't understand. After reading the explanation on floor 6, I suddenly became enlightened. No need to say anything, I'll go back and continue studying...
Floor11 kevinye Posted 2007-03-09 08:31
新手上路 Posts 4 Credits 10
That's pretty good, finally got it.
Floor12 nzisisco Posted 2007-03-10 16:34
初级用户 Posts 47 Credits 107
What the 6th floor said is great. I've understood a lot... and I'm full of confidence again.
Floor13 zhxy9804 Posted 2007-03-11 12:16
中级用户 Posts 97 Credits 208
Give him some points. 6th floor. Just saying it's good is not enough. Take some practical actions!
Floor14 hy433124shc Posted 2007-03-12 08:51
中级用户 Posts 75 Credits 201
The explanation on the 6th floor is easy to understand!
It's said clearly and explicitly!
I'll add 2 points!
Floor15 tianlijian Posted 2007-03-13 11:58
初级用户 Posts 45 Credits 120
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023