#53046 - 2001-01-09 12:24 AM
Help! Cannot get "dynamic" date file-delete script to work...
|
masken
MM club member
   
Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
|
Ok, so here's another "if file is older than... delete". But anyway... I was looking for an easy way to do this on, which wasn't XXXkb of scriptcode, and since I couldn't understand a of the advanced one described here, I tried to do my own  So I worked some more on cj's script , which I didn't get to work with the dates comparison...  I ended up with the script below, in which I cannot seem to get the "GetFileTime()" function to work. Although this script will not generate exact dates, but it will do for me  Another thing that would be great is to be able to have multiple directories, I believe this may be possible with some sort of Array? I don't know how to do this..., any1? The faulty results are described below... code:
; =================================================================== ; === Script to delete files older than $DelDayCount number of days ; === in a directory. ; =================================================================== BREAK ON CLS;=== Set your number of days for "files older than" to be deleted, ;=== and the directory to look in (ex: ...=dir("C:\deltest\*.txt). $DelDayCount = "90" $FileName = Dir("C:\datetest\*.*") While $FileName <> "" AND @ERROR = 0 IF $FileName = "." $FileName = Dir() ENDIF IF $FileName = ".." $FileName = Dir() ENDIF ;=== Get the file date (comes in YYYY/MM/DD). $FileDate = GetFileTime("$FileName") $FileYear = Val(Substr($FileDate,1,4)) $FileMonth = Val(Substr($FileDate,6,2)) $FileDay = Val(Substr($FileDate,9,2)) ;=== Get this Year. $NowYear = @YEAR ? "Test number 1, check which variables are set..." ? "===============================================" ? "FileDate is $FileDate" ? "FileYear is $FileYear" ? "FileMonth is $FileMonth" ? "FileDay is $FileDay" ? "NowYear is $NowYear" ? "FileName is $FileName" Get $x ;=== First thing to compare is the years, depending on your ;=== wishes, this can speed up the process, since no further ;=== calculations is nesccessary for really old files. ;=== BEWARE of shiftings between years, a minimum of two is ;=== a must (of course depending on your $DelDayCount.) $YearTotal = $NowYear - $FileYear IF $YearTotal > 2 GOTO FILEDELETE ENDIF ;=== Calculate the total number of days that differ between the ;=== file and todays date. $YearDiff = $YearTotal * 365 $MonthDayDiff = $FileDay + ($FileMonth * 30) $FileDiff = @YDAYNO + ($YearDiff - $MonthDayDiff) ;=== And here is the comparison. IF $FileDiff > $DelDayCount GOTO FILEDELETE ENDIF GOTO NEXTFILE :FILEDELETE ? "Deleting... $FileName" ;DEL("$FileName") :NEXTFILE ? "" ? "Test number 2, check which variables are set..." ? "===============================================" ? "YearTotal is $YearTotal" ? "YearDiff is $YearDiff" ? "MonthDayDiff is $$MonthDayDiff" ? "FileDiff is $FileDiff" Get $x ;=== Get the next file, and do until there are no more files. $FileName = Dir() LOOP
I've been testing this with some files copied into C:\datetest, and the first file Dir() finds is "Key.ini", modified 1999-07-01 19:37, the script then generated the following output: code:
Test number 1, check which varibles are set... ============================================== FileDate is FileYear is 0 FileMonth is 0 FileDay is 0 NowYear is 2001 FileName is Key.ini Deleting... Key.iniTest number 2, check which varibles are set... ============================================== YearTotal is 2001 YearDiff is $YearDiff MonthDayDiff is $MonthDayDiff FileDiff is $FileDiff
The script doesn't work with any file until it hits my copy of msdos.sys, then, all of the sudden, the script works well! (FileDate is 2000/11/23 11:30:28).
[This message has been edited by masken (edited 08 January 2001).]
_________________________
The tart is out there
|
|
Top
|
|
|
|
#53047 - 2001-01-08 02:13 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Hi masken,I've written a script with your wishes. It has the multiple directory functionality and it has the 'VooDoo' date calculation . I have crunched the code a litle bit, if you need explanation about a certain part, don't hesitate to ask. code:
BREAK ON CLS ;*********************************************************************** ;* Set some variables ;***********************************************************************; * This is where the temporary filelist and the logfile are placed. ; * (Preferably on the local hard-disk (Need for speed ) $WorkDir="E:\scripts\remove" ;* These are the directories that need to be cleaned up. DIM $RemoveDir[10] $RemoveDir[0]="E:\scripts\remove\test" $RemoveDir[1]="E:\scripts\remove\test2" ; * Delete files if they are older than the current date minus these days. $days=30 ;*********************************************************************** ;* Create a Logfile. ;*********************************************************************** $lognr=0 while exist("$workdir\log$lognr.txt") $lognr=$lognr+1 loop $nul=open (2,"$workdir\log$lognr.txt",5) ;*********************************************************************** ;* Find the date (minus $days) ;*********************************************************************** $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 $CompareDate="$rd_year/$rd_month/$rd_day" ;*********************************************************************** ;* Generate a textfile with all filenames (again for each directory) ;*********************************************************************** $dirnr=0 while $removedir[$dirnr]>"" $nul=close(1) del "$workDir\files.txt" $Remdir=$removedir[$dirnr] shell "%comspec% /c dir $Remdir /s/b > $workdir\files.txt" $nul=open (1,"$workdir\files.txt") gosub main_loop $dirnr=$dirnr+1 loop ;*********************************************************************** ;* The End. ;*********************************************************************** EXIT ;*********************************************************************** ;* Subroutine: Mainloop comparing dates. ;*********************************************************************** :main_loop $line=readline(1) while @ERROR=0 gosub do_line $line=readline(1) loop return ;*********************************************************************** ;* Sub-subroutine: Delete $line and write a line to the Log-file. ;*********************************************************************** :do_line $time=getfiletime($line) if $time>"" if substr($time,1,10)<$CompareDate $nul=writeline(2,$line+", Filetime = $time, Removed on @DATE @TIME"+chr(13)+chr(10)) del $line endif endif return
The problem with the MSDOS.SYS is problably an Attribute problem (attributes like read-only and system-file) If that is the case, tell me and I will add some SETFILEATTR() commands.
------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------
_________________________
The Code is out there
|
|
Top
|
|
|
|
#53048 - 2001-01-08 04:56 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
masken
MM club member
   
Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
|
Hi DrillSergeant!Hmm... I didn't get this one to work! I tried to change the code to:
code:
$WorkDir="C:\datetest\workdir" ;* These are the directories that need to be cleaned up. DIM $RemoveDir[1] $RemoveDir[0]="C:\datetest\dir_n1" $RemoveDir[1]="C:\datetest\dir_n2" ; * Delete files if they are older than the current date minus these days. $days=90
I get the following error: "Script error: array out of bounds!. $RemoveDir[1]="C:\datetest\dir_n2" if I set: DIM $RemoveDir[2] The correct files get deleted, and logs are created, but I still get a KIX Script error: while $removedir[$dirnr] > "" ..and one other thing; why all the math? It makes it so much more complicated to follow? Ok ok, it's probably more right this way  Again, thanks for answering! [This message has been edited by masken (edited 08 January 2001).] [This message has been edited by masken (edited 09 January 2001).]
_________________________
The tart is out there
|
|
Top
|
|
|
|
#53051 - 2001-01-09 10:29 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
What I have found, (and what I always suspected) is that
code:
DIM $ARRAY[2]$test=$ARRAY[2]
Will give an 'array reference out of bounds' error. This means that DIM $ARRAY[2] will give you only TWO records (these are 0 and 1 !) This is contradicting to what is said in the manual. (It states: A DIM $ARRAY[2] will give you three records 0, 1 & 2.)To solve your problem you should add another 1 to the DIM. As you can see in my original script, I just DIM it with 10 to get around this problem. P.S. Sorry for my late reply, One of our servers crashed. (I have to do some real work sometimes ) ------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------
_________________________
The Code is out there
|
|
Top
|
|
|
|
#53054 - 2001-01-09 11:24 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
ok, i think i have found the source of confusion on this "bug"I have the manual for ver 3.61 printed out (It is a very well worn document, and we are not going to talk about the various stains that can be found on several pages ), and according to it a dim $array[10] would give you 11 spaces 0-10 in the array, but after printing out the 3.63 version.... it says that a dim $array[10] will give you 10 array spaces 0-9 Now the test to preform is, did the array function really change between 3.61 and 3.62?.... Survey SAYS! nope, this appears to be a typo in the manual. also, the 3.63 manual refers to arrays in 2 spots page 20(correct), and page 28 (wrong). Bryce [This message has been edited by Bryce (edited 09 January 2001).]
|
|
Top
|
|
|
|
#53055 - 2001-01-10 01:08 AM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
YESSS !! I'm sane !!Weird thing, I also thought it was me. Especially filling arrays with WHILE - LOOPs I always thought I was miscounting, and never thought about testing it. Ok, I'm glad that is cleared up. This also inspired me to do a little extra work on my 'SELECT' script, which I will post in a minute.
------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------
_________________________
The Code is out there
|
|
Top
|
|
|
|
#53058 - 2001-01-11 03:45 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
Anonymous
Anonymous
Unregistered
|
Hi, it's much simpler to use a free, plain DOS util for this. I use T-DEL (http://tutils.cjb.net/tdel.htm), which has tons of options. Just be careful that: - it doesn't support long file names. - if it will automatically delete any empty directory it will work on (mkdir it again as the next command, just in case).
|
|
Top
|
|
|
|
#53060 - 2001-01-11 03:57 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
masken
MM club member
   
Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
|
WHAT? Guy's, there's anti-KIX people in the forum!  Well kaidza, since I have the script running now, and it supports long filenames, generates a log, and doesn't delete diretories (which is just the way I want it), i'm pretty pleased. If I want to start it any other time than at logon, i'll just activate it with PACT Timer. Try to get that working with a DOS app  KIX rocks, and this BB rocks, because the users consists of many talented people. Now... if I just can get that subdir's function to work... 
_________________________
The tart is out there
|
|
Top
|
|
|
|
#53061 - 2001-01-12 07:20 PM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Oops, sorry I didn't reply sooner but I didn't realize that you had another question.How to control the files in subdirs? Aren't they checked then? We have implemented the command. DIR $REMDIR /b /s /b = basic notation /s = subdirectories It should also list (and check) all files in the subdirectories. Or have I misunderstood your question? ------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------ [This message has been edited by DrillSergeant (edited 12 January 2001).]
_________________________
The Code is out there
|
|
Top
|
|
|
|
#53063 - 2001-01-15 09:33 AM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
DIR *.* gives:
code:
Volume in station C is SYSTEM Volumenummer is 39AE-F446Map van C:\ 08/24/96 10:11a 214,836 IO.SYS 04/01/99 01:29p 1,453 MSDOS.SYS 08/24/96 10:11a 2,100 DBLBUFF.SYS 02/06/98 02:17p 129,080 LOGO.SYS 08/31/00 07:23p <DIR> WINNT 08/31/00 07:24p <DIR> Program Files 01/15/01 08:20a 279,969,792 pagefile.sys 08/31/00 08:29p <DIR> DMINT40 08/31/00 08:36p <DIR> TEMP 09/11/00 01:54p <DIR> Windows 09/27/00 04:01p <DIR> Compaq 12/07/00 03:14p 662 NETWORKR.LOG 12/22/00 03:21p 1,478 appicon.bmp 01/15/01 09:28a 0 test.txt 14 bestand(en) 280,319,401 bytes 1,182,269,440 bytes beschikbaar
dir *.* /b gives: code:
IO.SYS MSDOS.SYS DBLBUFF.SYS LOGO.SYS WINNT Program Files pagefile.sys DMINT40 TEMP Windows Compaq NETWORKR.LOG appicon.bmp test.txt
Notice the difference? 
The examples above are in Dutch b.t.w. ------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------
_________________________
The Code is out there
|
|
Top
|
|
|
|
#53064 - 2001-01-15 11:39 AM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
masken
MM club member
   
Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
|
Ok!Thanks DrillSergeant! Not only have we now a fully functional "delete-files-older-than" script, we also know how to use it the way we want to! I publish the final version below... code:
;====================================================================== ;=== DESCRIPTION: Script to delete files older than '$days' old. ;=== AUTHOR: DrillSergeant / additions by Masken  ;=== DATE: 2001-01-15 ;=== FILENAME: delfiles.kix ;=== PURPOSE: Automated deletion of specified files in specified ;=== folders. ;=== REVISIONS: Rev 0: Initial release ;======================================================================BREAK ON CLS ;====================================================================== ;=== Set varables. $WorkDir specifies where the logfile, and temporary ;=== files should be placed (prefferably on a local HD, for speed. In ;=== this case a hidden share on a server is specified). ;=== $RemoveDir is the list with directories in which the script looks ;=== for files. ;=== The smallest value for the $RemoveDir[] array is 3. Always ;=== specify an extra varable for the array (where array ranges from ;=== 0 to x +1), to get the script working properly. ;=== Files older than today's date minus $Days will be deleted. $WorkDir = "\\[server]\del_log$$" DIM $RemoveDir[3] $RemoveDir[0] = "D:\AS400\In" $RemoveDir[1] = "D:\AS400\Out" $Days = 90 ;====================================================================== ;=== Create a logfile on a central server (the number of the logfile is ;=== incremented if a logfile for the same workstation already exists ;=== in the $WorkDir). ;=== Here @WKSTA is used to generate the log's filename for each ;=== workstation.
$LogNr = 0 WHILE EXIST("$WorkDir\@WKSTA_log$LogNr.txt") $LogNr = $LogNr + 1 LOOP $nul = OPEN(2,"$WorkDir\@WKSTA_log$LogNr.txt",5) ;====================================================================== ;=== Calculate the date (minus $Days).
$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 $CompareDate = "$Rd_Year/$Rd_Month/$Rd_Day" ;====================================================================== ;=== 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!
$DirNr = 0 WHILE $RemoveDir[$DirNr] > "" $nul = close(1) DEL "$WorkDir\files.txt" $Remdir = $RemoveDir[$DirNr] SHELL "%COMSPEC% /c DIR $Remdir /s/b > $WorkDir\files.txt" $nul = OPEN(1,"$WorkDir\files.txt") GOSUB MAIN_LOOP $DirNr = $DirNr+1 LOOP ;====================================================================== ;=== The End.
EXIT ;====================================================================== ;=== Subroutine: Mainloop that reads lines. Goes down into the ;=== sub-subroutine to compare dates between today and the file. :MAIN_LOOP $line = readline(1) WHILE @ERROR=0 GOSUB DO_LINE $line = readline(1) LOOP RETURN ;====================================================================== ;=== Sub-Subrutin: Compares the dates in files.txt with the calculated ;=== date. If file should be deleted, add to log and delete.
O_LINE $time = GetFileTime($line) IF $time > "" IF Substr($time,1,10) < $CompareDate $nul = writeline(2,$line+", Filetime = $time, Removed on @DATE @TIME"+chr(13)+chr(10)) DEL $line ENDIF ENDIF RETURN
_________________________
The tart is out there
|
|
Top
|
|
|
|
#53065 - 2001-01-15 11:59 AM
Re: Help! Cannot get "dynamic" date file-delete script to work...
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Aaargh, you've streched it out! now everyone can read it.  Kidding, it looks good! Do you mind if I post the final version on my homepage? I'll make some minor changes: 1. Create a variable to set the sub-dir option. 2. Use the %TEMP% variable to put the output of the DIR command to the local disk. Ok, now that we've closed this chapter what will be our next project? ------------------ Greetz, Roger the Young ------------------------ The code is out there... ------------------------
_________________________
The Code is out there
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 583 anonymous users online.
|
|
|