Page 1 of 2 12>
Topic Options
#59352 - 2001-09-27 05:18 PM delete files/folders according to their time stamp...
Anonymous
Unregistered


Hi there, is there a way to delete files and folders according to the date they were created? We have a new policy on our ftp site that if there is a file/folder older than 60days then it will be deleted. Unfortunatly, we don't have time to babysit and look on all individual files/folders and delete them... Please give me a sample code to start on, thanks
Top
#59353 - 2001-09-27 06:05 PM Re: delete files/folders according to their time stamp...
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
jvd,

have a look in this Topic

J.

_________________________



Top
#59354 - 2001-09-27 07:38 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


Hi jpols, looking at shawns code, I can say that he's comparing two files and determining which one is older.

Here's my question..

Instead of doing this:

quote:

$file1 = "C:\KIX32\KIXREF.EXE"
$file2 = "C:\KIX32\KIX32.EXE"


Can you do this: ??
quote:

$files_folders = "C:\KIX32\*.*"

[ 27 September 2001: Message edited by: jvd626 ]

Top
#59355 - 2001-09-27 08:21 PM Re: delete files/folders according to their time stamp...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
J. with permission ...

One has to check a file at a time, here's an example that enumerates a folder (no recursion) and dumps all files older than (in this case) thirty days ... is this kinda what your looking for ?

code:

break on


$folder = "c:\winnt\system32"


$daysold = 30 ; check for files older than this many days


$today = @DATE
$rd_year = val(substr("$today",1,4))
$rd_month = val(substr("$today",6,2))
$rd_day = val(substr("$today",9,2))


gosub "intdate" ; get the internalized date


$check_date = $rd_internal - $daysold ; calculate the date to check against


$filename = dir("$folder")


while $filename <> "" and @error = 0


if (getfileattr($filename) & 16 ) = 0 ; not a dir ...


$time = getfiletime("$folder\$filename")
$rd_year = val(substr("$time",1,4))
$rd_month = val(substr("$time",6,2))
$rd_day = val(substr("$time",9,2))


gosub "intdate"


$file_date = $rd_internal


if $file_date < $check_date


?"$filename older than $daysold days..."


endif


endif


$filename = dir() ; one mo time


loop


exit 1


;=======
:intdate
;=======
;
; Library routine to convert a date from Calendar to internal format.
;
; The algorithm used here is taken directly from the following document:
;
;
; Variables passed:
; $rd_day IN Day of month (0-31)
; $rd_month IN Month of year (1-12)
; $rd_year IN Full year
; $rd_internal OUT Internal representation
;
; Amendment history:
; Version 1.0 21 June 2000 Richard Howarth
;
GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal
DIM $MyYear,$MyMonth
$MyYear=$rd_year
$MyMonth=$rd_month
if $MyMonth < 3
$MyMonth = $MyMonth + 12
$MyYear = $MyYear - 1
endif
$rd_internal = $rd_day + ( 153 * $MyMonth - 457 ) / 5 + 365 * $MyYear +
$MyYear / 4 - $MyYear / 100 + $MyYear / 400 - 306
RETURN

;=======
:extdate
;=======
; Library routine to convert a date from internal to Calendar format.
;
; The algorithm used here is taken directly from the following document:
;
; Variables passed:
; $rd_day OUT Day of month (0-31)
; $rd_month OUT Month of year (1-12)
; $rd_year OUT Full year
; $rd_internal IN Internal representation
;
; Amendment history:
; Version 1.0 21 June 2000 Richard Howarth
;
GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal
DIM $MyZ,$MyH,$MyA,$MyB,$MyC
$MyZ=$rd_internal + 306
$MyH=100*$MyZ-25
$MyA=$MyH/3652425
$MyB=$MyA-$MyA/4
$rd_year=(100*$MyB+$MyH)/36525
$MyC=$MyB+$MyZ-365*$rd_year-$rd_year/4
$rd_month=(5*$MyC+456)/153
$rd_day=$MyC-(153*$rd_month-457)/5
if $rd_month > 12
$rd_year=$rd_year + 1
$rd_month = $rd_month - 12
endif
RETURN


-Shawn

[ 27 September 2001: Message edited by: Shawn ]

Top
#59356 - 2001-09-27 08:33 PM Re: delete files/folders according to their time stamp...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Try this one:

It deletes all files older than x days in the specified directory and all subdirectories. Additionally, one can specify specific file filters like *.tmp.

Jens

code:

;NAME CLEANTEMPFILES
;
;ACTION Cleans up the temporary directory
;
;SYNTAX CLEANTEMPFILES(DIRECTORY,DAYS)
;
;PARAMETERS DIRECTORY
; Required string containing the directory to be cleaned
;
; DAYS
; Required integer indicating how old a file must be before it is deleted
;
;REMARKS The function will recursively delete all subdirectories and files
; Requires the UDFs FULLFILE, DATEMATH
;
;RETURNS 0 if successful, otherwise error code
;
;EXAMPLE $retcode=cleantempfiles('c:\temp',7)
;
function cleantempfiles($tempdir, $olderas)
Dim $retcode, $filter[10], $filetime, $timediff, $filename, $filefilter

$filter[0]='_*.*'
$filter[1]='*.tmp'
$filter[2]='~*.*'
$filter[3]='vbe'
$filter[4]='*.exd'
$filter[5]='Acrobat Distiller 5'
$filter[6]='*.log'
$filter[7]='{*}.*'
$filter[8]='*.tmp.gif'
$filter[9]='cddb'
$filter[10]='*.dir'

if exist($tempdir)
for each $filefilter in $filter
? 'Filtering '+$tempdir+' for '+$filefilter
$filefilter=fullfile($tempdir,$filefilter)
$filename=dir($filefilter,1)
While $filename<>'' and @ERROR = 0
if $filename<>'.' and $filename<>'..'
$filename=fullfile($tempdir,$filename)
if exist($filename)
$filetime=substr(getfiletime($filename),1,10)
$timediff=datemath(@DATE,$filetime)
if $timediff>$olderas
if getfileattr($filename) & 16
$retcode=cleantempfiles($filename,$olderas)
rd $filename
? 'Deleting obsolete directory '+$filename
else
del $filename
? 'Deleting obsolete file '+$filename
endif
endif
endif
endif
$filename=Dir('',1)
loop
next
endif
$cleantempfiles=@ERROR
endfunction
;NAME FULLFILE
;
;ACTION Creates a fully qualified path
;
;SYNTAX FULLFILE(PATH1, PATH2)
;
;PARAMETERS PATH1
; Required string containing pathname
;
; PATH2
; Required string containing pathname or filename
;
;RETURNS The fully qualified path or filename
;
;REMARKS none
;
;EXAMPLE $file = fullfile('c:\windows','system32\kixtart.exe')
; $file = 'c:\windows\system32\kixtart.exe'
;
function fullfile($path1, $path2)

if substr($path1,len($path1),1)='\'
$path1=substr($path1,1,len($path1)-1)
endif

if substr($path2,1,1)='\'
$path2=substr($path2,2,len($path2)-1)
endif

$fullfile=$path1+'\'+$path2

endfunction
;NAME DATEMATH
;
;ACTION Performs mathematical computations on a date or computes days between two dates
;
;SYNTAX DATEMATH(DATE1,DATE2)
; or
; DATEMATH(DATE1,DAYS)
;
;PARAMETERS DATE1
; Required string containing date in the form of "YYYY/MM/DD"
;
; DATE2 or INTEGER
; Required string containing date in the form of "YYYY/MM/DD" or an integer (positive or negative)
;
;REMARKS Date parameters must be in the form of YYYY/MM/DD.
; Requires the UDF SERIALDATE
;
;RETURNS If two dates are passed, the number of days between them is returned as in integer.
; If a date is passed as "date1" and a number of days is passed as integer, the resulting
; date will be returned in the form of YYYY/MM/DD.
;
;EXAMPLE $retcode=datemath('2001/07/31','2001/07/01')
;
function datemath($ExpD1,$ExpD2)
; if both parameters are dates (yyyy/mm/dd), a positive
; integer indicating the days between the two dates will be returned.
; if the first is a date (yyyy/mm/dd) and the second is an integer
; (number of days), a new date will be returned.
; UDF dependencies: SerialDate, abs
select
case instr($ExpD1,'/') and instr($ExpD2,'/')
; two date passed - return daysbetween integer
$DateMath=serialdate($ExpD1)-serialdate($ExpD2)
if $DateMath<0
$DateMath=$DateMath*(-1)
endif
case instr($ExpD1,'/') and 1-instr($ExpD2,'/')
; date and a number passed - return date
$ExpD2=0+$ExpD2
$Datemath=serialdate(serialdate($ExpD1)+$ExpD2)
case 1 ; incorrect parameters passed
endselect
endfunction

;NAME SERIALDATE
;
;ACTION Convert dates to numbers (and back) for the purpose of performing date math
;
;SYNTAX SERIALDATE(DATE)
;
;PARAMETERS DATE or NUMBER
; if a date is used, it must be in the form of "YYYY/MM/DD"
; if a number is used, it must be a number previously derived from this function.
;
;REMARKS This function was developed as a core routine for the DateMath( ) function. In
; normal usage, you would most like just use the DateMath( ) function which depends
; on this function.
; Algorithms used in the development of this routine were obtained from:
; http://www.capecod.net/~pbaum/date/date0.htm
;
;RETURNS If a date is passed to this function, the function returns a number. If a number is
; passed to this function, a date "YYYY/MM/DD" is returned
;
;EXAMPLE $retcode=serialdate('2001/07/01')
;
function serialdate($ExpD)
; parameter ($ExpD) must be a date (in the form of yyyy/mm/dd)
; or an integer previously derived by this function.
; if passed a date, it returns an integer.
; If passed an integer, it returns the date.
; Integers can be used for general-purpose math computations on dates
; and then converted back to dates by calling this function again.
; Algorithms were obtained from: http://www.capecod.net/~pbaum/date/date0.htm
dim $z,$h,$a,$b,$c,$y,$m,$d
if instr($ExpD,'/') ; date passed
$y=val(substr($ExpD,1,4))
$m=val(substr($ExpD,6,2))
$d=val(substr($ExpD,9,2))
if $m<3
$m=$m+12
$y=$y-1
endif
; return an integer
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
else ; integer passed
$z=0+$ExpD+306 ; force numeric
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
if $m>12
$y=$y+1
$m=$m-12
endif
if $m<9
$m='0'+$m
endif
if $d<9
$d='0'+$d
endif
; return a string date
$SerialDate=''+$y+'/'+$m+'/'+$d
endif
endfunction


[ 27 September 2001: Message edited by: sealeopard ]

_________________________
There are two types of vessels, submarines and targets.

Top
#59357 - 2001-09-27 09:24 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


Thanks guys, you guys are great and fast! I'll try it out and let you know...
Top
#59358 - 2001-09-27 10:15 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


Ok,

Shawn: I tried running the script and it just shows me: "system32 older than 30 days..." Is that the only thing it does? Do I have to add more stuff to it in order to work? I know i have to change the c:\winnt\system32 directory to something else...

Sealeopard: I tried running the script and it gives me this error:

quote:

Script error : unknown command !.
function cleantempfiles($tempdir, $olderas)

I know I'm the dumbest person when it comes to script, I hope you guys can shed a light...

Top
#59359 - 2001-09-27 10:46 PM Re: delete files/folders according to their time stamp...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
oops, should be ...

$folder = "c:\winnt\system32\*.*"

guess I was over-zealous when editing code for post ... Jens script requires KiXtart Version 4.0 (2001) ...

-Shawn

Top
#59360 - 2001-09-28 02:46 AM Re: delete files/folders according to their time stamp...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
OOOPPPSSS:

Was there even a Kixtart version below 4.0? I'm so used to all those UDFs I can't imagine how I survived without them.

sorry, yes, the functions require Kixtart 4.0.

Jens

_________________________
There are two types of vessels, submarines and targets.

Top
#59361 - 2001-10-02 04:01 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


jens I can't seem to make it work. How does it delete the files? Can you give me an example folder path?

for example:

quote:

[path]
c:\ftpfolder\*.*

delete everything *.* in this folder if they are over 60days old(according to their creation date)

[exception]
except this folder c:\ftpfolder\jack\


shawn, yours worked but I'm not sure how to make it delete the files/folders. It just shows its 30days old and does nothing else...am I missing something here??

Top
#59362 - 2001-10-02 04:05 PM Re: delete files/folders according to their time stamp...
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Huh ? What ?

did I missed something ?
Totaly lost this topic ...

wasn't there a udf of Bryce ? Or was this Foldersize ????

Jochen

_________________________



Top
#59363 - 2001-10-02 04:38 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


whats udf? is that a feature on the new version of kix 4.0? I'm still using version 3.63.

Hey joche, is that "huh? what?" to me? Cause I'm not really good in english so I'm probably wrong in my grammar

Top
#59364 - 2001-10-02 04:46 PM Re: delete files/folders according to their time stamp...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
JV,

That huh ? is a what ? to everyone ... or up here in the Great White North we say Eh ?

I would never post any code that actually did a real DELETE of files ... the intent was that one should replace the ...

if $file_date < $check_date
?"$filename older than $daysold days..."
endif

... bit with a line that actually deletes $filename ... by the way, did you mention whether you wanted to delete files more than one level (folder) deep (recursion) ... ?

-Shawn

p.s. I think Jochen's been working on Euro-conversion too long ... pulling all-nighters and losing sleep ...

[ 02 October 2001: Message edited by: Shawn ]

Top
#59365 - 2001-10-02 04:52 PM Re: delete files/folders according to their time stamp...
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
udf = user defined function (KiX 2001, yes)

I would recommend to use Shawn's code (3rd reply) only with a modification:

instead of:

?"$filename older than $daysold days..."

do :

del $filename

Only thing left then is to decide what are the exceptions...

J.

_________________________



Top
#59366 - 2001-10-02 04:54 PM Re: delete files/folders according to their time stamp...
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Shawn's got his old speed-o-light reply speed back

Yes, maybe not the lack of sleep but the puzzling of my bio-rythm ...

J.

_________________________



Top
#59367 - 2001-10-02 05:02 PM Re: delete files/folders according to their time stamp...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
code:

This is a biorythm ...



_ _ _ _ _ _
\ / \ / \ / \ / \ / \
- - - - - -



This is a biorythm on EuroConversion ...



-----------------------



-Shawn

[ 02 October 2001: Message edited by: Shawn ]

Top
#59368 - 2001-10-02 05:34 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


hi shawn, I'll try to use your code. And yes, I want to delete EVERY folders/files beyond this directory c:\ftpfolder\ It doesn't matter how deep it is...

Though I want the code to have an exception to "leave alone" or not delete folders(and folder/files within it) in the c:\ftpfolder\ directory? I hope this make sense...

Top
#59369 - 2001-10-02 05:53 PM Re: delete files/folders according to their time stamp...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hey j

yap - then you'll need to combine this code snippet with another one that recurses through the directory structure. If memory serves, Eric (kholm) and Bryce (Bryce) have posted script fragments to do this ...

Bryce used a strategy whereby he shelled out to the DIR command, piped the result to a file, then read it back into his script ... then for each file, deleted it ...

Erik posted a script that used the DIR() function and recursive gosub calls ...

All one really needs to do is take one of these two scripts and stitch it together with the date delete code to have one heck of a KiXtart 3.63 folder cleanup script ... hopefully one of these two guys can digup their scripts for you ...

-Shawn

Top
#59370 - 2001-10-02 08:54 PM Re: delete files/folders according to their time stamp...
Anonymous
Unregistered


Wow, I thought this would be an easy script to make. I guess in some situation, you'd have to compile it to an executable huh? I hope bryce or eric has a solution...
Top
#59371 - 2001-10-02 09:28 PM Re: delete files/folders according to their time stamp...
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
I would love to help... but they have me chained to a desk in the back room formatting PC's! aggh I must go before they see that i am gone!


But really, you should easily be able to take shawns code and feed it filenames from a recursive shelled DIR command.

code:

$folder = "c:\diablo"
shell '%comspec% /c dir $folder /s/b/a-d > %temp%\temp.dat'
$nul = open(1,"%temp%\temp.dat",2)
$file = readline(1)
do
? $file
$file = readline(1)
until @error <> 0

Bryce

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 657 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.105 seconds in which 0.061 seconds were spent on a total of 13 queries. Zlib compression enabled.

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