Solved Batch file to delete specific words in text

March 28, 2016 at 01:57:11
Specs: Windows 7
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!!!


See More: Batch file to delete specific words in text

Report •

✔ Best Answer
March 29, 2016 at 06:53:01
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



#1
March 28, 2016 at 03:25:30
Here your homewotk, but remember in real life you are alone in facing problems.

@echo off > "myfile.new"
for /F "tokens=1*" %%i in ('type "myfile.txt"') do (echo.%%j) >> "myfile.new"
move "myfile.new" "myfile.txt"


Report •

#2
March 28, 2016 at 15:17:41
In other words study the code you have been given carefully and try to understand exactly what each part is doing. If necessary look things up in Google or come back here for explanations.

Always pop back and let us know the outcome - thanks


Report •

#3
March 28, 2016 at 23:00:10
Example, I have a text file and the contents are:
Ramu is a boy.
He is a ramu.
ramu was a driver.
that car driver is ramu.

1). How do I write a batch file that will remove word "ramu" but it will not remove whole line.
Output should be:
is a boy.
He is a .
was a driver.
that car driver is .

2). How do I write a batch file that will replace word "ramu" as "balu".
Output should be:
balu is a boy.
He is a balu.
balu was a driver.
that car driver is balu.


Report •

Related Solutions

#4
March 28, 2016 at 23:05:16
Thankyou IVO & DEREK for your suggestions and please provide answer for above question.
Thankyou.:-)

Report •

#5
March 29, 2016 at 06:53:01
✔ Best Answer
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


Report •

Ask Question