C# ASP.NET SQL SERVER

Powershell script to remove empty directories

Here's  a Powershell script that I've just created to remove empty folders which I'm sure I'm not going to need again in the future.

$items = Get-ChildItem -Recurse

foreach($item in $items)
{
      if( $item.PSIsContainer )
      {
            $subitems = Get-ChildItem -Recurse -Path $item.FullName
            if($subitems -eq $null)
            {
                  "Remove item: " + $item.FullName
                  Remove-Item $item.FullName
            }
            $subitems = $null
      }
}

 

 

» Similar Posts

  1. Powershell Copy-Item does not honor the exclude parameter
  2. Powershell Get-ChildItem - FileSystemInfo or Array
  3. Powershell Ripped Media Renaming Script

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://guyellisrocks.com/trackback.ashx?id=153

» Comments

  1. Rob Manderson avatar

    If you're sure you going to need the empty folders again in the future then why on earth are you removing them???

    Rob Manderson — April 11, 2009 3:48 AM
  2. guy ellis avatar

    In this case I'd written a script which had accidentally created a ton of new empty folders deep inside and large folder tree and I didn't want to manual click through the tree to remove all of them.

    guy ellis — April 11, 2009 9:40 AM
  3. Selenae avatar

    Thanks for the script

    Selenae — November 20, 2009 9:01 AM
  4. jwagner avatar

    Give this a whirl. It will not error on an empty folder. It will delete all files older than 30 days and leave folders alone.

    Take out Where {!$_.PSIsContainer} if you want to nuke every file and folder.

    Change to Where {$_.PSIsContainer}| to nuke every folder. (Does not check for files in subfolders...)

    ---scriptstart----

    $Now = Get-Date

    $Days = “30”

    $TargetFolder = “C:\Test”

    $LastWrite = $Now.AddDays(-$days)

    $Files = get-childitem $TargetFolder -recurse -force | Where {!$_.PSIsContainer}|Where {$_.LastWriteTime -le “$LastWrite”}

    foreach ($File in $Files)

    {IF ($Files -ne $NULL)

    {write-host “Deleting File $File.FullName” -foregroundcolor “Red” ; Remove-Item $File.FullName -recurse -force

    }

    }

    ---script end----

    jwagner — March 25, 2010 3:30 PM
  5. guy ellis avatar

    Thanks jwagner - appreciated.

    guy ellis — March 25, 2010 4:25 PM
  6. Gary avatar

    With some of the ideas posted here i created the following script to cleanup my music folders. I had to many empty folders where the music got pulled out and re-organized by iTunes.

    ---------------------------------------

    Param ([system.io.directoryinfo]$folderpath = $(throw "provide folderpath"))

    $folders = get-childitem $folderpath -recurse -force | ? {$_.PSIsContainer}

    if ($folders -ne $null)

    {

    [array]::Reverse($folders)

    foreach($folder in $folders)

    {

    write-host "-----------------------------------------"

    write-host "Examining contents of $($folder.fullname)"

    $childitems = Get-Childitem $folder.fullname -recurse -force | ? {($_.fullname -like "*.mp3") -or ($_.fullname -like "*.m4a") -or ($_.fullname -like "*.wma")`

    -or ($_.fullname -like "*.aa") -or ($_.fullname -like "*.aax")}

    if($childitems -eq $null)

    {

    "Remove folder: " + $folder.FullName

    Remove-Item $folder.FullName -recurse -force

    }

    else

    {

    write-host "media found in $folder, skipping delete"

    }

    write-host "-----------------------------------------"

    $childitems = $null

    }

    }

    else

    {

    write-host "no sub folders found"

    }

    Gary — April 16, 2010 11:55 PM
  7. scott hayles avatar

    Hi - loved it (although I'm too gutless to run it in case some empty folders simply "have to be there" for an application to run correctly).

    What I'm wondering is - is it possible to re-design this script so that it will recurse through the C drive and provide a list of sets of folders with identical contents; all the "Duplicate Finders" are too granular insofar as they work on the duplicate file level, whereas I only want to identify pairs (or triples, or quads...) of (possibly distinctly named) folders with the exact same file and folder sub-structure.

    Also as a real challenge, replace the notion "folders with the exact same file and folder sub-structure" with "folders, one of which is a proper subset of the other".

    I know this is asking a lot, but at the same time it might be quite a challenge, mightn't it? I won't consider it cheating if the script simply generates (say) csv data to be further examined using Excel or SQLServer!!!

    Scott

    scott hayles — July 18, 2010 7:59 AM
  8. ryan avatar

    the first script above .. how would you use that against another path .. how would i change this so it will run against a drive letter or a unc path ..

    $items = Get-ChildItem -Recurse

    foreach($item in $items)

    {

    if( $item.PSIsContainer )

    {

    $subitems = Get-ChildItem -Recurse -Path $item.FullName

    if($subitems -eq $null)

    {

    "Remove item: " + $item.FullName

    Remove-Item $item.FullName

    }

    $subitems = $null

    }

    }

    i tried to run it and it just keeps using the local drive i cannot get it to run in another script with the path of the server volume - either using the unc naming or thr drive letter on the system that is running it .

    thanks in advance

    ryan — September 10, 2010 11:26 AM
  9. bewnet avatar

    Thanks for posting-saves time.

    bewnet — January 5, 2011 6:47 PM
  10. Boyd avatar

    I had accidentally run a Duplicate file delete program and forgot to check "Delete Empty Folders". This PS script cleared out all these empty folders in a snap. Thanks!!

    Boyd — March 14, 2011 4:27 PM
  11. amit avatar

    i want a script which delete folder that contain only one file in it.

    i.e. i have one folder named abc . this folder contain 5000 sub folder & each sub folder contain files thay may be from 1 to 15 . i just want to delete sub folders that contain only one file in it. all the others folder should remain intact.

    Thanks in advance.

    amit — April 27, 2011 1:27 PM
  12. GVdP avatar

    Compressed version of the script above:

    $folder=<root folder from which you want to clear out empty folders>

    ls $folder|%{if($_.psIsContainer -and !(ls $_.fullName)){echo "Removing $($_.fullName)"; del $_.fullName -force -recurse}}

    And for Amit:

    ls $folder|%{if($_.psIsContainer -and (@(ls $_.fullName).count -eq 1)){echo "Removing $($_.fullName)"; del $_.fullName -force -recurse}}

    Cheers!!

    GVdP — May 25, 2011 7:48 AM
  13. taapes avatar

    One Problem with this Script:

    If it deletes an empty folder and the result is that the folder above it is now empty then this script doesn't delete the new empty folder. To fix this the foreach loop should be a for loop starting from the end of the $items array and moving to the front:

    for($i=$items.length-1 ;$i -ge 0; $i--)

    {

    $item = $items[$i]

    ....

    taapes — July 12, 2011 5:33 PM
  14. guy ellis avatar

    That's a great idea taapes - thanks!

    guy ellis — July 12, 2011 8:20 PM

» Leave a Comment