#138367 - 2005-04-21 04:37 PM
delete files by date
|
bene
Fresh Scripter
Registered: 2005-03-16
Posts: 13
Loc: Germany
|
hi @ all, i found this thread - but is that really the easiest way to delete files by date?
my problem is:
i have lots of archives to store for a special time (lets say 30 days) those files are located in one single directory (no subfolders). What's the easiest way to delete all files in that directory older than 30 days? thanks, bene
|
|
Top
|
|
|
|
#138368 - 2005-04-21 04:42 PM
Re: delete files by date
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Well, lets think about this. If you want to delete files by date, it is not a trivial task.
First of all, you have to gather the list of file into say an array. Next, you have to get the file dates Ok, now let's determine today's date We'll compare the date of the files and and based on the date given, we'll remove them, otherwse keep them.
HTH,
Kent
|
|
Top
|
|
|
|
#138369 - 2005-04-21 05:02 PM
Re: delete files by date
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
However if you want quick and dirty and you don't mind if the age calculation is out by a couple of days then this will do you:
Code:
Break ON $sDir="C:\TEMP" $sFile=Dir($sDir) While NOT @ERROR AND $sFile<>"" $sFile=$sDir+"\"+$sFile If Not (16 & GetFileAttr($sFile)) $aiFileDate=Split(Split(GetFileTime($sFile))[0],"/") $aiToday=Split(@DATE,"/") If $aiFileDate[0] $iFileAge=(Cint($aiToday[0])-$aiFileDate[0])*365 +(CInt($aiToday[1])-$aiFileDate[1])*30 +(Cint($aiToday[2])-$aiFileDate[2]) $sFile+" is at about "+$iFileAge+" days old."+@CRLF ; ** DELETE YOUR FILES HERE ** EndIf EndIf $sFile=Dir() Loop
If you only delete files whose age is at least 33 days it should be sufficient for you.
|
|
Top
|
|
|
|
#138370 - 2005-04-21 05:02 PM
Re: delete files by date
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
no matter what you do you will need to use one of the date math UDF's.
here is something that i have in my date math udf folder...
Code:
Function EpocDate($date) DIM $y,$m,$d $date = Split($date,"/") If UBound($date) <> 2 Exit 1 EndIf $y=Val($date[0]) $m=Val($date[1]) $d=Val($date[2]) If $m<3 $m=$m+12 $y=$y-1 EndIf $Epocdate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306 EndFunction
it just turns the date into a number of days that have passes since 1/1/1
some code using the above UDF as an example.
Code:
$file = Dir('.\*.*') While not @ERROR
;check to see if we are working witha file. If not (GetFileAttr($file) & 16)
;return the file age in days vs todays date $fileage = epocdate(@DATE) - epocdate(Split(GetFileTime($FILE))[0]) ? 'the File ' + $file + ' is ' + $fileage + ' days old ' GetFileTime($file) EndIf
$file = Dir() Loop
I dont know if it can get any simpler....
|
|
Top
|
|
|
|
#138371 - 2005-04-21 07:29 PM
Re: delete files by date
|
bene
Fresh Scripter
Registered: 2005-03-16
Posts: 13
Loc: Germany
|
hi richard and bryce, thank you, for the great ideas. i tried to understand, what you wrote there. i decided to use batchfiles to create backups and delete old files by using scheduled tasks. it already works! your way is probably the secure way (what if i execute my batch by fault?) so i'll try to change that, when i have more time. thanks for the fast and detailed response!!! bene
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1471 anonymous users online.
|
|
|