![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||