![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
#2 |
|
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 |
|
|
|
#3 |
|
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 > |
|
|
|
#4 |
|
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 |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |