I have a batch adapted from @MC ND, that search for a string and replace it in a given file.
It works well except that it deletes blank lines in my initial file.
@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
)
The input file is :
A
To_be_replaced
B
I expect the output to be:
A
Well_Replaced
B
The actual output is:
A
Well_Replaced
B
How can I manage to not delete blank lines ?
Here's an example based on the comments thus far:
@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)