Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Remove empty folders

已查看 54 次
跳至第一个未读帖子

brhessel

未读,
2007年6月26日 15:08:142007/6/26
收件人
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.

dreeschkind

未读,
2007年6月26日 17:08:022007/6/26
收件人
ls . -fi * -rec | ?{$_.psiscontainer} | %{if (@(gci $_.fullname).count -eq 0)
{del $_.fullname -whatif}}

--
greetings
dreeschkind

RichS

未读,
2007年6月26日 17:11:012007/6/26
收件人
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

Kiron

未读,
2007年6月26日 20:28:102007/6/26
收件人
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

Kiron

未读,
2007年6月27日 00:30:042007/6/27
收件人
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

ryanbeesley

未读,
2007年7月14日 00:47:002007/7/14
收件人
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

RyanBee

未读,
2007年7月14日 01:46:072007/7/14
收件人
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}}

Kiron

未读,
2007年7月14日 10:05:282007/7/14
收件人
Thanks for catching that and fixing the script. I'm guilty of not testing
_thoroughly_ :)


--
Kiron

Wammus

未读,
2010年5月7日 08:35:012010/5/7
收件人
Just for testing I changed the line to:

ForEach ($dir in @(Get-ChildItem . -Recurse | Where {$_.PSIsContainer} | Sort -Descending FullName)) {
If (!(Get-ChildItem $dir.FullName)) {$dir.FullName}}

Now it also displays directories that have the (special) characters [ and ] in them (which are not empty at all)

When I change the last part of the command back to {Remove-Item $dir.fullName -WhatIf} it only displays the empty folders and not the folders that have [ and/or ] in the name.

Any solution/suggestion?


RyanBe wrote:

Slight change... the old way kept prompting me...

14-Jul-07

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:

Previous Posts In This Thread:

On Tuesday, June 26, 2007 3:08 PM
brhessel wrote:

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.

On Tuesday, June 26, 2007 5:08 PM
dreeschkin wrote:

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

--
greetings
dreeschkind

"brhessel" wrote:

On Tuesday, June 26, 2007 5:11 PM
Rich wrote:

Something like this should remove the 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:

On Tuesday, June 26, 2007 8:28 PM
Kiron wrote:

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

To see if a folder is empty:

!(gci <path> -fo)

To remove all empty folders under <path>:

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

--
Kiron

On Wednesday, June 27, 2007 12:30 AM
Kiro wrote:

Scratch my previous answer, I tested it and only worked on the first empty
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:

!(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

On Saturday, July 14, 2007 12:47 AM
Ryan Beesley wrote:

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.

On Saturday, July 14, 2007 1:46 AM
RyanBe wrote:

Slight change... the old way kept prompting me...
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:

On Saturday, July 14, 2007 10:05 AM
Kiron wrote:

Thanks for catching that and fixing the script.

Thanks for catching that and fixing the script. I am guilty of not testing
_thoroughly_ :)


--
Kiron


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Binding Beyond the Limitation of Name Scopes
http://www.eggheadcafe.com/tutorials/aspnet/ef583104-e507-491d-b05f-49faac8854c8/wpf-binding-beyond-the-li.aspx

Wolfgang Kais

未读,
2010年5月19日 16:17:322010/5/19
收件人

Hello wammus.

<Wammus> wrote:
> Just for testing I changed the line to:
>
> ForEach ($dir in @(Get-ChildItem . -Recurse | Where {$_.PSIsContainer} |
> Sort -Descending FullName)) {
> If (!(Get-ChildItem $dir.FullName)) {$dir.FullName}}
>
> Now it also displays directories that have the (special) characters [
> and ] in them (which are not empty at all)
>
> When I change the last part of the command back to {Remove-Item
> $dir.fullName -WhatIf} it only displays the empty folders and not the
> folders that have [ and/or ] in the name.
>
> Any solution/suggestion?

Since you did not supply a parameter name when using Remove-Item,
$dir.fullName is used as value of the -Path parameter which supports
wildcards. So, if the directory name was c:\what[ever], the CmdLet would
try to find an item whose name starts with c:\what and then ends with
one of the letters e, v or r. Try the -LiteralPath parameter instead:
Remove-Item -LiteralPath $dir.fullName -WhatIf

--
Kind regards,
Wolfgang Kais


0 个新帖子