If specific text found in file, del file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BAT Beginner
Posts: 16
Joined: 29 Jan 2010 17:19

If specific text found in file, del file

#1 Post by BAT Beginner » 23 Feb 2010 20:54

I need a code that would detect a specific text in a file, and if that text is found, it be deleted.
Would anyone happen to know such a code?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: If specific text found in file, del file

#2 Post by !k » 24 Feb 2010 04:17

find /i "a specific text" file.txt && del file.txt /q
or
findstr /r /c:"[Aa] [Ss]pecific [Tt]ext" file.txt && del file.txt /q
findstr can use regular expressions

BAT Beginner
Posts: 16
Joined: 29 Jan 2010 17:19

Re: If specific text found in file, del file

#3 Post by BAT Beginner » 24 Feb 2010 16:23

could you do *.* instead of file.txt? would that work?I'ma go find out.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: If specific text found in file, del file

#4 Post by !k » 25 Feb 2010 02:11

Code: Select all

for /f "delims=" %%f in ('findstr /m /c:"a specific text" "d:\path\*.txt"') do del "%%~ff"

Sure to use the path or extension, because this code also contains a "a specific text" and the batch file will erase itself

Post Reply