这个问题在这里已有答案:
我有一个改编自@MC ND的批处理,它搜索一个字符串并将其替换为给定的文件。
除了删除我的初始文件中的空行外,它运行良好。
@echo off
setlocal enableextensions disabledelayedexpansion
set "search=To_be_replaced"
set "replace=Well_Replaced"
set "File=TEST.txt"
for /f "delims=" %%i in ('type "%File%" ^& break ^> "%File%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%File%" echo(!line:%search%=%replace%!
endlocal
)
输入文件是:
A
To_be_replaced
B
我希望输出为:
A
Well_Replaced
B
实际输出是:
A
Well_Replaced
B
如何设法不删除空白行?
以下是基于迄今为止的评论的示例:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "search=To_be_replaced"
Set "replace=Well_Replaced"
Set "File=TEST.txt"
For /F "Tokens=1*Delims=]" %%A In ('Find /V /N ""^<"%File%"^&Break^>"%File%"'
)Do (Set "line=%%B"
SetLocal EnableDelayedExpansion
(If Not "%%B"=="" (Echo(!line:%search%=%replace%!)Else Echo()>>"%File%"
EndLocal)