What is the bug that uses the for command in batch processing
This article mainly introduces the batch processing of the use of for command what bug, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
1. About colons: when a line begins with:, use for / f "tokens=1* delims=:"% I in ('findstr / n. * test.txt') do echo.%%j to put:
Filter out
2. About the semicolon: findstr. * test.txt can fully display the lines starting with the semicolon, but it cannot be found when used in the for statement, and the parameter / n must be added.
It seems that the for statement ignores lines that begin with a semicolon as comments, as if it is not findstr's fault, while type, more and findstr that are not included in the for statement are all OK
Solutions to problems 1 and 2 are:
Slightly more complicated:
@ echo off
:: blank lines are not ignored
For / f "delims="% I in ('findstr / n. * test.txt') do (
Set "str=%%i"
Call set "str=%%str:*:=%%"
Call echo "str%%"
)
Pause
The simplest:
@ echo off
:: this code ignores blank lines
For / f "delims= eol=" I in (test.txt) do echo I
Pause
3. In the for extension,%% ~ si indicates that the path of the extension contains only short file names, but in the following testing process, it is found that this is not the case:
Test environment:
D:\ abcdefg hijk\ abcd efgh.txt
D:\ abcdefg hijk\ te st.txt
Test the code:
@ echo off
For / f "delims="% I in ('dir / a / b * .txt') do echo% ~ si
Pause
Test results:
D:\ ABCDEF~1\ ABCDEF~1.TXT
D:\ ABCDEF~1\ TEST~1.TXTtxt
Did you see the last record of the test results? Unexpectedly extended to TEST~1.TXTtxt!
Thank you for reading this article carefully. I hope the article "what is the bug of using for command in batch processing" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!