Find Empty Folders

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
black777
Posts: 25
Last visit: Thu Apr 02, 2015 12:50 pm

Find Empty Folders

Post by black777 »

All,I am new to PowerShell. I am currently reading Don Jones' book and two others, but still having trouble with the fundamental switch from VBScript methodology to PowerShell. I was about to write a VBScript to loop through a folder structure finding any empty folders and removing them. Not a difficult script, but one that I do not know how I would do in PowerShell.I was hoping someone who understands the methodology of PowerShell can help me get a start. My thought process is to use a Get-ChildItem, a loop of some sort to travel down each folder and then a filter for the mode to only return directories. I was then thinking of getting the size of the returned object(s).Any help would be appreciated.--Black
User avatar
black777
Posts: 25
Last visit: Thu Apr 02, 2015 12:50 pm

Find Empty Folders

Post by black777 »

All,I am new to PowerShell. I am currently reading Don Jones' book and two others, but still having trouble with the fundamental switch from VBScript methodology to PowerShell. I was about to write a VBScript to loop through a folder structure finding any empty folders and removing them. Not a difficult script, but one that I do not know how I would do in PowerShell.I was hoping someone who understands the methodology of PowerShell can help me get a start. My thought process is to use a Get-ChildItem, a loop of some sort to travel down each folder and then a filter for the mode to only return directories. I was then thinking of getting the size of the returned object(s).Any help would be appreciated.--Black
User avatar
black777
Posts: 25
Last visit: Thu Apr 02, 2015 12:50 pm

Find Empty Folders

Post by black777 »

jvierra,This is perfect. I was looking at Mode being a 'd' and nothing going, but you come up with $_.Attributes -eq "Directory" which makes much more sense and is so much more readable. The directories I am looking for also need to have no other folders in them. I tried the following, but no luck.dir M:jsmith -rec | where {($_.Attributes -eq "Directory") -and (($_.GetFiles()).Count -eq 0) -and (($_.GetFolders()).Count -eq 0)}Unfortunately, the ($_.GetFolders).Count didn't work as there is not a GetFolders method. (Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'GetFolders'.)I tried a dir M:jsmith | Get-Member and cannot find a GetFiles method. I am a bit confused. I thought I would see the GetFiles method and any other Get* methods and could learn how to ask for folder count, but no such luck. Am I moving down the wrong path?Thanks.--Black
User avatar
black777
Posts: 25
Last visit: Thu Apr 02, 2015 12:50 pm

Find Empty Folders

Post by black777 »

jvierra,You are totally right. I am not used to PowerShell scrolling and as such only went back to where I saw "TypeName: System.IO.FileInfo" believing that to be the start of the output for the cmd "dir M:jsmith | Get-Member" but if I had just scrolled up a bit higher I would have seen the "TypeName: System.IO.DirectoryInfo" and then seen the GetFiles method and then I would have seen the "GetDirectories" method and solved my problem.dir M:jduque -rec | where {($_.Attributes -eq "Directory") -and (($_.GetFiles()).Count -eq 0) -and (($_.GetDirectories()).Count -eq 0)}Works great. I am afraid to test this it but I was hoping I could then do a pipe and then ($_.Delete()). (Not sure if the braces around $_.Delete() are needed or not.) I know there is a -WhatIf argument, but I have no idea where that would go within the cmd line to check if the folder is deleted. I don't want to make a mistake and blow away folders with data in them.So far, I think the following is the correct cmd line to delete fully empty folders.dir M:jduque -rec | where {($_.Attributes -eq "Directory") -and
(($_.GetFiles()).Count -eq 0) -and (($_.GetDirectories()).Count -eq 0)} | ($_.Delete())Thank you for your help.--Black
User avatar
black777
Posts: 25
Last visit: Thu Apr 02, 2015 12:50 pm

Find Empty Folders

Post by black777 »

jvierra,I took your suggestion and put the results into a variable. Much easier to work with and see what I have before deleting.I think I have come upon some type of glitch. In my collection of $cFolders a number of them have [] in the path. i.e. M:jduqueSMG[HOLD]. When I pass the collection through the aliased ForEach-Object cmdlet with a -WhatIf parameter, the returned values only include those folders without brackets in them. I know that brackets are special inside PowerShell, but I figured they would be escaped by PowerShell as the values are passed as part of the -Path. I guess assuming that the brackets would somehow be escaped is incorrect.Do you know of a better way to pass the full path of a folder to the -Path parameter such that brackets wouldn't interfere with the Remove-Item cmdlet?Thanks--Black
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Find Empty Folders

Post by jvierra »

Black

The characters are illegal in file/folder names. You can create them but you can't get at them except through the "short name" (8.3). If 8.3 is disabled as it would be on many secure systems and on some fle sharing services then you are out of luck.

If you have a Mac or Unix box then it can and will use these characters and they are supported in DOS/NT but not in the file provider in PoSH apparently.

You cannot access these folders even if you escape the brackets manually.

That said. YOu can delete them manually one at at ime starting at the deepest level and use the "Delete()" method on each object. To do this you will have to maintain the hierarchical structure and work from the bottom up.

I saw this code somewhere. I think it was either "The PowerShell Guy" or, possibly, Jeff Snovers blog. He had a simple routine that could walk the folder tree from the end to the root. Sort of a reverse binary tree walker. Or it could be done using simple recursion. The remove-item doesn't seemt to like the folder names and chokes on teh object itself if the folder is not empty.

Also a recursive or repetitive call on the empty folder filter would slowly find all totally empty folders. Your empty folder filter doesn't work as it still returns folders containing empty folder ( as far as empty of files).


This topic is 15 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked