find and delete a word in a text by batch file

2019-09-08 05:54发布

问题:

Hi all I want to find and delete some word by a batch file i tried this:

@Echo OFF
REM Set These Variables
SET "InFile=Unloaded.txt"
SET "OutFile=NowLoaded.txt"
SET "Replace=%r%"
SET "ReplaceWith=  "

REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"

REM Create The OutFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
  FOR /L %%a IN (1 1 0) DO SET /p "="
  FOR /L %%A IN (1 1 %Till%) DO (
    SET "line="
    SET /P "line="
    IF "!line!x" == "x" ( Echo.
   ) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
  )
)>>"%OutFile%"

ENDLOCAL

but in out put instead of delete that says Echo Is Off. please help me thanks.

回答1:

Take a look at replacer.bat.You can use it by like this to delete a word in text file:

call replacer.bat text.txt "word" ""

More powerful solutions are JREPL.bat and FindRepl.bat.