Avatar of E=mc2
E=mc2
Flag for Canada asked on

Delete files that contain a specific string

I want to delete files that contain a specific string..

if exist *.txt findstr /c:"AB*123*" *.txt ... then delete the files

I need to know how to complete this.
Microsoft Legacy OSMicrosoft DOSWindows OS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Steven Harris

I'm not sure what exactly you are trying to accomplish.  Try something like:

del *yourstring*.*

Open in new window


If it is the "AB*123*" you are searching for in a txt file, then:

del AB*123*.txt

Open in new window


If you want to do this recursively, use /s:

del /s *yourstring*.*

Open in new window

E=mc2

ASKER
I want to delete the files that contain the string AB*123* within them, not within the name of the file
Member_2_276102

Are you needing to learn how to pipe output from findstr into a del command? That should be just about all that's need if I understand your question.

Tom
Your help has saved me hundreds of hours of internet surfing.
fblack61
Steven Harris

You can run from console if you you are in the current directory:

for /f "eol=: delims=" %F in ('findstr /m AB*123* *.txt') do del "%F"

Open in new window


To run within batch file, change %F to %%F.


Make sure to run this as a test first to confirm the requirements are met.
E=mc2

ASKER
for /f "eol=: delims=" %F in ('findstr /m AB*123* *.txt') do del "%F"

The above does not work.  I think it has to do with the fact that AB*123* is the string to look for, I don't think it's the correct syntax.   If you look for *123* then it will work.

I need for the command to look for AB*123* within the file itself, and once it finds this string, then to delete the file that it found the string inside.
Steven Harris

OK, let's step back and look at it again...

Try running the following from directory where the files are.  You should return all file names that meet the criteria:

for /F %a in ('dir /b *.txt findstr "AB*123*"') do @echo del "%a"

Open in new window


Also, are you sure you are wanting the *?

for findstr:

 .         Wildcard: any character
 *         Repeat: zero or more occurrences of previous character or class
 ^         Line position: beginning of line
 $         Line position: end of line
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.