computing
  • 4

Solved Batch File To Delete Specific Words In Text

  • 4

Example, I have a text file and the contents are:
Dog is a pet.
Dog has four legs.
Dog has tail.
Dog has two eyes.

How do I write a batch file that will remove word “Dog” but it will not remove whole line.
Out put should be:
is a pet.
has four legs.
has tail.
has two eyes.

Thanks!!!

Share

1 Answer

  1. Here the script for all seasons, it deletes or replaces a word everywhere it is located on the line.
    The script requires a good knoledge of batch theory, so try
    – for /?
    – set /?
    I am not a script pusher, so as Derek suggested:

    STUDY STUDY STUDY STUDY … don’t waste your time.

    @echo off & setlocal EnableDelayedExpansion
    
    set /P _file=Enter filename^>
    set /P _word1=Enter the word to be canceled or replaced^>
    set /P _word2=Type the word for replacement or press Enter^>
    
    type nul > "%_file%.tmp"
    for /F "delims=" %%i in ('type "%_file%"') do (
     set row=%%i
     set row=!row:%_word1%=%_word2%!
      echo.!row!
    ) >> "%_file%.tmp"
    move "%_file%.tmp" "%_file%"
    

    message edited by IVO

    • 0