Find a term and remove it, from many files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
anshuman
Posts: 1
Joined: 18 Sep 2013 04:02

Find a term and remove it, from many files

#1 Post by anshuman » 18 Sep 2013 04:07

Hi, My requirement is i have to find a word 'Grants' In many text files and delete the entire line containing 'Grants'. But i need to do In many text files at a time. How can this be achieved

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find a term and remove it, from many files

#2 Post by foxidrive » 18 Sep 2013 04:18

Give this a whirl:

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.txt /b /a-d ')  do find /v "Grants" < "%%a"  >"%%~na-new%%~xa"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Find a term and remove it, from many files

#3 Post by dbenham » 18 Sep 2013 07:02

Similar to foxidrive's solution, except uses FINDSTR with regex and word boundry anchors. Also effectively edits the files instead of creating new modified copies.

Code: Select all

@echo off
for /f "delims=" %%F in (' dir *.txt /b /a-d ')  do (
  findstr /v "\<Grants\>" "%%F" >"%%F.new"
  move /y "%%F.new" "%%F" >nul
)

Add the /I switch if you want the search to be case insensitive.


Dave Benham

Post Reply