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-11 17:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Common Discussion]Some Pitfalls in Reading Text Content with the for Statement View 5,386 Replies 17
Original Poster Posted 2007-01-25 05:33 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
### 1、Ordinary for statements ignore text starting with a semicolon:

Content of test.txt:

;abc
ai
::te
,te st
test


Test code:

@echo off
for /f "delims=" %%i in (test.txt) do echo %%i
pause


This trap does not exist in the statements of type test.txt, more test.txt, and findstr .* test.txt without using for.

### 2、When using findstr /n .* test.txt with tokens=1* delims=: in the for statement, all colons at the beginning of the line will be filtered:

test.txt is the same as 1;

Test code:

@echo off
for /f "tokens=1* delims=:" %%i in ('findstr /n .* test.txt') do echo %%j
pause


General solutions to solve problem 1 and problem 2:

A little more complicated one:

@echo off
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set "str=%%i"
call set "str=%%str:*:=%%"
call echo "%%str%%"
)
pause


The most concise one (After testing, this solution is not universal, because "delims= eol=" will filter out the content with quotes at the beginning of the line. It seems that it is really driving the tiger away from the front door and letting the wolf in from the back door. Quotes and semicolons cannot be taken into account):

@echo off
for /f "delims= eol=" %%i in (test.txt) do echo %%i
pause


In the concise solution, the order of "delims= eol=" cannot be reversed, otherwise deviations will occur.

I don't know what other traps the powerful for will have that will make us jump in without knowing, please everyone collect them so that we can take fewer detours when writing code in the future.

[ Last edited by namejm on 2007-5-17 at 09:17 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +15 2007-01-25 05:39
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2007-01-25 05:42 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
First of all, what I want to say is that the FOR statement ignores lines starting with "semicolons" by default, not colons.

Secondly, in the command "for /f "eol= delims=" %%a in (xxx) do command", no lines starting with any symbol are ignored, that is to say, all contents will be displayed........
我今后在论坛的目标就是做个超级坏人!!!
Floor 3 Posted 2007-01-25 05:46 ·  中国 广东 中山 电信
新手上路
Credits 19
Posts 10
Joined 2005-12-13 01:56
20-year member
UID 47019
Status Offline
Almost all tutorials say that delims should be placed at the end. I just found out today that there are also traps.
Floor 4 Posted 2007-01-25 05:51 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Another thing to note, it can't be called a trap, any program naturally can't take all aspects into account, this is where we need to pay more attention in our daily lives.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 5 Posted 2007-01-25 05:54 ·  中国 广东 电信
荣誉版主
★★★★
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 9527 at 2007-1-24 16:42:
First of all, I want to say that the FOR statement by default ignores lines starting with a semicolon, not a colon.

  Hehe, thanks for the reminder, the typo has been corrected.

Secondly, in the command "for /f "eol= delims=" %%a in (xxx) do command", no lines starting with any symbol will be ignored, that is, all contents will be displayed...

  This solution will ignore lines starting with spaces. Brother, just test it and you'll know.
Originally posted by pengfei at 2007-1-24 16:51:
Another point worth noting, it can't be said to be a trap. Any program naturally can't take into account all aspects, and this is where we need to pay more attention in our daily lives.

  These points worth noting are not mentioned anywhere in Microsoft's user manual. Whether it's a bug it deliberately ignores, a problem too small to mention, or it hasn't noticed, it's impossible to know. But from the user's perspective, there are bugs with no formal explanation, which are like hidden traps waiting for others to fall into ^_^

[ Last edited by namejm on 2007-1-24 at 05:59 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2007-01-25 08:22 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
"delims= eol=" order cannot be reversed

It's not like that in for /?
Floor 7 Posted 2007-01-25 08:31 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, the /? in for also says: eol=c - refers to the end of a line comment character (just one). I'm really angry - heh, actually it filters out lines starting with c. I don't know which guy did the translation, his English is too bad.

Let's get back to the main point. What I said about the order not being reversible doesn't refer to all situations, but rather that in order to handle line contents starting with semicolons and colons simultaneously, this order can't be reversed.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2007-01-25 08:43 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by namejm at 2007-1-25 08:31:
  Haha, the /? for for also says: eol=c - refers to the end of a line comment character (just one). I'm really frustrated. - Hehe, actually it filters out lines starting with c. I don't know which guy did the translation, E...

Sweat, just realized there's such a big super error in for /?, really dizzy
Floor 9 Posted 2007-01-25 09:24 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
for /f "eol= delims=" %%i in (test.txt) do echo %%i "eol= delims=" cannot be reversed, and no errors were found in local testing.

It seems that the comments in for /? are really a bit problematic.
eol=c - refers to the end of a line comment character (just one)
delims=xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs
default delimiter set.

There is ambiguity in our understanding of the above description. eol filters lines whose first character matches. Just like the default delimiter of delims is space or TAB. And the default first character of eol to filter is the ; character, but for /? does not explain.

Brother namejm's explanation gives the solution code, for /f "delims= eol=" %%i in (test.txt) do echo %%i Its function is the same as "delims=" to cancel the default space as a delimiter, and "eol=" to cancel the ; as a delimiter.

The following code is to filter lines whose first character is a colon, and cancel the default semicolon delimiter.


@echo off
for /f "delims= eol=: " %%i in (test.txt) do echo %%i
pause



test.txt

abc
:123
4:56
;
;def
::
dok:cto:::


The eol delimiter can only be one. The above code deliberately has two delimiters in elo, but only filters lines whose first character is :, and blank lines are not filtered.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 10 Posted 2007-03-14 22:30 ·  中国 江苏 南京 电信
初级用户
Credits 76
Posts 39
Joined 2007-03-09 06:54
19-year member
UID 81169
Gender Male
Status Offline
It takes half an hour to read a post... It really is a classic post..............................
狐狸喜欢狡猾。。
Floor 11 Posted 2007-04-04 01:07 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
The following code filters lines starting with a colon and cancels the default space and tab delimiters.
CODE:
--------------------------------------------------------------------------------

@echo off
for /f "delims= eol=: " %%i in (test.txt) do echo %%i
pause

It should be
The following code filters lines starting with a colon and cancels the default space and tab delimiters.
Floor 12 Posted 2007-05-02 15:32 ·  中国 云南 昆明 电信
初级用户
Credits 57
Posts 29
Joined 2006-12-27 09:36
19-year member
UID 74694
Gender Male
Status Offline
It seems that I can only study basic things more. I didn't understand thoroughly.
Floor 13 Posted 2007-08-15 17:27 ·  中国 广东 佛山 顺德区 电信
初级用户
★★
Credits 193
Posts 98
Joined 2007-01-17 11:56
19-year member
UID 76803
Gender Male
Status Offline
...Hehe. Microsoft still has this bug... Oh my
这家伙很聪明 什么都没留下
Floor 14 Posted 2007-08-15 22:04 ·  中国 北京 海淀区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
There are more bugs in set /?...
Floor 15 Posted 2008-01-11 14:39 ·  中国 北京 联通
新手上路
Credits 10
Posts 5
Joined 2007-12-27 11:33
18-year member
UID 106916
Gender Male
Status Offline
Well! Many details are worth pondering
Forum Jump: