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 - Deleting old files

Reply
 
Old 10-28-2009   #1 (permalink)
Greg


 
 

Deleting old files

Is there a command to delete all files in a directory that are more than
three days old?

Any help is greatly appreciated.

My System SpecsSystem Spec
Old 10-28-2009   #2 (permalink)
Rob Campbell


 
 

RE: Deleting old files

gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
remove-item -whatif


I'd run it with the -whatif first, and if it looks right, take it off and
re-run.

"Greg" wrote:
Quote:

> Is there a command to delete all files in a directory that are more than
> three days old?
>
> Any help is greatly appreciated.
> .
>
My System SpecsSystem Spec
Old 10-29-2009   #3 (permalink)
Larry__Weiss


 
 

Re: Deleting old files

Do filters like that have to consider a potential execution span that starts
before midnight and finishes after midnight and get coded as below?

$purgedate = (get-date).adddays(-3)
gci "c:\somedir\*.*" | ? {$_.lastwritetime -lt $purgedate}

- Larry


Rob Campbell wrote:
Quote:

> gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
> remove-item -whatif
>
> I'd run it with the -whatif first, and if it looks right, take it off and
> re-run.
>
> "Greg" wrote:
Quote:

>> Is there a command to delete all files in a directory that are more than
>> three days old?
>>
My System SpecsSystem Spec
Old 10-29-2009   #4 (permalink)
Rob Campbell


 
 

Re: Deleting old files

Before or after midnight won't matter. The filter will evaluate whether the
timespan between the lastwritetime and the current datetime is more than 3
days (or 72 hours, or 4320 minutes......).

"Larry__Weiss" wrote:
Quote:

> Do filters like that have to consider a potential execution span that starts
> before midnight and finishes after midnight and get coded as below?
>
> $purgedate = (get-date).adddays(-3)
> gci "c:\somedir\*.*" | ? {$_.lastwritetime -lt $purgedate}
>
> - Larry
>
>
> Rob Campbell wrote:
Quote:

> > gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
> > remove-item -whatif
> >
> > I'd run it with the -whatif first, and if it looks right, take it off and
> > re-run.
> >
> > "Greg" wrote:
Quote:

> >> Is there a command to delete all files in a directory that are more than
> >> three days old?
> >>
> .
>
My System SpecsSystem Spec
Old 10-29-2009   #5 (permalink)
Larry__Weiss


 
 

Re: Deleting old files

Then that might not be what is needed.
I would expect that one would just want calendar days age,
and that the age calculation not be dependent on how much
of the current day has already expired.

That aside, what I was really wanting to know was is the filter
criteria evaluated one time only, or evaluated again and again
as each item is presented for filtering?

- Larry


Rob Campbell wrote:
Quote:

> Before or after midnight won't matter. The filter will evaluate whether the
> timespan between the lastwritetime and the current datetime is more than 3
> days (or 72 hours, or 4320 minutes......).
>
> "Larry__Weiss" wrote:
>
Quote:

>> Do filters like that have to consider a potential execution span that starts
>> before midnight and finishes after midnight and get coded as below?
>>
>> $purgedate = (get-date).adddays(-3)
>> gci "c:\somedir\*.*" | ? {$_.lastwritetime -lt $purgedate}
>>
>> - Larry
>>
>>
>> Rob Campbell wrote:
Quote:

>>> gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
>>> remove-item -whatif
>>>
>>> I'd run it with the -whatif first, and if it looks right, take it off and
>>> re-run.
>>>
>>> "Greg" wrote:
>>>> Is there a command to delete all files in a directory that are more than
>>>> three days old?
>>>>
>> .
>>
My System SpecsSystem Spec
Old 10-29-2009   #6 (permalink)
Rob Campbell


 
 

Re: Deleting old files

I believe it's evaluated for each file as it passes through the pipeline.

On the side note, a quick way to do that kind of calendar day math is:

if ([int]$_.lastwritetime.tooadate() -lt [int](get-date).tooadate() -3){ri $_}

"Larry__Weiss" wrote:
Quote:

> Then that might not be what is needed.
> I would expect that one would just want calendar days age,
> and that the age calculation not be dependent on how much
> of the current day has already expired.
>
> That aside, what I was really wanting to know was is the filter
> criteria evaluated one time only, or evaluated again and again
> as each item is presented for filtering?
>
> - Larry
>
>
> Rob Campbell wrote:
Quote:

> > Before or after midnight won't matter. The filter will evaluate whether the
> > timespan between the lastwritetime and the current datetime is more than 3
> > days (or 72 hours, or 4320 minutes......).
> >
> > "Larry__Weiss" wrote:
> >
Quote:

> >> Do filters like that have to consider a potential execution span that starts
> >> before midnight and finishes after midnight and get coded as below?
> >>
> >> $purgedate = (get-date).adddays(-3)
> >> gci "c:\somedir\*.*" | ? {$_.lastwritetime -lt $purgedate}
> >>
> >> - Larry
> >>
> >>
> >> Rob Campbell wrote:
> >>> gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
> >>> remove-item -whatif
> >>>
> >>> I'd run it with the -whatif first, and if it looks right, take it off and
> >>> re-run.
> >>>
> >>> "Greg" wrote:
> >>>> Is there a command to delete all files in a directory that are more than
> >>>> three days old?
> >>>>
> >> .
> >>
> .
>
My System SpecsSystem Spec
Old 10-29-2009   #7 (permalink)
Larry__Weiss


 
 

Re: Deleting old files

from:
http://msdn.microsoft.com/en-us/libr....tooadate.aspx
..NET Framework Class Library
DateTime..::.ToOADate Method
Converts the value of this instance to the equivalent OLE Automation date.

from:
http://blogs.msdn.com/oldnewthing/ar.../05/54806.aspx
The OLE automation date format is a floating point value, counting days since
midnight 30 December 1899. Hours and minutes are represented as fractional days.

Since PowerShell rounds to nearest int when converting float to integer, won't
that make the filter still be sensitive to current time of day the way you have
coded it?
http://www.johndcook.com/blog/2008/0...in-powershell/

- Larry



Rob Campbell wrote:
Quote:

> I believe it's evaluated for each file as it passes through the pipeline.
>
> On the side note, a quick way to do that kind of calendar day math is:
>
> if ([int]$_.lastwritetime.tooadate() -lt [int](get-date).tooadate() -3){ri $_}
>
> "Larry__Weiss" wrote:
Quote:

>> Then that might not be what is needed.
>> I would expect that one would just want calendar days age,
>> and that the age calculation not be dependent on how much
>> of the current day has already expired.
>>
>> That aside, what I was really wanting to know was is the filter
>> criteria evaluated one time only, or evaluated again and again
>> as each item is presented for filtering?
>>
>>
>> Rob Campbell wrote:
Quote:

>>> Before or after midnight won't matter. The filter will evaluate whether the
>>> timespan between the lastwritetime and the current datetime is more than 3
>>> days (or 72 hours, or 4320 minutes......).
>>>
>>> "Larry__Weiss" wrote:
>>>> Do filters like that have to consider a potential execution span that starts
>>>> before midnight and finishes after midnight and get coded as below?
>>>> $purgedate = (get-date).adddays(-3)
>>>> gci "c:\somedir\*.*" | ? {$_.lastwritetime -lt $purgedate}
>>>>
>>>>
>>>> Rob Campbell wrote:
>>>>> gci "c:\somedir\*.*" |? {$_.lastwritetime -lt (get-date).adddays(-3)} |
>>>>> remove-item -whatif
>>>>> I'd run it with the -whatif first, and if it looks right, take it off and
>>>>> re-run.
>>>>>
>>>>> "Greg" wrote:
>>>>>> Is there a command to delete all files in a directory that are more than
>>>>>> three days old?
>>>>>>
My System SpecsSystem Spec
Old 10-29-2009   #8 (permalink)
Larry__Weiss


 
 

Re: Deleting old files

Then I'd want to save off anything like an offset date that I'd want to
consistently apply to all items while filtering.

- Larry

Rob Campbell wrote:
Quote:

> I believe it's evaluated for each file as it passes through the pipeline.
>
> "Larry__Weiss" wrote:
Quote:

>> ...what I was really wanting to know was is the filter
>> criteria evaluated one time only, or evaluated again and again
>> as each item is presented for filtering?
>>
My System SpecsSystem Spec
Old 10-29-2009   #9 (permalink)
Rob Campbell


 
 

Re: Deleting old files

You're right. Run it close enough to midnight and throw enough files at it,
and it's probably going to break.


"Larry__Weiss" wrote:
Quote:

> Then I'd want to save off anything like an offset date that I'd want to
> consistently apply to all items while filtering.
>
> - Larry
>
> Rob Campbell wrote:
Quote:

> > I believe it's evaluated for each file as it passes through the pipeline.
> >
> > "Larry__Weiss" wrote:
Quote:

> >> ...what I was really wanting to know was is the filter
> >> criteria evaluated one time only, or evaluated again and again
> >> as each item is presented for filtering?
> >>
> .
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Deleting log files VB Script
Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed? PowerShell
Deleting large .avi files and .vob files Vista performance & maintenance
Deleting Files Vista security
deleting files Vista General


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