China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 19:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Seeking help on the use of the tokens parameter in the for command? View 4,956 Replies 29
Original Poster Posted 2006-10-25 04:16 ·  中国 广东 广州 白云区 电信
初级用户
Credits 22
Posts 7
Joined 2006-10-25 03:39
19-year member
UID 68145
Gender Male
Status Offline
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:\>
Floor 2 Posted 2007-01-01 05:08 ·  中国 山东 青岛 中移铁通
初级用户
Credits 63
Posts 33
Joined 2006-12-04 05:49
19-year member
UID 72514
Gender Male
Status Offline
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
Floor 3 Posted 2007-01-01 05:19 ·  中国 山东 青岛 中移铁通
初级用户
Credits 63
Posts 33
Joined 2006-12-04 05:49
19-year member
UID 72514
Gender Male
Status Offline
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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
hy433124shc +1 2007-03-14 09:40
Floor 4 Posted 2007-01-04 04:09 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 14
Joined 2006-12-27 11:38
19-year member
UID 74713
Gender Male
Status Offline
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...
Floor 5 Posted 2007-01-04 04:15 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 14
Joined 2006-12-27 11:38
19-year member
UID 74713
Gender Male
Status Offline

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.
Recent Ratings for This Post ( 7 in total) Click for details
RaterScoreTime
hy433124shc +2 2007-03-14 09:39
logictianjin +1 2007-04-07 01:29
caucfeiyu +1 2007-04-12 16:57
gugewang +1 2008-01-11 20:30
h2thesun +1 2008-03-03 23:47
dosz +1 2009-03-07 11:42
slw218856 +1 2009-11-11 22:48
Floor 6 Posted 2007-01-05 23:01 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
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.
Recent Ratings for This Post ( 8 in total) Click for details
RaterScoreTime
zhxy9804 +2 2007-03-11 12:16
hy433124shc +2 2007-03-12 08:48
logictianjin +1 2007-04-07 01:29
caucfeiyu +1 2007-04-12 17:01
z310394543 +1 2007-10-29 22:48
joyoustar +2 2008-01-07 11:42
regvip2008 +1 2008-01-14 17:44
dosz +2 2009-03-07 11:43
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 7 Posted 2007-01-15 05:00 ·  中国 广东 广州 白云区 电信
初级用户
Credits 21
Posts 6
Joined 2006-12-17 00:10
19-year member
UID 73746
Gender Male
Status Offline
Floor 8 Posted 2007-01-16 09:53 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Well, after reading, it has deepened some impressions. for needs to be studied well.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 9 Posted 2007-01-22 11:56 ·  中国 山东 菏泽 联通
初级用户
Credits 22
Posts 9
Joined 2006-07-03 13:14
20-year member
UID 57930
Status Offline
Floor 10 Posted 2007-01-22 22:42 ·  中国 广东 广州 白云区 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
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...
Floor 11 Posted 2007-03-09 08:31 ·  中国 江苏 无锡 电信
新手上路
Credits 10
Posts 4
Joined 2007-03-07 10:23
19-year member
UID 80993
Gender Male
Status Offline
That's pretty good, finally got it.
Floor 12 Posted 2007-03-10 16:34 ·  新西兰
初级用户
Credits 107
Posts 47
Joined 2007-03-03 03:12
19-year member
UID 80618
Gender Male
Status Offline
What the 6th floor said is great. I've understood a lot... and I'm full of confidence again.
Floor 13 Posted 2007-03-11 12:16 ·  中国 山东 临沂 电信
中级用户
★★
Credits 208
Posts 97
Joined 2006-12-28 22:08
19-year member
UID 74845
Gender Male
Status Offline
Give him some points. 6th floor. Just saying it's good is not enough. Take some practical actions!
Floor 14 Posted 2007-03-12 08:51 ·  中国 山东 潍坊 电信
中级用户
★★
Credits 201
Posts 75
Joined 2007-02-24 00:26
19-year member
UID 80034
Gender Male
Status Offline
The explanation on the 6th floor is easy to understand!
It's said clearly and explicitly!
I'll add 2 points!
Floor 15 Posted 2007-03-13 11:58 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
Forum Jump: