@echo off :: 普通的 for+findstr 語句會忽略分號開頭的行 :: findstr /n .* 用delims=:后,會忽略行首所有的冒號 :: 還有!、、.等特殊符號需要處理 :: 以下代碼可以準確提取這些敏感字符 :: 解決了 setlocal 最大遞歸層的問題(setlocal 兩兩嵌套處理超過15行內(nèi)容時會帶來此問題) :: 能計算空行 :: code by jm 2006-12-12 thanks 3742668 CMD@XP set num=0 for /f "delims=" %%i in ('findstr /n .* test.txt') do ( set /a num+=1 set "str=%%i" setlocal enabledelayedexpansion echo 第 !num! 行: !str:*:=! endlocal ) pause goto :eof @echo off :: 超過15行內(nèi)容時,會帶來 setlocal 最大遞歸層的問題 :: code by 3742668 2006-12-11 CMD@XP @echo off setlocal enabledelayedexpansion set num= for /f "eol= delims=" %%i in ('findstr .* a.txt') do ( setlocal DISABLEDELAYEDEXPANSION set "tmpstr=%%i" setlocal enabledelayedexpansion set /a num += 1 set "!num!=!tmpstr!" ) for /l %%i in (1,1,%num%) do echo 第%%i行: !%%i! pause exit /b 0 @echo off :: 不能正確提取帶感嘆號的內(nèi)容 :: code by jm 2006-12-11 CMD@XP set num=0 setlocal enabledelayedexpansion for /f "delims=" %%i in ('findstr /n .* test.txt') do ( set /a num+=1 set "str=%%i" echo 第 !num! 行: !str:*:=! ) pause goto :eof @echo off :: 結(jié)果要用引號括起來 :: code by jm 2006-12-11 CMD@XP set num=0 ::setlocal enabledelayedexpansion for /f "delims=" %%i in ('findstr /n .* test.txt') do ( set /a num+=1 set "str=%%i" call :display ) pause goto :eof :display echo 第 %num% 行: "%str:*:=%" goto :eof test.txt的內(nèi)容 :abc ::xy