Save blank lines in text file when using package for / f

I am trying to find and replace a string in a text file using a script package. I came across this answer , which almost solved my problem, but empty lines were not saved in the output file. I tried this answer too , but the lines start with line numbers [] ... [17]

Any help to extend this answer to save blank lines in the output file would be appreciated. Thanks

    setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

for /f "delims=" %%i in ('type "%textFile%" ^| find /v /n "" ^& break ^> "%textFile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)

The result looks like this: enter image description here

+4
source share
1 answer

You have not studied the answer well enough on your second link - it has a solution that works fine.

, *]= :

@echo off
setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

for /f "delims=" %%i in ('type "%textFile%" ^| find /v /n "" ^& break ^> "%textFile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*]=!"
    if defined line set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)

- append , , . , , MOVE temp.

FINDSTR FIND - .

@echo off
setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

>"%textFile%.new" (
  for /f "delims=" %%i in ('findstr /n "^" "%textFile%"') do (
      set "line=%%i"
      setlocal enabledelayedexpansion
      set "line=!line:*:=!"
      if defined line set "line=!line:%search%=%replace%!"
      echo(!line!
      endlocal
  )
)
move /y "%textFile%.new" "%textFile%" >nul

, . , . - . :

  • =
  • * !
  • replace !
  • , / ", , &, | ..

JREPL.BAT find/replace utility. , . script ( /JScript ), Windows XP, exe .

, / .

call jrepl "<Tool>" "XYZ" /l /f "C:\abc.txt" /o -
+7

Source: https://habr.com/ru/post/1649927/


All Articles