修改当前文件夹中的txt文件
@echo off
set "行首=$$$$"
set "行尾=####"
for %%a in (*.txt) do (
set ".="
(for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%%a"') do (
if defined . (
echo,%%b
) else (
if not "%%b"=="" (
set .=.
echo %行首%%%b%行尾%
)
)
))>$
move $ "%%a"
)
@echo off&setlocal enabledelayedexpansion
set n=1
for /f "delims=:" %%a in ('findstr /v /r /c:"^[ ]*$" "test.txt"') do (
"set stline=%%a"
goto :begin
)
:begin
(for /f "tokens=1* delims=:" %%a in ('findstr .* "test.txt"') do (
if !n! lss %stline% echo.%%b
if !n! equ %stline% (
echo.xxx%%byyy
echo.
more +%stline% "test.txt"
goto :end
)
set /a n+=1
))>$
:end
move $ "test.txt"
pause
所有出现文件名test.txt的地方,可以修改为实际文本文件的文件名
if !n! equ %stline% (
echo.xxx%%byyy ::此处表示行首添加xxx,行尾添加yyy,xxx,yyy可以换成其它字符
echo.
more +%stline% "test.txt"
goto :end
)
@echo off
cd /d %~dp0
set file=xxx.txt
set TA=$$$$
set TB=####
cd.>"%file%_tmp.txt"
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%file%"') do (
set str=%%b
if "!str!"=="" echo.!str!>>"%file%_tmp.txt"
if not !n!==1 (
if not "!str!"=="" (
set txt=%TA%!str!%TB%
echo !txt!>>"%file%_tmp.txt"
set n=1
)
) else (
echo.!str!>>"%file%_tmp.txt"
)
)
start "" "%file%_tmp.txt"
exit
如果空行中包含空格,这个批处理将会认为是有字符的行。