Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Remove empty folders

Reply
 
Old 06-26-2007   #1 (permalink)
brhessel


 
 

Remove empty folders

Looking for some help. I am scripting the move of files with a last
access time older than 30 days and I would like to remove the folder
that contains these files if that folder is empty after the files are
moved. Here is what I have so far to move the files and maintain the
folder structure. (Thanks to other posts on here)

$now = get-Date
get-ChildItem -recurse *|Where-Object{$_.LastAccessTime -le
$now.AddDays(-30)}|move-item -Destination {join-path 'Z:'
$_.FullName.SubString($pwd.Path.length)} -PassThru -Whatif

any help is appreciated.


My System SpecsSystem Spec
Old 06-26-2007   #2 (permalink)
dreeschkind


 
 

RE: Remove empty folders

ls . -fi * -rec | ?{$_.psiscontainer} | %{if (@(gci $_.fullname).count -eq 0)
{del $_.fullname -whatif}}

--
greetings
dreeschkind

"brhessel" wrote:

> Looking for some help. I am scripting the move of files with a last
> access time older than 30 days and I would like to remove the folder
> that contains these files if that folder is empty after the files are
> moved. Here is what I have so far to move the files and maintain the
> folder structure. (Thanks to other posts on here)
>
> $now = get-Date
> get-ChildItem -recurse *|Where-Object{$_.LastAccessTime -le
> $now.AddDays(-30)}|move-item -Destination {join-path 'Z:'
> $_.FullName.SubString($pwd.Path.length)} -PassThru -Whatif
>
> any help is appreciated.
>
>

My System SpecsSystem Spec
Old 06-26-2007   #3 (permalink)
RichS


 
 

RE: Remove empty folders

Something like this should remove the empty folders. Modify the "c:\" in the
getfolder to point to the root of the folders you want to test

$fso = New-Object -com "Scripting.FileSystemObject"
$folder = $fso.GetFolder("C:\")

foreach ($subfolder in $folder.SubFolders)
{
If ($subfolder.Size -eq 0)
{

remove-item $subfolder.Path -Verbose

}


}


--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"brhessel" wrote:

> Looking for some help. I am scripting the move of files with a last
> access time older than 30 days and I would like to remove the folder
> that contains these files if that folder is empty after the files are
> moved. Here is what I have so far to move the files and maintain the
> folder structure. (Thanks to other posts on here)
>
> $now = get-Date
> get-ChildItem -recurse *|Where-Object{$_.LastAccessTime -le
> $now.AddDays(-30)}|move-item -Destination {join-path 'Z:'
> $_.FullName.SubString($pwd.Path.length)} -PassThru -Whatif
>
> any help is appreciated.
>
>

My System SpecsSystem Spec
Old 06-26-2007   #4 (permalink)
Kiron


 
 

Re: Remove empty folders

If you want to include hidden and read-only empty folders include the -force
parameter:

To see if a folder is empty:

# True if empty
!(gci <path> -fo)

To remove all empty folders under <path>:

ri (gci <path> -r -fo | ? {$_.PSIsContainer -and !(gci
$_.fullName -fo)}).fullName -fo

--
Kiron

My System SpecsSystem Spec
Old 06-27-2007   #5 (permalink)
Kiron


 
 

RE: Remove empty folders

Scratch my previous answer, I tested it and only worked on the first empty
folder. This one does the job:

To see if a folder is empty:

# True if empty
!(gci <path> -fo)

To remove "all" empty folders under <path>:

foreach($dir in @(gci <path> -r -fo | ? {$_.PSIsContainer -and !(gci
$_.fullName -fo)})) {ri $dir.fullName -fo}

--
Kiron
My System SpecsSystem Spec
Old 07-14-2007   #6 (permalink)
Ryan Beesley


 
 

RE: Remove empty folders - Kiro

I'm not sure this quite works.

>To see if a folder is empty:
>
>!(gci <path> -fo)
>
>To remove "all" empty folders under <path>:
>
>foreach($dir in @(gci <path> -r -fo | ? {$_.PSIsContainer -and !(gci $_.fullName -fo)})) {ri $dir.fullName -fo}


The problem is that you may have a tree like this

|
|\DIR1
| |\DIR1.1 (empty)
| \DIR1.2 (empty)
\DIR2 (empty)

The above script will remove DIR1.1, DIR1.2, and DIR2, but it will leave DIR1 because when it was checked it wasn't empty, it contained Items DIR1.1 and DIR1.2. With a massively deep tree, this leaves many branches untrimed.

This is what I came up with:

foreach ($dir in @(gci . -r | where {$_.PSIsContainer} | sort -descending fullName)) {ri $dir.fullName | where {!(gci $dir.fullName)}}

First I build a list of all possible directories and I sort it decending so that the parent directory is always after the children in the list. Then I iterate over that list and only remove the directory if it has no children. Since I am working from a list of directories created before I start deleting, this lets me check each directory along the way and should prune everything.

To use, CD to the directory you want to prune, and run the above statement.

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
My System SpecsSystem Spec
Old 07-14-2007   #7 (permalink)
RyanBee


 
 

RE: Remove empty folders - Kiro

Slight change... the old way kept prompting me... I relized that I should
have been using an if statement instead of where... try this instead.

foreach ($dir in @(gci . -r | where {$_.PSIsContainer} | sort -descending
fullName)) {if (!(gci $dir.fullName)) {ri $dir.fullName}}

"Ryan Beesley" wrote:

> I'm not sure this quite works.
>
> >To see if a folder is empty:
> >
> >!(gci <path> -fo)
> >
> >To remove "all" empty folders under <path>:
> >
> >foreach($dir in @(gci <path> -r -fo | ? {$_.PSIsContainer -and !(gci $_.fullName -fo)})) {ri $dir.fullName -fo}

>
> The problem is that you may have a tree like this
>
> |
> |\DIR1
> | |\DIR1.1 (empty)
> | \DIR1.2 (empty)
> \DIR2 (empty)
>
> The above script will remove DIR1.1, DIR1.2, and DIR2, but it will leave DIR1 because when it was checked it wasn't empty, it contained Items DIR1.1 and DIR1.2. With a massively deep tree, this leaves many branches untrimed.
>
> This is what I came up with:
>
> foreach ($dir in @(gci . -r | where {$_.PSIsContainer} | sort -descending fullName)) {ri $dir.fullName | where {!(gci $dir.fullName)}}
>
> First I build a list of all possible directories and I sort it decending so that the parent directory is always after the children in the list. Then I iterate over that list and only remove the directory if it has no children. Since I am working from a list of directories created before I start deleting, this lets me check each directory along the way and should prune everything.
>
> To use, CD to the directory you want to prune, and run the above statement.
>
> EggHeadCafe.com - .NET Developer Portal of Choice
> http://www.eggheadcafe.com
>

My System SpecsSystem Spec
Old 07-14-2007   #8 (permalink)
Kiron


 
 

Re: Remove empty folders - Kiron

Thanks for catching that and fixing the script. I'm guilty of not testing
_thoroughly_


--
Kiron

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Remove empty folders in C:\Windows\Temp ?? Vista General
remove all empty folders PowerShell
file folder icons display non-empty folders as empty Vista file management
Can I Permanently Remove Empty Removable Drives? Vista hardware & devices
empty folders??? Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46