Clavus
Clavus

Reputation: 129

Batch file to recursively loop through subfolders and remove files upon condition

I'm trying to automate a little tedious process I have to go through while updating files on my server. I have a content folder, with a lot of subfolders, each potentially containing some files. Some files have a compressed version (ending with .bz2). So a folder could have something like:

sound1.wav
sound1.wav.bz2
sound2.wav
texture1.tex
texture2.tex
texture2.tex.bz2

What I want to do is remove every file (somewhere in the content folder) that has an equivalent compressed file. Meaning in the above example I just want 'texture2.tex' and 'sound1.wav' removed.

Upvotes: 1

Views: 10605

Answers (2)

MiKL
MiKL

Reputation: 11

Little mistake. It should be:

for /r %%f in (*.bz2) do if exist "%%f" del "%%f"

Or, at the command line instead of in a batch file:

for /r %f in (*.bz2) do if exist "%f" del "%f"

Upvotes: -2

Joey
Joey

Reputation: 354864

for /r %%f in (*) do if exist "%%f.bz2" del "%%f"

Or, at the command line instead of in a batch file:

for /r %f in (*) do if exist "%f.bz2" del "%f"

Upvotes: 4

Related Questions