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-06-24 08:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to get multiple values separated by | in a line of a file in a FOR loop? View 4,022 Replies 20
Original Poster Posted 2006-09-27 03:14 ·  中国 浙江 杭州 电信
初级用户
Credits 28
Posts 10
Joined 2006-09-19 13:29
19-year member
UID 63072
Gender Male
Status Offline
There is a text a.txt

435241|Yangxin|District 8
435250|BaiSha|District 8
****

How to get the values separated by "|"?
Floor 2 Posted 2006-09-27 03:20 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
对于 /f "tokens=1,2,3 delims=|" %%i in (t.txt) do (...)
Floor 3 Posted 2006-09-27 04:35 ·  中国 浙江 杭州 电信
初级用户
Credits 28
Posts 10
Joined 2006-09-19 13:29
19-year member
UID 63072
Gender Male
Status Offline
Thank you for your reply.

What does %%i refer to, the first value, the second value, or the third value?
Floor 4 Posted 2006-09-27 04:37 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
%%i The first one
%%j The second one
%%k The third one
我今后在论坛的目标就是做个超级坏人!!!
Floor 5 Posted 2006-09-27 08:05 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
The landlord first, take a look at /?
Floor 6 Posted 2006-09-27 08:47 ·  中国 浙江 杭州 电信
初级用户
Credits 28
Posts 10
Joined 2006-09-19 13:29
19-year member
UID 63072
Gender Male
Status Offline
Originally posted by pip at 2006-9-27 04:37:
%%i first
%%j second
%%k third



Are the variables i, j, k in %%i, %%j, %%k arbitrary? Are they arbitrary? Or are they in a fixed order?

I'm a newbie, asking for help humbly
Floor 7 Posted 2006-09-27 08:58 ·  中国 浙江 杭州 电信
初级用户
Credits 28
Posts 10
Joined 2006-09-19 13:29
19-year member
UID 63072
Gender Male
Status Offline
I wrote it here, but execution always fails:

for /f "tokens=1,2,3 delims=|" %%i in (txip.txt)

do ( echo 'update dzhd_t_txfjxx set v_ip=%%k where c_jgbh=%%i and c_txdm=%%j ;')

The content of txip.txt is:
31001402|07|10.138.9.165
31001402|05|10.238.60.81
31001402|06|10.138.9.164
31002001|05|10.138.9.37
31002001|06|10.138.9.38
31002801|01|10.138.13.10
31000903|01|10.138.20.68
31000903|03|10.138.20.74

The error message is:
E:\ftp>sql.bat
The syntax of the command is incorrect.

E:\ftp>for /f "tokens=1,2,3 delims=|" %i in (txip.txt)
Floor 8 Posted 2006-09-27 09:07 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
%%i, %%j, %%k are an order issue; if the initial variable is defined as %%a, then the sorting will be %%a, %%b, %%c, %%d...; similarly, if the initial variable is %%0, the sorting is %%0, %%1, %%2...

This is related to the base of the characters or strings extracted by the FOR statement. Each time a character or string is extracted, it is assigned to the next variable. And so on.

Of course, there are also quantity limits. If the variables are defined by letters, their number cannot exceed 26 (a-z).

The maximum number of variables defined by Arabic numerals cannot exceed 10 (0-9).
Floor 9 Posted 2006-09-27 10:00 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by pengfei at 2006-9-27 09:07:
There are of course also number limits. If it is a variable defined by letters, their number cannot exceed 26 (a-z).

The statement that there are number limits is correct, but the claim that the number of letter variables cannot exceed 26 is incorrect because the letter variables in the for statement are case-sensitive, so the number of letter variables should not exceed 52.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-09-27 10:10 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Yes, then the assignment rule for 52 variables is that the lowercase is assigned first and then assigned to the uppercase?

The help also mentioned that a line specifies a maximum of 26 characters, and the mention of not being able to use more than 52 at the same time, since I didn't study it deeply, I didn't mention it.

[ Last edited by pengfei on 2006-9-27 at 10:17 ]
Floor 11 Posted 2006-09-27 10:28 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by pengfei at 2006-9-27 10:10:
Yes, then the assignment pattern for 52 variables is to first complete the assignment in lowercase and then assign to uppercase?

The help also mentioned that a line specifies a maximum of 26 characters, and it mentioned that more than 52 cannot be used at the same time, by 䠮..

  This question hasn't been deeply thought about. It is speculated like this: If you start using lowercase letters to define variable names, then the variables after for on the same level should at most only go to z. If it is uppercase, it can at most only go to Z. That is to say, the case of variable names should be consistent from start to finish, and cannot be mixed. Otherwise, there wouldn't be the statement in the help that a line specifies a maximum of 26 characters, and it mentioned that more than 52 cannot be used at the same time. It's just a speculation. Those who understand, please analyze it more deeply.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-09-27 11:08 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by namejm at 2006-9-27 10:28:

  This problem hasn't been deeply thought about. It is speculated like this: if lowercase letters are used to define variable names from the beginning, then the maximum number of variables after for on the same level should be up to z. If it is uppercase...


I think it is like this. The number of letters used as variables in a loop cannot be more than 52. Variable names can only be single letters, case-sensitive. So the number of letter variables that the system can handle normally cannot be more than 52.

If numbers are added, the maximum number of variables that can be used globally is 62, and the number of variables assigned by FOR to extract text lines cannot be more than 26 (a-z) or (A-Z).
Floor 13 Posted 2006-09-27 20:24 ·  中国 浙江 杭州 电信
初级用户
Credits 28
Posts 10
Joined 2006-09-19 13:29
19-year member
UID 63072
Gender Male
Status Offline
Everyone, help me see where I made a mistake.

for /f "tokens=2,3* delims=|" %%i in (txip1.txt)

do ( echo 'update dzhd_t_txfjxx set v_ip=%%k where c_jgbh=%%i and c_txdm=%%j ;') >a.sql

The prompt is "E:\ftp>rem for /f "tokens=1,2,3 delims=|" %0 in (txip1.txt)
The command syntax is incorrect.

E:\ftp>for /f "tokens=2,3* delims=|" %i in (txip1.txt)
Floor 14 Posted 2006-09-27 20:58 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
The parentheses in that line were mistyped. `>a.sql` should be together with ECHO.
Floor 15 Posted 2006-09-28 04:20 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
The code you just wrote. Hehe. Interested to take a look
Function splitfile(strFile)
Const ForReading = 1,ForWriting=2
Dim i, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(strFile, ForReading, False)
Do While theFile.AtEndOfStream <> True
retstring=Split(theFile.ReadLine,"|",-1,1)
For i=0 to UBound(retstring)
wscript.echo retstring(i)
Next
loop
theFile.Close
End Function
知识在于不断积累
Forum Jump: