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.