Page 1 of 1 1
Topic Options
#35307 - 2003-01-17 12:10 AM Delete files older then x days...
rogierg Offline
Fresh Scripter

Registered: 2003-01-16
Posts: 10
I'm making a script to archive and mail some logfiles, it also should delete files older then x days...

I've got two methods and both don't work:

Kix based:
$curryear=substr(@DATE,1,4)
$currmonth=substr(@DATE,6,2)
$currday=substr(@DATE,9,2)
$currdays=((365*$curryear)+((365*$currmonth)/12)+$currday)
$filename=dir("@SCRIPTDIR\*.*")
while $filename<>“” and @ERROR=0
$filedate=getfiletime($filename)
$fileyear=substr($filedate,1,4)
$filemonth=substr($filedate,6,2)
$fileday=substr($filedate,9,2)
$filedays=((365*$fileyear)+((365*$filemonth)/12)+$fileday)
$fileage=$currdays-$filedays
? "$filename is $fileage days old"
$filename=dir()
loop

This code works but not for all files, on a lot of files this gives 00-00-0000 as result. Why?

Also I tried forfiles:
shell "@SCRIPTDIR\forfiles -p$savepath -d-$savedays -v -c""%comspec% -C echo @FILE"""

All variables work, but forfiles doesn't echo files older then $savedays.

forfiles error:
----- DIRECTORY : d:\temp\p-logger\logs
can't execute (error 2)
1 file(s) processed
1 total file(s) processed
C:\WINDOWS\system32\cmd.exe -C echo @FILE

Hope you can help me...

Top
#35308 - 2003-01-17 12:32 AM Re: Delete files older then x days...
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
search the starters forum for 'Dir Tools' only in title ... there is a sample of how to do this even recursively [Wink]
_________________________



Top
#35309 - 2003-01-16 01:17 PM Re: Delete files older then x days...
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear,

An example of the logfile output by running for the following
directories: c:\winnt\help + c:\windows\help
code:
2001/04/05 04:40:15 delete 'c:\winnt\help\31users.FTS' (2001/03/03 05:24:09 size=76800 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\31users.hlp' (1996/10/14 03:38:00 size=58591 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\access.FTS' (2001/03/03 05:24:08 size=94720 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\access.hlp' (1996/10/14 02:38:00 size=32359 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\accessib.chm' (2001/01/14 13:47:18 size=21705 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\accessib.cnt' (2001/01/14 14:35:02 size=121 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\accessib.hlp' (2001/01/14 14:35:02 size=16804 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\acc_dis.cnt' (1996/10/14 03:38:00 size=502 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\acc_dis.hlp' (1996/10/14 03:38:00 size=12643 older 3 days)
.....
2001/04/05 04:40:15 delete 'c:\winnt\help\wangshl.cnt' (1996/10/14 02:38:00 size=102 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wangshl.hlp' (1996/10/14 02:38:00 size=8509 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wordpad.cnt' (1996/10/14 02:38:00 size=1888 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wordpad.hlp' (1996/10/14 02:38:00 size=26793 older 3 days)

An example of the screen output:
code:
check directory 'c:\windows\help'
check directory 'c:\winnt\help'

Informative DELETION: process completed. 112 files deleted + 2 files skipped.

directories checked:
'c:\windows\help'
'c:\winnt\help'
debug output: 'c:\temp\deletion_debug.txt'

The debug output looks:
code:
days         3
current date 2001/04/05
compare date 2001/04/02

check directory 'c:\windows\help'
check directory 'c:\winnt\help'
2001/04/05 04:40:15 delete 'c:\winnt\help\31users.FTS' (2001/03/03 05:24:09 size=76800 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\31users.hlp' (1996/10/14 03:38:00 size=58591 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\access.FTS' (2001/03/03 05:24:08 size=94720 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\access.hlp' (1996/10/14 02:38:00 size=32359 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\accessib.chm' (2001/01/14 13:47:18 size=21705 older 3 days)
-skip- 'c:\winnt\help\accessib.cnt'
-skip- 'c:\winnt\help\accessib.hlp'
2001/04/05 04:40:15 delete 'c:\winnt\help\acc_dis.cnt' (1996/10/14 03:38:00 size=502 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\acc_dis.hlp' (1996/10/14 03:38:00 size=12643 older 3 days)
.....
2001/04/05 04:40:15 delete 'c:\winnt\help\wangocx.cnt' (1996/10/14 02:38:00 size=100 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\wangocx.hlp' (1996/10/14 02:38:00 size=19229 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\wangocxd.cnt' (1996/10/14 02:38:00 size=4415 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\wangocxd.hlp' (1996/10/14 02:38:00 size=291485 older 3 days)
2001/04/05 04:40:15 delete 'c:\winnt\help\wangshl.cnt' (1996/10/14 02:38:00 size=102 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wangshl.hlp' (1996/10/14 02:38:00 size=8509 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wordpad.cnt' (1996/10/14 02:38:00 size=1888 older 3 days)
2001/04/05 04:40:16 delete 'c:\winnt\help\wordpad.hlp' (1996/10/14 02:38:00 size=26793 older 3 days)

Our code is:
code:
;
; NT/95 delete files older then specified number of days - Kixtart 3.62, 3.63, 4.00
;
; (c) drillsergeant, mca - 2001
;
; vs 1.01 - program
;
; 1.00 (20010115) original version
; 1.01 (20010401)
;
CLS
BREAK on
FLUSHKB
MD "c:\temp"

IF (Len(@wksta) <> 0)
$wksta=@wksta
ELSE
$wksta="unknown"
ENDIF

; ---------------------------------------------------------------------------
; - site defined settings -
; ---------------------------------------------------------------------------

$debug_mode="yes" ; - yes/no -
$debug_file="c:\temp\deletion_debug.txt" ; - c:\temp\deletion_debug.txt = file
; - CON = means output to user screen

$days = 7 ; - delete files older than specified number of days -
$log_file="c:\temp\deletion_"+$wksta+".log" ; - append to this file -

$max_directories=2
DIM $remove_dir[$max_directories+1]
$remove_dir[1] = "c:\test1"
$remove_dir[2] = "c:\test2"

; ---------------------------------------------------------------------------
; - main program by DrillSergeant / MCA -
; ---------------------------------------------------------------------------

$temp_file="c:\temp\deletion.tmp"
$log_file=LCASE($log_file)
;
IF ($debug_mode = "yes")
DEL $debug_file
IF ReDirectOutput($debug_file)
ENDIF
ENDIF

; ---------------------------------------------------------------------------
; - Here you control whether files in subdirectories should be
; - or excluded, with the switch "/s".
; - The "/b" switch stands for "base notation", (which excludes
; - everything but the filename), don't change this one!
; ---------------------------------------------------------------------------

GOSUB "calculate_date"
?
IF Open(2,"$log_file",5)
ENDIF
;
$count_skipped=0
$count_delete=0
;
$i = 1
WHILE ($i <= $max_directories)
IF Close(1)
ENDIF
DEL $temp_file
? "check directory '"+$remove_dir[$i]+"'"
$command = '%comspec% /c dir "'+$remove_dir[$i]+'" /s /b >'+$temp_file
SHELL $command
IF (Open(1,$temp_file,2) = 0)
$line = ReadLine(1)
WHILE @error = 0
GOSUB "check_file"
$line = ReadLine(1)
LOOP
ENDIF
$i = $i + 1
LOOP
IF Close(1)
ENDIF
IF Close(2)
ENDIF
IF (Exist($temp_file) = 1)
DEL $temp_file
ENDIF
IF ReDirectOutput("CON")
ENDIF
? "Informative DELETION: process completed. "+
$count_delete+" files deleted + "+
$count_skipped+" files skipped."
?
? "directories checked: "
$i=1
WHILE ($i <= $max_directories)
? " '"+$remove_dir[$i]+"'"
$i=$i+1
LOOP
IF ($debug_mode = "yes")
? "debug output: '"+$debug_file+"'"
ENDIF
EXIT

; ------------------------------------------------------------------------
; - calculate the date (minus $days) by DrillSergeant - -
; ------------------------------------------------------------------------

:calculate_date
$cury = @year
$curm = @monthno
$curd = @mdayno
IF $curm < 3
$curm = $curm + 12
$cury = $cury - 1
ENDIF
$curint = $curd + ( 153 * $curm - 457 ) / 5 + 365 * $cury + $cury / 4 - $cury / 100 + $cury / 400 - 306
$myz = $curint - $days + 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
IF Len("$Rd_Month") = 1
$rd_month = "0" + "$Rd_Month"
ENDIF
IF Len("$Rd_Day") = 1
$rd_day = "0" + "$Rd_Day"
ENDIF
$compare_date = "$Rd_Year/$Rd_Month/$Rd_Day"
;
IF ($debug_mode = "yes")
? "days "+$days
? "current date "+@date
? "compare date "+$compare_date
ENDIF
RETURN

; ------------------------------------------------------------------------
; - check file for yes/no for deletion by DrillSergeant / MCA -
; ------------------------------------------------------------------------

:check_file
$time = GetFileTime($line)
IF ($time > "")
IF (Substr($time,1,10) < $compare_date)
$log_info=@date+" "+@time+" delete '"+$line+"'"+
" ("+GetFileTime($line)+
" size="+GetFileSize($line)+
" older "+$days+" days)"
IF WriteLine(2,$log_info+Chr(13)+Chr(10))
ENDIF
IF ($debug_mode = "yes")
? $log_info
ELSE
DEL $line ; <--------------------- delete files ------
ENDIF
$count_delete=$count_delete+1
ELSE
$log_info=" -skip- '"+$line+"'"
IF ($debug_mode = "yes")
? $log_info
ENDIF
$count_skipped=$count_skipped+1
ENDIF
ENDIF
RETURN

This please of code you can modify (site defined settings):
code:
 $debug_mode="yes"                        ; - yes/no -
$debug_file="c:\temp\deletion_debug.txt" ; - c:\temp\deletion_debug.txt = file
; - CON = means output to user screen

$days = 7 ; - delete files older than specified number of days -
$log_file="c:\temp\deletion_"+$wksta+".log" ; - append to this file -

$max_directories=2
DIM $remove_dir[$max_directories+1]
$remove_dir[1] = "c:\test1"
$remove_dir[2] = "c:\test2"

The script may give you an idea.
Greetings.

btw: change $debug_mode="yes" to "no" for really file
deletion.

btw: on the UDF forum all kind of UDFs are available to reduce some of above code.
Above code works for kixtart 3.6x and 4.x releases.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#35310 - 2003-01-16 02:59 PM Re: Delete files older then x days...
rogierg Offline
Fresh Scripter

Registered: 2003-01-16
Posts: 10
All the examples don't work. Some files are okay but the files I really want to process still have no date. The files are ZIP files created with pkzip 2.50 for DOS, could this be the problem? My guess is the getfiletime uses a variable in the file being processed, not the file system time/date...
Top
#35311 - 2003-01-16 03:05 PM Re: Delete files older then x days...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See also CleanTempFiles() - Cleans up the temporary directories (or any arbitrary directory) .

You should also take a look at the UDF Forum in general as it contains various UDFs with regards to DIR and date manipulation.

And please post code in between [CODE] tags as described in ABC's of KiXtart board etiquette and message to new forum users
_________________________
There are two types of vessels, submarines and targets.

Top
#35312 - 2003-01-16 03:22 PM Re: Delete files older then x days...
Jochen Administrator Offline
KiX Supporter
*****

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

das war jetzt das zweite Mal, das Ich auf diese Frage mit DirPlus() in Verbindung mit DateCalc() (link auf ein altes Topic) geantwortet habe, un Du mit CleanTempFiles() ... Wir sollten uns angewöhnen auf beides hinzuweisen [Big Grin]

Sorry for starting to talk German, but I always wanted to do this [Razz]
_________________________



Top
#35313 - 2003-01-16 03:35 PM Re: Delete files older then x days...
Jochen Administrator Offline
KiX Supporter
*****

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

by the way: I might think that CleanTempFiles() as well fails on that zip files Rogier mentioned as it uses getfiletime() too
_________________________



Top
#35314 - 2003-01-16 03:46 PM Re: Delete files older then x days...
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Sory for jumping in without reading all the details... not clear on what the real issue is.

If the problem is not getting the right date or the failure due to files in use, look to FSO for a solution.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#35315 - 2003-01-16 03:51 PM Re: Delete files older then x days...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Also, wenn du Deutch sprechen willst, dann is das kein Problem. Noch habe ich nicht so viel vergessen ;-) Allerdings muss ich manchmal doch schon etwas nachdenken, z.B. wollte meine Frau das deutsche Wort fuer 'desk' wissen, wobei mir nur 'Tisch' und 'Arbeitstisch' einfiel, nicht aber 'Schreibtisch' [Embarrassed] Hat ungefahr fuenf Minuten gedauert bis es mir wieder einfiel.

Anyway, I always thought that GetFileTime() retrieves the filetime associated with the filename as stored as part of the file system, e.g. FAT, FAT32, or NTFS. NTFS has the additional ability to store a LastAccessedTime in addition to the LastModifiedTime (and the FileCreationTime ?)
_________________________
There are two types of vessels, submarines and targets.

Top
#35316 - 2003-01-16 03:55 PM Re: Delete files older then x days...
Jochen Administrator Offline
KiX Supporter
*****

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

lets see what Rogier gets from fso ... there was a bunch of UDFs for this ... Chris ?
_________________________



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 451 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.129 seconds in which 0.104 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