Delete files that contain a specific string

Posted on 2013-11-27
Last Modified: 2013-11-30
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.
Question by:100questions
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

by:Steven Harris
ID: 39682696
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


Author Comment

ID: 39682701
I want to delete the files that contain the string AB*123* within them, not within the name of the file
LVL 27

Expert Comment

ID: 39683337
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.

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 18

Expert Comment

by:Steven Harris
ID: 39683435
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.

Author Comment

ID: 39683463
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.
LVL 18

Expert Comment

by:Steven Harris
ID: 39683576
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
LVL 54

Accepted Solution

Bill Prew earned 500 total points
ID: 39683694
Should be as simple as this, from a command line do:

for /f "tokens=*" %A in ('findstr /i /m /c:"AB.*123" "c:\temp\*.txt"') do del "%~A"

Open in new window

or in a BAT script do:

for /f "tokens=*" %%A in ('findstr /i /m /c:"AB.*123" "c:\temp\*.txt"') do del "%%~A"

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question