![]() |
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-09-15 12:20 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » [Common Discussion]Some Pitfalls in Reading Text Content with the for Statement |
| Printable Version 5,902 / 17 |
| Floor1 namejm | Posted 2007-01-25 05:33 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
### 1、Ordinary for statements ignore text starting with a semicolon:
Content of test.txt: Test code: 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: General solutions to solve problem 1 and problem 2: A little more complicated one: 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): 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 ] |
|
| Floor2 9527 | Posted 2007-01-25 05:42 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
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........ |
|
| Floor3 lzmyst | Posted 2007-01-25 05:46 |
| 新手上路 Posts 10 Credits 19 | |
|
Almost all tutorials say that delims should be placed at the end. I just found out today that there are also traps.
|
|
| Floor4 pengfei | Posted 2007-01-25 05:51 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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.
|
|
| Floor5 namejm | Posted 2007-01-25 05:54 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by 9527 at 2007-1-24 16:42: 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: 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 ] |
|
| Floor6 vkill | Posted 2007-01-25 08:22 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
"delims= eol=" order cannot be reversed
It's not like that in for /? |
|
| Floor7 namejm | Posted 2007-01-25 08:31 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor8 vkill | Posted 2007-01-25 08:43 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
Originally posted by namejm at 2007-1-25 08:31: Sweat, just realized there's such a big super error in for /?, really dizzy |
|
| Floor9 pengfei | Posted 2007-01-25 09:24 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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. test.txt 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. |
|
| Floor10 hxmupdata | Posted 2007-03-14 22:30 |
| 初级用户 Posts 39 Credits 76 | |
|
It takes half an hour to read a post... It really is a classic post..............................
|
|
| Floor11 wudixin96 | Posted 2007-04-04 01:07 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
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. |
|
| Floor12 ccuu668 | Posted 2007-05-02 15:32 |
| 初级用户 Posts 29 Credits 57 | |
|
It seems that I can only study basic things more. I didn't understand thoroughly.
|
|
| Floor13 luowei14 | Posted 2007-08-15 17:27 |
| 初级用户 Posts 98 Credits 193 | |
|
...Hehe. Microsoft still has this bug... Oh my
|
|
| Floor14 knoppix7 | Posted 2007-08-15 22:04 |
| 银牌会员 Posts 634 Credits 1,287 From cmd.exe | |
|
There are more bugs in set /?...
|
|
| Floor15 dai13910 | Posted 2008-01-11 14:39 |
| 新手上路 Posts 5 Credits 10 | |
|
Well! Many details are worth pondering
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |