Using a batch file, how do I delete files that have a certain string of characters inside? -


i want make .bat files can allow me delete file if string of characters appears inside. example, if want delete files have string harp inside of , files in folder are:

harp1.txt mellowharp.txt highharplow.txt 

i want able delete of these files. figured out how if starts or ends string, merely writing

del harp*.* del *harp.* 

but can't figure out how if harp found in middle of string.

i've tried things putting asterisk on each side of harp catch case, doesn't work.

this literally first day , experience .bat files, , appreciate on this, please gentle , explain because i'm coming no knowledge whatsoever.

well, since using del *harp*.* didn't work you, iterate through files in specified directory delete when substring "harp" found in file name.

this should it:

@echo off  /f %%f in ('dir /b * ^| find /i "harp"') del %%f  echo files deleted... pause>nul exit 

make sure .bat file in same directory other files.

explanation:

the for command loops through files in directory , assigns them variable %%f , searches substring "harp" in file name. then, when found, do del %%f tells program delete file (%%f). read more for statements here.

the echo command prints line of text console window. read more echo click here.

the pause>nul pauses program , waits user press key, without text being displayed on screen. (the normal text appear press key continue...). more pause here.

the exit command stops program. more on exit here.


Comments