Droidman
Droidman

Reputation: 11608

batch script delete file containining certain chars

I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like

del "C:\TEST\TEST2\*.TXT"

But I have no idea how to perform the filtering I need. Could someone help me to achieve this using batch?

EDIT more exactly, the question is "how to tell batch to include subfolders?"

Upvotes: 1

Views: 15960

Answers (2)

Endoro
Endoro

Reputation: 37589

Try this:

@echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"

Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.

You can also specify the MySearchString for the file name only:

@echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"

Upvotes: 3

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239824

The /s switch exists on quite a few commands (including del)

del /s "C:\TEST\TEST2\*.TXT"

The help for del /? says:

/S            Delete specified files from all subdirectories.

Upvotes: 7

Related Questions