### 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 ]
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
| Rater | Score | Time |
|---|---|---|
| ccwan | +15 | 2007-01-25 05:39 |
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
考虑问题复杂化,解决问题简洁化。
