若要考虑特殊字符,可以使用下面的演示代码(但是就无法避免在每行的首位都要加引号):
@echo off
for /f "delims=" %%i in ('findstr /n .* 1.txt') do call :intercept "%%i☆"
pause
goto :eof
:intercept
set str=%1
set "str=%str:~1,-1%"
set "str=%str:*:=%"
if not "%str:~0,4%"=="aaa " echo "%str:~0,-1%"
goto :eof
若无须考虑连接符号,则可以使用如下演示代码:
@echo off
for /f "delims=" %%i in ('findstr /n .* 1.txt') do call :intercept "%%i☆"
pause
goto :eof
:intercept
set str=%1
set str=%str:~1,-1%
set str=%str:*:=%
if not "%str:~0,4%"=="aaa " echo.%str:~0,-1%
goto :eof
以上代码不直接用 delims=: 是为了防止行首的冒号被抛弃;用 findstr /n .* 的格式是为了不忽略以分号开头的行;给 %%i 加引号是为了对付空格,在 %%i 后加星号,是为了对付空行;第二段代码用 echo 后加了一个点号,也是为了对付空行。总之,用 for 语句处理文本要注意的事项比较多,可视具体文本增删语句的某些部分。