Delete files which matches to the pat tern

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ernest211
Posts: 6
Joined: 14 Jun 2010 06:20

Delete files which matches to the pat tern

#1 Post by ernest211 » 25 Jun 2010 01:26

How can I delete files which matches to pattern for example I have folder with files
test.txt
some_image.bmp
test
setup.exe
test a1

now I want to delete files which name contain word “test” so this will by three files
test.txt
test (file don’t have extension)
test a1 (file don’t have extension)

I have loop

Code: Select all

for /f "delims=" %%a IN ('dir /b') do (    
   if "%%a"=="test*.*" (
     del %%a
   )
 )

but condition is bad how to fix that?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete files which matches to the pat tern

#2 Post by aGerman » 25 Jun 2010 08:01

Pipe it to FINDSTR to match the pattern. Try something like that

Code: Select all

for /f "delims=" %%a in ('dir /a-d /b^|findstr /b /i /c:"test"') do del "%%a"

dir /a-d /b - doesn't return folders (/a-d) and verbose infos
findstr /b /i /c:"test" - test have to be on the beginning of the name (/b), ignore upper/lower case (/i)

Regards
aGerman

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Delete files which matches to the pat tern

#3 Post by miskox » 28 Jun 2010 03:54

Can't a simple

Code: Select all

del *test*.*


do for you?

Saso

Post Reply