TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

keep deleting oldest file in a directory

Microsoft Windows Powershell


Reply
 
LinkBack Thread Tools Display Modes
Old 25-04-2008, 09:49 AM   #1
Rob Pettrey
Guest
 
Posts: n/a
keep deleting oldest file in a directory

I'm looking for the easiest way to delete the oldest file in a backup
directory.

Right now, I keep deleting the oldest file until I have enough space:

if ($backup_need -gt $backup_free)
{ do
{ dir $backup_dir | where {$_.lastwritetime -lt $cutoff_date} | remove-item
-force
$cutoff_date = $cutoff_date.adddays(+1)
if ($cutoff_date.date -eq $today.date) {break}
$backup_free = (get-WMIObject win32_LogicalDisk | where {$_.DeviceID
-match $backup_disk}).freespace/1gb
}
until ($backup_free -gt $backup_need)


Is there a simpler way to do this? Like: :^)

dir $backup_dir | where {$_.lastwritetime is the oldest file} | remove-item
-force
--or--
dir "f:\backup files\*.bkf" | sort -property lastwritetime | remove-item
-oldest

Is there some PowerShell command that just gives me the oldest file? or the
oldest date?

Is there a simpler way to do this? That is still readable?

--
Rob Pettrey
Microsoft Small Business Spe******t
  Reply With Quote
Old 25-04-2008, 05:51 PM   #2
Marco Shaw [MVP]
Guest
 
Posts: n/a
Re: keep deleting oldest file in a directory



> dir "f:\backup files\*.bkf" | sort -property lastwritetime | remove-item
> -oldest


Try just insert a select statement in the above like so:
....sort lastwritetime|select -first 1|remove-item -whatif

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 25-04-2008, 06:51 PM   #3
Rob Pettrey
Guest
 
Posts: n/a
Re: keep deleting oldest file in a directory

Brilliant!

Here is the greatly simplified routine, much more elegant:

if ($backup_need -gt $backup_free)
{ do
{ dir $backup_files | sort lastwritetime | select -first 1 | remove-item
-force
$backup_free = (get-WMIObject win32_LogicalDisk | where {$_.DeviceID
-match $backup_disk}).freespace/1gb
}
until ($backup_free -gt $backup_need)
}


Now, the next question: is it possible to simplify getting .freespace? The
only way I could find to update .freespace was to get the object again.
--
Rob Pettrey
Microsoft Small Business Spe******t


"Marco Shaw [MVP]" wrote:

>
>
> > dir "f:\backup files\*.bkf" | sort -property lastwritetime | remove-item
> > -oldest

>
> Try just insert a select statement in the above like so:
> ....sort lastwritetime|select -first 1|remove-item -whatif
>
> 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 25-04-2008, 08:59 PM   #4
Kiron
Guest
 
Posts: n/a
Re: keep deleting oldest file in a directory

[io.driveInfo]$cDrive = 'c'
$backup_free = $cDrive.availableFreeSpace
if ($backup_need -gt $backup_free) {
$difference = $backup_need - $backup_free
dir $backup_dir |
sort lastWriteTime |
% {
$difference -= $_.length
# -whatIf for testing
remove-item $_.psPath -whatIf
if ($difference -le 0) {break}
}
}

--
Kiron
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 04:04 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional