![]() |
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-25 23:14 |
47,812 topics / 349,910 posts / today 0 new / 48,264 members |
| DOS批处理 & 脚本技术(批处理室) » How to get multiple values separated by | in a line of a file in a FOR loop? |
| Printable Version 4,243 / 20 |
| Floor1 tomorrower | Posted 2006-09-27 03:14 |
| 初级用户 Posts 10 Credits 28 | |
|
There is a text a.txt
435241|Yangxin|District 8 435250|BaiSha|District 8 **** How to get the values separated by "|"? |
|
| Floor2 NaturalJ0 | Posted 2006-09-27 03:20 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
对于 /f "tokens=1,2,3 delims=|" %%i in (t.txt) do (...)
|
|
| Floor3 tomorrower | Posted 2006-09-27 04:35 |
| 初级用户 Posts 10 Credits 28 | |
|
Thank you for your reply.
What does %%i refer to, the first value, the second value, or the third value? |
|
| Floor4 9527 | Posted 2006-09-27 04:37 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
%%i The first one
%%j The second one %%k The third one |
|
| Floor5 vkill | Posted 2006-09-27 08:05 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
The landlord first, take a look at /?
|
|
| Floor6 tomorrower | Posted 2006-09-27 08:47 |
| 初级用户 Posts 10 Credits 28 | |
Originally posted by pip at 2006-9-27 04:37: 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 |
|
| Floor7 tomorrower | Posted 2006-09-27 08:58 |
| 初级用户 Posts 10 Credits 28 | |
|
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) |
|
| Floor8 pengfei | Posted 2006-09-27 09:07 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
%%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). |
|
| Floor9 namejm | Posted 2006-09-27 10:00 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by pengfei at 2006-9-27 09:07: 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. |
|
| Floor10 pengfei | Posted 2006-09-27 10:10 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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 ] |
|
| Floor11 namejm | Posted 2006-09-27 10:28 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by pengfei at 2006-9-27 10:10: 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. |
|
| Floor12 pengfei | Posted 2006-09-27 11:08 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
Originally posted by namejm at 2006-9-27 10:28: 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). |
|
| Floor13 tomorrower | Posted 2006-09-27 20:24 |
| 初级用户 Posts 10 Credits 28 | |
|
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) |
|
| Floor14 NaturalJ0 | Posted 2006-09-27 20:58 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
The parentheses in that line were mistyped. `>a.sql` should be together with ECHO.
|
|
| Floor15 piziliu2004 | Posted 2006-09-28 04:20 |
| 中级用户 Posts 139 Credits 321 | |
|
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 |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |