- @echo off
- setlocal enabledelayedexpansion
- for /r %%i in (*.txt) do (
- set "file=%%i"
- set /a count=1
- cd.>!file!.tmp
- for /f "delims=" %%j in (!file!) do (
- set "line=%%j"
- set "line_bak=!line!"
- echo !count!
- call :intercept
- if defined line (
- if !count! EQU 1 (
- echo !line! 2>nul>>!file!.tmp
- ) else (
- echo !line_bak! 2>nul>>!file!.tmp
- )
- )else (
- echo\>>!file!.tmp
- if !count! EQU 1 set /a count-=1
- )
- set /a count+=1
- )
- move /y !file!.tmp !file!
- )
- pause
- goto :eof
- :intercept
- if "!line:~0,1!"==" " set "line=!line:~1!"&goto intercept
复制代码 代码效果:
1、保存了标题所在行之前的空白行
2、文本中的特殊字符不能处理 |