Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums

Go Back   Vista Forums > Vista technology newsgroups > PowerShell

How to delete "old" directories (recursively)?

Reply
 
Thread Tools Display Modes
Old 04-14-2008   #1
David Stuart
Guest
 
Posts: n/a

How to delete "old" directories (recursively)?

Hello,

I have a very deeply nested directory structure which is part of a
loadbuild system. Because our loadbuild machine periodically runs out of
space, I would like to run a scheduled task which deletes old
build artifacts.

So, how can I make a powershell script which deletes (recursively)
directories which are older than X days? I'm not that familiar with the
..NET APIs, which is (I think) why I'm running into trouble on this one.

Thanks for the help. I imagine this is pretty easy for powershell veterans.
  Reply With Quote

Old 04-14-2008   #2
Marco Shaw [MVP]
Guest
 
Posts: n/a

Re: How to delete "old" directories (recursively)?

David Stuart wrote:
Quote:

> Hello,
>
> I have a very deeply nested directory structure which is part of a
> loadbuild system. Because our loadbuild machine periodically runs out of
> space, I would like to run a scheduled task which deletes old
> build artifacts.
>
> So, how can I make a powershell script which deletes (recursively)
> directories which are older than X days? I'm not that familiar with the
> .NET APIs, which is (I think) why I'm running into trouble on this one.
>
> Thanks for the help. I imagine this is pretty easy for powershell veterans.
Here's how I list all directories older than 2008 starting in c:\program
files:
PS C:\Program Files> get-childitem . -rec| `
where-object{$_.psiscontainer -and ($_.lastwritetime -lt "01/01/2008")}

Now to actually delete (just add this to the above command):
|foreach-object{remove-item $_ -recurse -whatif}

-whatif so you can check out what would be deleted. You can do a
start-transcript, run the above with the -whatif, stop-transcript, then
review the log of what should be deleted to make sure if meets your
requirements.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
  Reply With Quote
Old 04-14-2008   #3
ajax76
Guest
 
Posts: n/a

Re: How to delete "old" directories (recursively)?

Get-ChildItem -Recurse -Path .\* |
where{ ($_.mode -match '^d.*') -and
(([datetime]::Today-$_.CreationTime.Date).Days -gt X) } |
%{ Remove-Item $_ }


It's all one line.
Replace X for real value of days and .\* for real path.
  Reply With Quote
Old 04-15-2008   #4
David Stuart
Guest
 
Posts: n/a

Re: How to delete "old" directories (recursively)?

On Mon, 14 Apr 2008 20:30:49 -0700, ajax76 wrote:
Quote:

> Get-ChildItem -Recurse -Path .\* |
> where{ ($_.mode -match '^d.*') -and
> (([datetime]::Today-$_.CreationTime.Date).Days -gt X) } |
> %{ Remove-Item $_ }
>
>
> It's all one line.
> Replace X for real value of days and .\* for real path.
Thanks ..

That did the trick!
  Reply With Quote
 
Reply

Thread Tools
Display Modes









Vistax64.com 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.
© Vistax64.com 2005-2008

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 47 48