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-07-07 14:42
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use % as a delimiter in a for statement (solved) View 3,616 Replies 21
Original Poster Posted 2006-12-16 04:48 ·  中国 江苏 南京 中移铁通
初级用户
Credits 26
Posts 11
Joined 2006-12-15 10:48
19-year member
UID 73609
Gender Male
Status Offline
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a>>2222.txt
pause
The content in 222.txt is as follows:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
There was an execution error, I don't understand...
At first, I just wanted to remove the % in the file, but the for execution didn't work
I'm sorry, it's because I didn't write my content in enough detail to cause disputes among everyone, , just to explain the problem, I didn't write it in too much detail, sorry
[ Last edited by jinrich on 2006-12-15 at 11:16 PM ]
Just does it;I lov bat
Floor 2 Posted 2006-12-16 05:14 ·  中国 江苏 南京 中移铁通
初级用户
Credits 26
Posts 11
Joined 2006-12-15 10:48
19-year member
UID 73609
Gender Male
Status Offline
Could the percent sign just not be used as a separator? I also can't make it work in Unix.
Just does it;I lov bat
Floor 3 Posted 2006-12-16 05:26 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
One % doesn't work, two % are enough.
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
Floor 4 Posted 2006-12-16 05:39 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The code is incorrect unless you don't want the characters after the first %.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2006-12-16 05:52 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
@echo off
for /f "tokens=1-6 delims=%%" %%a in (222.txt) do echo %%a%%b%%c%%d%%e%%f>>2222.txt
pause
bat c c++
Floor 6 Posted 2006-12-16 05:54 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
Originally posted by ccwan at 2006-12-16 05:39:
The code is incorrect unless you don't want the characters after the first %.

You didn't test it, don't make unfounded conclusions. Who said it's incorrect? I want the following, and this is also correct.
The content of 222.txt is:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%

@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
Displays: 0|60|0|100.00

@echo off
for /f "tokens=1,2 delims=%% " %%a in (222.txt) do echo %%a %%b
pause
Displays: 0|60|0|100.00 |0.00

@echo off
for /f "tokens=1,2,3,4,5,6 delims=%% " %%a in (222.txt) do echo %%a %%b %%c %%d %%e %%f
pause
Displays:
0|60|0|100.00 |0.00 |17.32 |14.57 |55.64 |8.14|18.14|0.02|16.35|16.19|83.51|26
Floor 7 Posted 2006-12-16 05:55 ·  中国 江苏 南京 中移铁通
初级用户
Credits 26
Posts 11
Joined 2006-12-15 10:48
19-year member
UID 73609
Gender Male
Status Offline
Thanks, expert. It's done.
Just does it;I lov bat
Floor 8 Posted 2006-12-16 06:01 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I don't talk nonsense without testing.
Please try the following code:
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 9 Posted 2006-12-16 06:41 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Just to interject, the code above is not rigorous enough. If there are spaces or horizontal tabs in 222.txt, the for loop will only get the characters before the space. It is recommended to add "delims=".
Floor 10 Posted 2006-12-16 06:44 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Thank you, Brother lxmxn. At that time, I was only focused on achieving the effect and indeed didn't consider it comprehensively. Thanks for the reminder!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2006-12-16 06:44 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
The result of my test is:
0|60|0|100.00|0.00|17.32|14.57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
There is nothing wrong. I don't know where the code you mentioned is wrong. Please clarify where the place you said would have an error is.
Floor 12 Posted 2006-12-16 06:47 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Re tianzizhi :

  Make the following content as the content of 222.txt, and try again?

0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%

Floor 13 Posted 2006-12-16 06:59 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
Originally posted by lxmxn at 2006-12-16 06:47:

  Re tianzizhi :

  Take the following content as the content of 222.txt and try again?

0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
...

lxmxn, you may have misunderstood. What I'm talking about is ccwan.
Said that my answer on the third floor would be wrong in some places,
『Floor 3』:
One % is not okay, use two %s.
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
『Floor 4』:
Unless you don't want the characters after the first %, the code is incorrect.

I don't know what he means by the code being incorrect, and then he posted the code you saw. I tested it again, and there's nothing wrong.
For the content in 222.txt that you mentioned, there are spaces, so you need to add delims=.
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
The test result is:
0|60|0|100.00| 0.00|17.32|14 .57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
There's nothing wrong either. Still, please let ccwan clarify what he means by the mistake. I want to know.
Floor 14 Posted 2006-12-16 07:06 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe,
The content of 222.txt is:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%

@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
Displays: 0|60|0|100.00


The result is not what the original poster wanted, of course it's incorrect. The situation at that time was just like that!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 15 Posted 2006-12-16 07:07 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Oh, it was a misunderstanding. Sorry about that.
Forum Jump: