Garmekain
Garmekain

Reputation: 674

Delete file with same name contained in different sub-folders

For some reason, I have a copy of dismhost.exe in a lot of folders, inside temp folder; what I want to do is delete every instance of it, which are inside folders inside temp.

So the structure is as follows:

/temp
 /folder1
  dismhost.exe
 /folder2
  dismhost.exe
 /folder3
  dismhost.exe
 ...

I first tried

rm ./*/dismhost.exe

but then I remembered there is no rm in windows, so I tried with rd with same arguments. That raised an error, saying that the * modifier is not valid.

How can I achieve this?

Upvotes: 0

Views: 4120

Answers (3)

Maulik Kakadiya
Maulik Kakadiya

Reputation: 1677

In Windows

To delete a single file, use the following command:

del C:\enter\your\path\here /f /s

remove a specific file type from a folder:

del *.extension

Swap out "extension" for the file type you want to remove.

You can extend the command to delete all of the specific file extension from subfolders with the addition of a couple of parameters:

del /s /q *.extension

if you want to delete multiple file types, you can add multiple extension types:

del /s /q *.png *.svg

If you want to files along with folders remove, you can use the following commands:

del /f /s /q C:\enter\your\path\here > nul

rmdir /s /q C:\enter\your\path\here

Upvotes: 0

vitsoft
vitsoft

Reputation: 5805

Or use recursive delete.:

del /s \temp\dismhost.exe

Upvotes: 0

lit
lit

Reputation: 16256

This can be done using a FOR loop iterating over a list of files returned by a recursive DIR search. When you are satisfied with the output, remove ECHO in order to actually delete the files.

FOR /F "usebackq tokens=*" %f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (ECHO DEL "%~f")

If this is placed into a .bat script, be sure to double the % characters on the variable.

FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (
    ECHO DEL "%%~f"
)

Upvotes: 3

Related Questions