user1313051
user1313051

Reputation: 39

Deleting folders and subdirectories when a file extension is found in it

I'm new to the board so please go easy! :)

Basically, I'm writing a batch file to delete folders that don't have a file with a certain extension type in them.

so for example if i have the following folder

C:\MUSIC\FLAC\JAZZ\Jazzysong.ape

I need a bit of code that can delete the folder JAZZ because .ape is the wrong file type for the FLAC folder.

Any help would be awesome guys as I've tried variants of findstr, exist and writing to text file and trying to read the path using dir /s /b

I'm lost

Upvotes: 1

Views: 668

Answers (1)

Andriy M
Andriy M

Reputation: 77737

I'm still unsure as to which kind of test you want to perform on a directory before deciding to remove it – "whether a certain file type exists" or "whether a certain file type doesn't exist". Your post seems to suggests it is the latter, so it must be the title that is misleading. For the purpose of this answer, I'm assuming that it is indeed the "whether a certain file type doesn't exist" test that you want.

Here's a very simplistic and unassuming approach to the problem:

@ECHO OFF
FOR /D /R "D:\directory_with_flac_files_only" %%D IN (*) DO (
  IF NOT EXIST "%%D\*.flac" ECHO RD /S /Q "%%D"
)

That is, just traverse the directory tree and, for every subdirectory, check if it doesn't contain the necessary files. If it doesn't, say goodbye to it. (For safety, I added ECHO before the RD command, so that it does indeed say its goodbye but does not perform the real action, just in case you rush into executing the script without reading further.)

That approach is wrong, however. Even though a directory may not contain the required files, its sub-directories might, and this script does not verify that fact. As a consequence, you might lose some of your invaluable FLAC files. (And I've just saved you the grief.)

So, something more elaborate is needed here. The method I came up with consists in the following steps:

  1. Sort the directories into two categories: those that contain the files with the necessary extension and those that don't.

  2. Process the list of directories that do not contain the files:

    1) if a directory is found in the other list, omit it;

    2) if it is not found in the other list, remove it.

(In case you are wondering: a directory from the first list may indeed be found in the second list as part of its subdirectory's path.)

I'm far from being convinced that it's the best approach possible, but at least it should work (it did work for me, anyway). Here's my implementation of it:

@ECHO OFF

:: set the params
SET "ext=flac"
SET "rootdir=D:\dir_with_flac_files_only"
SET "todelete=%TEMP%\dirs_to_delete.txt"
SET "topreserve=%TEMP%\dirs_to_preserve.txt"

:: clear the temp lists
TYPE NUL >"%todelete%"
TYPE NUL >"%topreserve%"

:: sort the dirs
FOR /D /R "%rootdir%" %%D IN (*) DO (
  IF EXIST "%%D\*.%ext%" (
    ECHO %%D>>"%topreserve%"
  ) ELSE (
    ECHO %%D>>"%todelete%"
  )
)

:: process the sorted lists
FOR /F "usebackq delims=" %%D IN ("%todelete%") DO (
  FIND "%%D" <"%topreserve%" >NUL || ECHO RD /S /Q "%%D" 2>NUL
)

You can still see the ECHO before RD, but consider it just my way of saying, ‘In case I missed something…’. Run the script and look at the output. If it correctly shows only the directories that are due to be deleted, remove ECHO and run the script again to get on with the action.

Upvotes: 2

Related Questions