Page 1 of 1

Delete files which matches to the pat tern

Posted: 25 Jun 2010 01:26
by ernest211
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?

Re: Delete files which matches to the pat tern

Posted: 25 Jun 2010 08:01
by aGerman
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

Re: Delete files which matches to the pat tern

Posted: 28 Jun 2010 03:54
by miskox
Can't a simple

Code: Select all

del *test*.*


do for you?

Saso