2

Question: Is it possible to delete every folder in a certain directory that does not contain specific file types for example music files (mp3/flac/wav/etc)?

I've retagged some files using Mp3Tag which also moves them into a new folder structure:

\Artist\Album\Artist Name - Album - Track Number - Track.ext

Example file structure

This results in the old subfolders without any music files (mp3/flac/wav/etc) while leaving remnants of cover.jpg or log files and etc.

So for example, in the image above I want to delete "\Adele - 25 Album". Ultimately, I want to delete any subfolders without any music files (mp3/flac/wav/etc).

I've taken some hard stabs at this but I can't really come up with anything fruitful and having a hard time with it too. I wonder if someone could help me with some guidance for this task.

1
  • Rename f:\!organize to f:\organize. Modify your question to include every music file extension you want to search for and post it this way: *.aac, *.m4a, ... Or list the extensions of the files you would like to delete: *.txt, *.log, *.jpg. I would prefer an actual dir /b /s /a-d in text that represents the directory structure of the problem you are trying to solve. Does something like this occur?: music103\1.jpg music103\6\1.flac?
    – somebadhat
    May 24, 2020 at 23:04

3 Answers 3

0
  • Update:

Deleting folder with { and/or [ in the name, by invoking:

cmd.exe /s rm some_folder_with_{special}_character_they_[name]`:

cmd /v/s/c "del /q /f "%%~dpnxi*.*" && rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"

@echo off && setlocal enabledelayedexpansion

set "_mp3dir=F:\SUPER_USER\Q1507690\Organize"
set "_filter=%temp%\_filter_audio_!random!.log"

set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i

for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul || set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
     2^>nul %__APPDIR__%where.exe "%%~dpnxi:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
     if "!_rm_dir!"=="yEp" cmd /v/s/c "echo\del /q /f "%%~dpnxi\*.*" && echo\rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"
     )

set "_filter="<nul & del /q /f "%temp%\_filter_audio_*.log" & endlocal & goto :EOF

Test and if its seems zOk to you, just remove this echo command below:

if "!_rm_dir!"=="yEp" cmd /v/s/c "echo\del /q /f "%%~dpnxi*.*" && echo\rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"

@echo off && setlocal enabledelayedexpansion

set "_mp3dir=F:\Organize"
set "_filter=%temp%\_filter_audio_!random!.log"

set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i

for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul || set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
     2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
     if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
    )

set "_filter="<nul & del /q /f "%temp%\_filter_audio_*.log" & endlocal & goto :EOF
  1. Define (set) all the necessary extensions to keep your audio files in the folder tree:
set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
  1. Save all extensions in a file to use with findstr when checking the output of the listing for each folder (the last one at each level), using a for loop in the _audios variable previously defined:
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i
  1. Use a specific loop for folders and recursive (for /d /r) at the same time checking if the current folder in the loop is the last folder at the current level (tree ..|find "\-")that is, without subfolder below...
for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul

Note: This method is explained in detail in this answer

  1. Use a for /f loop additional to list files at the last level of each subfolder, and with the where command redirecting the file extension to the find to search the log file for the occurrence.
(for /f tokens^=* %%# in ('
     2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul
  1. When find command finds an audio extension at the output of the loop, the variable _rm_dir will change the string from yEp to nOp, preventing the current folder from being deleted/removed, and when the opposite happens, where no existing file or none with an audio extension, the variable will keep the value of _rm_dir for yEp.
set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
     2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
  1. After the previous loop is finished, if will take the action (if necessary) to delete/remove the folder without audio files, when the condition is true for the comparison of variable _rm_dir equal to string yEp:
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
  1. Test and check the output, if it looks correct, just remove the echo\ and specific ^ from ^&^&:
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
  1. The echo command is to give you a preview of the actions resulting from the script
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
  1. [Refer to the Update] To use your command to delete folders with a special character in the name, just invoke cmd with flag /s:

cmd /v/s/c "del /q /f "%%~dpnxi*.*" && rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"

rmdir /q "F:\SUPER_USER\Q1553051\Organize\540x21" && Folder: "F:\SUPER_USER\Q1553051\Organize\540x21" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\54x320" && Folder: "F:\SUPER_USER\Q1553051\Organize\54x320" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\12x349" && Folder: "F:\SUPER_USER\Q1553051\Organize\12x349" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\543x21" && Folder: "F:\SUPER_USER\Q1553051\Organize\543x21" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\123x45" && Folder: "F:\SUPER_USER\Q1553051\Organize\123x45" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\12x346" && Folder: "F:\SUPER_USER\Q1553051\Organize\12x346" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\empty_" && Folder: "F:\SUPER_USER\Q1553051\Organize\empty_" was deleted

But, please consider paying attention to the comment @somebadhat:
Rename F:\!Organize to F:\Organize

Try to rename your folder by removing !, even if only for running this script.

1
  • Thanks for the assistance. I'm still having an issue similar to what Pimp Juice IT provided when running both of yours, in which folders with characters such as { [ and other similar characters still exist after running the script. Here is an imgur link of what I see in my resulting test folder. i.imgur.com/ttNA0oT.png
    – Josh
    May 27, 2020 at 18:21
0

PowerShell:

Using the -LiteralPath parameter with Remove-Item alllows it to handle special characters in the name.

$Source = 'F:\Organize'
$KeepExts = '.mp3', '.flac', '.wav'

# Directories with files to keep
$Keep = gci $Source -file -recurse |
   Where Extension -in $KeepExts |
      Select -expand DirectoryName -unique

# Directories with no subfolders
$LeafDirs = gci $Source -dir -Recurse |
   Where { (!(gci $_.FullName -dir)) } |
      select -expand FullName

$ToDelete = $LeafDirs | Where { $_ -notIn $Keep }

Remove-Item -LiteralPath $ToDelete -recurse
0
  1. As mentioned in a previous comment, to keep this simple, it will be best to remove the ! exclamation mark from any folder names so the PowerShell Get-ChildItem will work without issue to start.

    Note: Once the cleanup process is entirely complete, you can easily add the exclamation mark back to any folder name(s) as desired if you must.

  2. You can then use the below PowerShell script I provided as an example to do what you need it to do for only deleting folders and all folder contents that do not contain the media file extensions you need to keep.

Important Items

  • You will want to add the specific file extensions to the $Filter = @("*.aac","*.mp3","*.wav","*.<ext>","*.<ext>","...","...","*.<ext>"); variable at the top of the script so it knows to exclude those directories from the removal process since those contain what you wish to keep folder content wise.

  • This process is started first wiping all empty directories with no files or folders within by running Robocopy <dest> <dest> /S /MOVE and then once the files are removed from folders not containing the file extensions you wish to keep, it will run this at the end wiping any further empty directories.

PowerShell

$Src = "F:\Organize"
$Filter = @("*.aac","*.mp3","*.wav","*.flac");

Robocopy $Src $Src /S /MOVE 

$AllDirs = (Get-ChildItem $Src -Directory -Recurse).FullName;

$WipeDirs = @();
$AllDirs | % { Process {
    If(!(Get-ChildItem -LiteralPath "$_" -File -Include $Filter -Recurse).DirectoryName){ 
        [array]$WipeDirs += "$_" 
        }
    }};

$WipeDirs | % { Process {
    Get-ChildItem $_ | Remove-Item -Force;
    }};

$AllDirs+$Src | % {
    $nonFilterFiles = (Get-ChildItem -LiteralPath "$_" -File).FullName; 
    $FilterFiles    = (Get-ChildItem -Path "$_\*" -File -Include $Filter).FullName;

    If($FilterFiles.Count -ge 1){ Return; };
    If($FilterFiles.Count -eq $nonFilterFiles.Count){ Return; };
    If(!$FilterFiles){ $nonFilterFiles | % { Process { Remove-Item -LiteralPath $_ -Force; }}};
    If((!$FilterFiles) -and $nonFilterFiles -ge 1){ $nonFilterFiles | % { Process { Remove-Item -LiteralPath $_ -Force; }}};

    };

Robocopy $Src $Src /S /MOVE 

Supporting Resources

10
  • Josh - I will be happy to make adjustments, etc. and further test if there are any issues. Just explain to me the problem, I will recreate and test, and then edit my answer for a solution. Any further questions, or whatever, let me know—I'm here to help you get this resolved fully. May 24, 2020 at 19:53
  • Hello Pimp Juice IT. Thanks so much for the assistance. I tried running it in a test folder, but it still has some folders that only contain a "cover.jpg" file. Here's an example of what I'm seeing in my test folder after running the script you posted above in a .ps1 file. i.imgur.com/QDjkaj6.png
    – Josh
    May 25, 2020 at 2:13
  • I tried with your updated script but it still has the same issue as seen above in the Imgur image link. However now it also deletes the .ps1 file. Right now I have a bunch of example files and folders copied to "F:\TEST\Organize" which also has the .ps1 file inside. Running the .ps1 file deletes itself after running.
    – Josh
    May 26, 2020 at 3:52
  • I'm okay with the the ! issue and renaming the folder before running the script. Yeah there are quite a bit of folders that have square brackets which aren't properly deleted by Robocopy, but I don't see any other issues other than that. Thank you for the help... Here's an example of what I'm seeing after running the script in my test folder: i.imgur.com/ttNA0oT.png
    – Josh
    May 27, 2020 at 18:16
  • 1
    Hey there apologies for just ghosting you. I'll get back to you tonight in regards to this and see how the solution works out for me.
    – Josh
    Feb 4, 2021 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .