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

» Leave a Comment