Page 1 of 1 1
Topic Options
#144831 - 2005-08-05 04:27 PM Count files and filesize, then remove files older then 1 day
Zakonski Offline
Fresh Scripter

Registered: 2005-08-05
Posts: 5
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

Top
#144832 - 2005-08-05 09:03 PM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
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!!


Edited by Co (2005-08-05 09:05 PM)
_________________________
Co


Top
#144833 - 2005-08-05 11:03 PM Re: Count files and filesize, then remove files older then 1 day
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3164
Loc: Houston TX
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


Top
#144834 - 2005-08-06 02:19 PM Re: Count files and filesize, then remove files older then 1 day
Zakonski Offline
Fresh Scripter

Registered: 2005-08-05
Posts: 5
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

?

Top
#144835 - 2005-08-06 03:58 PM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
Hmm, I have that too. It has something to do with the Dirplus() UDF. I still can't figure out what caused this problem...
_________________________
Co


Top
#144836 - 2005-08-06 05:24 PM Re: Count files and filesize, then remove files older then 1 day
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22333
Loc: OK
you have some extra time?
try with other dir udfs...
like dirscan()
_________________________
!

download KiXnet

Top
#144837 - 2005-08-06 05:36 PM Re: Count files and filesize, then remove files older then 1 day
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3164
Loc: Houston TX
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.

Top
#144838 - 2005-08-06 08:57 PM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
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


Top
#144839 - 2005-08-06 09:01 PM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
Quote:


...Script: C:\Documents and Settings\mzi\Bureaublad\KIX\testomgeving\monitorentempfiles.kix
Line : 19...





Zakonski also uses a Dutch OS...


Edited by Co (2005-08-06 09:05 PM)
_________________________
Co


Top
#144840 - 2005-08-07 05:05 AM Re: Count files and filesize, then remove files older then 1 day
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
Or use the ready-made CleanDirectory() - Cleans up the temporary directories (or any arbitrary directory) UDF.
_________________________
There are two types of vessels, submarines and targets.

Top
#144841 - 2005-08-07 06:58 AM Re: Count files and filesize, then remove files older then 1 day
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3164
Loc: Houston TX
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

Top
#144842 - 2005-08-07 07:00 AM Re: Count files and filesize, then remove files older then 1 day
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3164
Loc: Houston TX
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.

Top
#144843 - 2005-08-07 10:23 AM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
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


Top
#144844 - 2005-08-07 10:49 AM Re: Count files and filesize, then remove files older then 1 day
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1338
Loc: NL
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.
_________________________
Co


Top
#144845 - 2005-08-07 02:00 PM Re: Count files and filesize, then remove files older then 1 day
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
It would be easy for him to modify the CleanDirectory() UDF to suit his needs.
_________________________
There are two types of vessels, submarines and targets.

Top
#144846 - 2005-08-07 03:35 PM Re: Count files and filesize, then remove files older then 1 day
Zakonski Offline
Fresh Scripter

Registered: 2005-08-05
Posts: 5
Bryce, it works fantastic! Thank you very much!
Top
#144847 - 2005-08-07 09:17 PM Re: Count files and filesize, then remove files older then 1 day
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11569
Loc: CA
Bryce,

When I add this to the mix I get bunch of errors

Code:
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')


Top
#144848 - 2005-08-07 11:17 PM Re: Count files and filesize, then remove files older then 1 day
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
Doc, you forgot our newest addition!
Code:

$SO=SetOption('NoMacrosInStrings','On')

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

Top
Page 1 of 1 1


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

Who's Online
0 registered and 159 anonymous users online.
Newest Members
emnipetro, Hirze, thequeen, ameliaryan, shalomsalami
17641 Registered Users

Generated in 0.04 seconds in which 0.011 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