Page 1 of 1 1
Topic Options
#138367 - 2005-04-21 04:37 PM delete files by date
bene Offline
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 Offline
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
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#138369 - 2005-04-21 05:02 PM Re: delete files by date
Richard H. Administrator Offline
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 Offline
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 Offline
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
#138372 - 2005-04-21 07:34 PM Re: delete files by date
NTDOC Administrator Online   content
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Bene

I think the issue is that you're too new to the KiXtart scripting language and you probably don't know how to use a UDF (User Defined Function).

Anyways... as you get time try to start reading some of the FAQs in the FAQ forum and the KiXtart manual and you'll soon learn how easy KiX is to use and then wonder how you ever got along without it in your day to day Admin work.

Best wishes and keep reading, experimenting with KiXtart as you have time.

Top
#138373 - 2005-04-27 03:58 AM Re: delete files by date
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Why is using a ready-made UDF not easy?
_________________________
There are two types of vessels, submarines and targets.

Top
#138374 - 2005-04-27 06:15 AM Re: delete files by date
NTDOC Administrator Online   content
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Grasping the concept overall is to a new user. You don't remember since you had a Teacher in school to explain and walk you through many beginning things in College probably many years ago.

For many others programming/scripting is quite new and you don't know ANYTHING about it.

Yes, it is easy. But to someone that isn't even sure how to fire up KiX to read a script - running a UDF is even harder.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1471 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.149 seconds in which 0.119 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org