Zakonski
|
(Fresh Scripter)
|
2005-08-05 04:27 PM
|
|
|
|
|
Count files and filesize, then remove files older then 1 day
|
|
I've read a post about deleting files which were older then 30 days. I've tried to edit the script, but it didn't work. Here's what I want: For example, it's the 5th of July 2005. There's a folder ..\TMP. I want to count the total of files which are older then the 5th of July 2005 I want to count the total size of files which are older then the 5th of July 2005 The result may be printed on the screen, or to a file. After that it needs to delete all the files, older then the 5th of July 2005.
Any suggestions? My skills with scripting are not that good, so type slowly please
|
Co
|
(MM club member)
|
2005-08-05 09:03 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Zakonski,
Welcome on board!
I think you can make it yourself much easier when you copy all files older then a specific date to another folder. Count ALL the files in this folder and delete them when you're finished.
This must be possible for a newbie scripter to create... Read the reference and search the board. That's a good training to learn how to script.
Good Luck!!
|
Bryce
|
(KiX Supporter)
|
2005-08-05 11:03 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
take a look at the UDF's DIRPlus() and FlipCTime() and TimeConvert().
ahh... waht the hell... silver platter time
Code:
Call 'g:\kix\udf\dirplus\DirPlus.kix' Call 'g:\kix\udf\FlipcTime\flipcTime.kix' Call 'g:\kix\udf\TimeConvert\TimeConvert.kix'
DIM $DeletedFiles[0]
;get and array of all file on the folder $files = Dirplus("c:\temp",'/a-d /s')
for each $file in $files ;the current date/time of the file $TS = split($file.datelastmodified)
;need to format the date/time into usable values
;this tunes the time in to Milltary time $ts[1] = timeconvert($ts[1]+$ts[2])
;this puts the date into a yyyy/mm/dd format $ts[0] = split($ts[0],'/') $ts[0] = $ts[0][2]+'/'+$ts[0][0]+'/'+$ts[0][1]
;the age of the file in seconds since 1970/1/1 $ts = flipctime($ts[0],$ts[1])
;the current number of seconds since 1970/1/1 $currentTime = flipctime(@DATE,@TIME)
;Check to the the number of seconds that have elapsed since the .lastdatemodified time on the file ;of longer than 24hours or 86400 seconds, the file is old enough. If $currentTime - $ts > 86400 ;add the wanted information to the $deletedFiles Array $deletedFiles[ubound($deletedFiles)] = $file ;resize the array by 1 redim preserve $deletedFiles[ubound($DeletedFiles)+1] EndIf next
If ubound($deletedFiles) > 0 ;need to trim down the size of the array by one. redim preserve $deletedFiles[ubound($deletedFiles)-1] Else ;no files were found that were older than 1 day $deletedFiles=0 EndIf
"Number of Files that are older than 1 day = " + (ubound($DeletedFiles)+1)
;add up the size of the files to be deleted for each $file in $DeletedFiles $deletedFileSize = $file.size+$deletedFileSize next
? "Total Size of files to be deleted = " $deletedFileSize
;time to delete the files, you will neede to uncomment this... ;because it WILL delete the files!! ;for each $file in $deletedFiles ; $file.delete ;next
|
Zakonski
|
(Fresh Scripter)
|
2005-08-06 02:19 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Thanks for helping Bryce, when the directory is empty, it says:
Number of files older then 1 day = 0 Total size of files to be deleted =
When I place some temp files in de TEMP dir, it says:
ERROR : array reference out of bounds! Script: C:\Documents and Settings\mzi\Bureaublad\KIX\testomgeving\monitorentempfiles.kix Line : 19
?
|
Co
|
(MM club member)
|
2005-08-06 03:58 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Hmm, I have that too. It has something to do with the Dirplus() UDF. I still can't figure out what caused this problem...
|
Lonkero
|
(KiX Master Guru)
|
2005-08-06 05:24 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
you have some extra time? try with other dir udfs... like dirscan()
|
Bryce
|
(KiX Supporter)
|
2005-08-06 05:36 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
think i know, could you paste what line 19 is please?
and whiel you are at it, toss a
? $file.datelastmodified
and paste that too.
|
Co
|
(MM club member)
|
2005-08-06 08:57 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
line 19: $ts[1] = timeconvert($ts[1]+$ts[2])
? $file.datelastmodified gives: 8-7-2004 16:13:56 (Dutch Windows XP which means 8-7-2004 = d-m-yyyy)
|
Co
|
(MM club member)
|
2005-08-06 09:01 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Quote:
...Script: C:\Documents and Settings\mzi\Bureaublad\KIX\testomgeving\monitorentempfiles.kix Line : 19...
Zakonski also uses a Dutch OS...
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Or use the ready-made CleanDirectory() - Cleans up the temporary directories (or any arbitrary directory) UDF.
|
Bryce
|
(KiX Supporter)
|
2005-08-07 06:58 AM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Quote:
Quote:
...Script: C:\Documents and Settings\mzi\Bureaublad\KIX\testomgeving\monitorentempfiles.kix Line : 19...
Zakonski also uses a Dutch OS...
AHH HA!
it seems that .datelastmodified returns the date/time in the format that is given by the OS.
a ? %file.datelastmodified returns "8/6/2005 11:37 pm" on my system m-d-yyyy
ok based off what you told me...
1. you dont need this line. delete or comment out. and you dont need the timeconvert() UDF Code:
$ts[1] = timeconvert($ts[1]+$ts[2])
2. need to change this Code:
$ts[0] = split($ts[0],'/') $ts[0] = $ts[0][2]+'/'+$ts[0][0]+'/'+$ts[0][1]
to
Code:
$ts[0] = split($ts[0],'-') $ts[0] = $ts[0][2]+'/'+$ts[0][1]+'/'+$ts[0][0]
Bryce
|
Bryce
|
(KiX Supporter)
|
2005-08-07 07:00 AM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Quote:
Or use the ready-made CleanDirectory() - Cleans up the temporary directories (or any arbitrary directory) UDF.
Jens,
I first thought that cleandirectory() world work for him too, but he was also wanting the total size of the deleted files.
|
Co
|
(MM club member)
|
2005-08-07 10:23 AM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Bryce,
The error is gone but the files aren't deleted...
Quote:
8-7-2004 16:13:56
8-7-2004 16:05:36
31-1-2005 22:55:06
6-8-2005 15:28:38
7-8-2005 10:06:00
6-8-2005 15:28:45
6-8-2005 15:24:48Number of Files that are older than 1 day = 0 Total Size of files to be deleted =
|
Co
|
(MM club member)
|
2005-08-07 10:49 AM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
I've added these lines:
? "current time: "$currentTime ? "ts: "$ts ? "current time - ts: "$currenttime - $ts
It gives:
Quote:
8-7-2004 16:13:56 current time: 1123410717 ts: 1089303236 current time - ts: 34107481
8-7-2004 16:05:36 current time: 1123410717 ts: 1089302736 current time - ts: 34107981
31-1-2005 22:55:06 current time: 1123410717 ts: 1107212106 current time - ts: 16198611
6-8-2005 15:28:38 current time: 1123410717 ts: 1123342118 current time - ts: 68599
7-8-2005 10:06:00 current time: 1123410717 ts: 1123409160 current time - ts: 1557
6-8-2005 15:28:45 current time: 1123410717 ts: 1123342125 current time - ts: 68592
6-8-2005 15:24:48 current time: 1123410718 ts: 1123341888 current time - ts: 68830Number of Files that are older than 1 day = 0 Total Size of files to be deleted =
There are three files newer than one day. All others should be deleted.
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
It would be easy for him to modify the CleanDirectory() UDF to suit his needs.
|
Zakonski
|
(Fresh Scripter)
|
2005-08-07 03:35 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Bryce, it works fantastic! Thank you very much!
|
NTDOC
|
(KiX Master)
|
2005-08-07 09:17 PM
|
|
|
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Bryce,
When I add this to the mix I get bunch of errors
Code:
$SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On')
|
|
Re: Count files and filesize, then remove files older then 1 day
|
|
Doc, you forgot our newest addition! Code:
$SO=SetOption('NoMacrosInStrings','On')
|