#59352 - 2001-09-27 05:18 PM
delete files/folders according to their time stamp...
|
Anonymous
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
|
|
|
|
#59354 - 2001-09-27 07:38 PM
Re: delete files/folders according to their time stamp...
|
Anonymous
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
   
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
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
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
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
|
|
|
|
#59361 - 2001-10-02 04:01 PM
Re: delete files/folders according to their time stamp...
|
Anonymous
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
|
|
|
|
#59363 - 2001-10-02 04:38 PM
Re: delete files/folders according to their time stamp...
|
Anonymous
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
   
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
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
|
|
|
|
#59368 - 2001-10-02 05:34 PM
Re: delete files/folders according to their time stamp...
|
Anonymous
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
|
|
|
|
#59370 - 2001-10-02 08:54 PM
Re: delete files/folders according to their time stamp...
|
Anonymous
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
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|