中国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-25 23:08
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » How to use % as a delimiter in a for statement (solved)
Printable Version  3,881 / 21
Floor1 jinrich Posted 2006-12-16 04:48
初级用户 Posts 11 Credits 26
@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 ]
Floor2 jinrich Posted 2006-12-16 05:14
初级用户 Posts 11 Credits 26
Could the percent sign just not be used as a separator? I also can't make it work in Unix.
Floor3 tianzizhi Posted 2006-12-16 05:26
高级用户 Posts 214 Credits 623
One % doesn't work, two % are enough.
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
Floor4 ccwan Posted 2006-12-16 05:39
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
The code is incorrect unless you don't want the characters after the first %.
Floor5 a9319751 Posted 2006-12-16 05:52
中级用户 Posts 170 Credits 439
@echo off
for /f "tokens=1-6 delims=%%" %%a in (222.txt) do echo %%a%%b%%c%%d%%e%%f>>2222.txt
pause
Floor6 tianzizhi Posted 2006-12-16 05:54
高级用户 Posts 214 Credits 623
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
Floor7 jinrich Posted 2006-12-16 05:55
初级用户 Posts 11 Credits 26
Thanks, expert. It's done.
Floor8 ccwan Posted 2006-12-16 06:01
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
I don't talk nonsense without testing.
Please try the following code:
Floor9 lxmxn Posted 2006-12-16 06:41
版主 Posts 4,938 Credits 11,386

  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=".
Floor10 ccwan Posted 2006-12-16 06:44
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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!
Floor11 tianzizhi Posted 2006-12-16 06:44
高级用户 Posts 214 Credits 623
@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.
Floor12 lxmxn Posted 2006-12-16 06:47
版主 Posts 4,938 Credits 11,386

  Re tianzizhi :

  Make the following content as the content of 222.txt, and try again?
Floor13 tianzizhi Posted 2006-12-16 06:59
高级用户 Posts 214 Credits 623
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?
Floor14 ccwan Posted 2006-12-16 07:06
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Hehe,


The result is not what the original poster wanted, of course it's incorrect. The situation at that time was just like that!
Floor15 lxmxn Posted 2006-12-16 07:07
版主 Posts 4,938 Credits 11,386

  Oh, it was a misunderstanding. Sorry about that.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023