0

Edited for clarity. I want to run a script to delete the empty folders and subfolders in my old music directory but I have run in to a problem I can’t figure out as I’m new to this. Some of the folders have been removed and if I create a new empty folder it will be deleted. The issue seems to be that some of the folders have files in them that don’t show up on Windows Explorer but are being seen by the script as evidenced by the error shown below. Also, I am able to delete the folders manually, I'm just trying to avoid hours of deleting of hundreds of folders and learn something on the way.

As I am new to Stack Overflow I can’t post images inline so these links are of images of a Windows explorer window showing a folder to be empty Image of empty folder in Windows Explorer and a screenshot of VS Code autocompleting the files that shouldn’t exist autocomplete of invisible files. I can’t seem to find any information on the permission errors that keep coming up. I have set the root folder and individual subfolders to be owned by Everyone with full control on my machine but that doesn’t make a difference. I've also run PowerShell as Admin but it still makes no difference. This is a personal project where I’m hoping to learn and I don’t intend to use this a free code writing service, I just need a bit of help understanding what’s going.

The issue seems to be more about the invisible files and their permissions over the actual deleting of the folders.

These are the various scripts that I have found from around the internet but don't have an effect on the folders

$MusicPath = "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy"
do {
    $dirs = Get-ChildItem $MusicPath -directory -recurse | Where-Object { (Get-ChildItem $_.fullName -Force).count -eq 0 } | Select-Object -expandproperty FullName
    $dirs | Foreach-Object { Remove-Item -force -recurse }
  } while ($dirs.count -gt 0)
Get-ChildItem $MusicPath -Directory | Where-Object {-NOT $_.GetFiles("*","AllDirectories")} | Remove-Item 

This script is from a comment below but couldn’t delete the empty folder because of the same permission error and it identified a folder with 20 mp3 files as empty

$Parent = 'C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy'  # CHANGE THIS TO YOUR DESIRED FOLDER!!! #
$Dirs = Get-ChildItem -Path $Parent -directory -Recurse
foreach ($item in $Dirs) {   
    if ((get-childitem $item.Fullname | measure-object | select -ExpandProperty count) -eq '0') {
    write-verbose -Message "The folder $($item.Fullname) is empty" -Verbose
    remove-item $item.Fullname -Verbose
    }
}

This line was to see if I could directly remove folders

Remove-Item -Path "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\Clinton Sparks\SXSW 2009 Showcasing Artists\"

And it resulted in the following error message, I've only shown one here but it is repeated for each invisible file.

Remove-Item : Cannot remove item C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\Clinton Sparks\SXSW 2009 Showcasing Artists\AlbumArtSmall.jpg: You do not have sufficient access rights to perform this operation. At D:\Respositories\BatchFiles\Delete_Empties.ps1:18 char:1 + Remove-Item -Path "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (AlbumArtSmall.jpg:FileInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

This line below would remove the folder but when I add the -force switch to one of the above codes it still does nothing.

remove-item -force -recurse -path "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\Clinton Sparks\SXSW 2009 Showcasing Artists\"

I did try to use this code to move the empty folders to a different directory but again it did nothing to the effected folders

Get-ChildItem -Path $MusicPath -Recurse | Move-Item -Destination $NewAlbumPath

I have also tried this .bat file with no luck, I did add the /S switch after the rmdir but then it tried to delete folders with files I want to keep.

@echo off
    setlocal enableextensions disabledelayedexpansion
    for /f "delims=" %%a in ('dir C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy /s /b /ad ^| sort /r') do 2>nul rmdir "%%~fa"

Hopefully this is clearer and my problem makes sense. If any can point me to a resourse that can help me solve this issue it would be a step forward.

LewisKelly
  • 3
  • 1
  • 6
  • Possible duplicate of: https://stackoverflow.com/a/11005300/175063 I know mentioned PowerShell, but also have Batch in your tags too. – Leptonator Apr 10 '20 at 13:41
  • your question isn't super clear on what you want to do, but your description of events suggest you should try running your scripts with elevated rights. Have you tried that? – memory of a dream Apr 10 '20 at 15:27
  • Apologies for not being clear enough. I would like to delete all empty folders and sub folders in my music directory to clear it up. The issue is that if I look at the folder in Windows Explorer it shows as empty (Pic in link 1) but there seems to be invisable files that VS Code can see (Pic in Link 2). I have tried running PowerShell as Admin and I have changed the Ownership of the music folder to 'Everyone' and given that user full control. Continued in the next post. – LewisKelly Apr 10 '20 at 17:51
  • I did try just using #Remove-Item -Path "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\Clinton Sparks\SXSW 2009 Showcasing Artists\" as an experiment and I got the following error which is in the next comment – LewisKelly Apr 10 '20 at 17:55
  • Remove-Item : Cannot remove item C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\Clinton Sparks\SXSW 2009 Showcasing Artists\AlbumArtSmall.jpg: You do not have sufficient access rights to perform this operation. At D:\Respositories\BatchFiles\Delete_Empties.ps1:18 char:1 + Remove-Item -Path "C:\Users\Lewis\Desktop\Extraction_Test\Music_Copy\ ... + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (AlbumArtSmall.jpg:FileInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand – LewisKelly Apr 10 '20 at 17:55

2 Answers2

0
$Parent = 'D:\MySuperCoolMusic'  # CHANGE THIS TO YOUR DESIRED FOLDER!!! #
$Dirs = Get-ChildItem -Path $Parent -directory -Recurse

foreach ($item in $Dirs) {   
    if ((get-childitem $item.Fullname | measure-object | select -ExpandProperty count) -eq '0') {
    write-verbose -Message "The folder $($item.Fullname) is empty" -Verbose

    remove-item $item.Fullname -Verbose
    }
}

You may need to run it a couple of times until it finds nothing. An empty folder inside a folder will result in the parent not being empty. So the first pass removes that empty folder and the 2nd pass removes the now empty parent.

This removes empty folders. As for those Ghost files - try turning explorer to not hide system files. They may show up then. Just be sure to turn that option back off when done. You don't want to accidentally delete a system file down the road.

If you want to forcefully delete something using PowerShell:

run it as an admin (right click and run as admin from start menu) change to the parent directory that contains the dir you want to remove then run this:

remove-item -force -recurse -path "c:\whatever folder you want to remove"
Zack A
  • 688
  • 5
  • 11
  • Thanks for your reply but I got the same error as before about permission rights shown in the editied question above. Additionally, that code flagged a folder with 20 .mp3 files in it as empty. The 'remove-item -force -recurse' works fine but doesn't when used with code to identify the empty folders. – LewisKelly Apr 10 '20 at 18:08
0

Thanks for the comments and suggestions, I have now got it to work more or less. The main issue was the hidden system files in each folder. Once I changed the options on Windows Explorer, I used a Batch file to delete these before running through and deleting the empty folder. Hopefully this helps someone in the future. This is the PowerShell script I used to move the files I wanted over to the folder I wanted.

# A script to run through each artist and album folder in a music folder 
# and move the mp3s which are from one of the SXSW showcasing artists 
# collections to a combined folder.

$MusicPath = "H:\Music Archive\*\SXSW 20** Showcasing Artists\"
$NewAlbumPath = "C:\Users\Lewis\Desktop\SXSW"

get-item $MusicPath | foreach ($_){$oldpath = $_.FullName; robocopy $oldpath $NewAlbumPath /MOVE /E}

This is the Batch script I used to remove all of the hidden system files that were making the deletion script to ignore the ‘empty’ folders

:: This script will delete all the hidden and hidden system files from 
:: the selected directory and sub-directories
del "H:\Music Archive" /A:H /S /Q

This is the script used to go through and delete the empty directories and sub directories. It will need to run a couple of time to make sure it catches everything. This is the code that is mentioned above in a reply that didn’t work at first due the hidden system files. It will flag a directory as empty when it still has files in it but won’t delete it when run properly.

# This is a script to remove the empty folders after the music 
# files have been moved

$Parent = "H:\Music Archive"
$Dirs = Get-ChildItem -Path $Parent -directory -Recurse

foreach ($item in $Dirs) {   
    if ((get-childitem $item.Fullname | measure-object | select -ExpandProperty count) -eq '0') {
    write-verbose -Message "The folder $($item.Fullname) is empty" -Verbose

    remove-item $item.Fullname -Verbose
    }
}
LewisKelly
  • 3
  • 1
  • 6