#135803 - 2005-03-16 09:25 PM
how to scrub a file?
|
annie
Fresh Scripter
Registered: 2002-02-12
Posts: 10
|
I need to 'scrub' a log file that's being placed on my server and will grow continually unless I purge records > say 72 hours old. Anyone know a decent way to do this? help??
|
Top
|
|
|
|
#135805 - 2005-03-16 09:33 PM
Re: how to scrub a file?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Normally with log files you rotate them rather than delete records.
Pick a period (hourly, daily) and when it is triggered MOVE the file to a new name - keep a few generations.
The only tricky thing is that the process writing to the file needs to understand what is happening when the file moves. You may need to stop the process, move the file and then re-start the process.
For example, assuming your file is called "mylogfile.txt" and you want to keep a weeks worth then at midnight each day you would do the following in a batch file: Code:
move mylogfile.006 mylogfile.007 move mylogfile.005 mylogfile.006 move mylogfile.004 mylogfile.005 move mylogfile.003 mylogfile.004 move mylogfile.002 mylogfile.003 move mylogfile.001 mylogfile.001 move mylogfile.txt mylogfile.001
|
Top
|
|
|
|
#135806 - 2005-03-18 07:11 PM
Re: how to scrub a file?
|
annie
Fresh Scripter
Registered: 2002-02-12
Posts: 10
|
I did search, and didn't find anything that removed records, only things that removed files or folders. I don't really wnat to copy the file, as my goal is to reduce the amount of space the logs are taking up, not increase by generating multiple copies. all I want to do is shave the oldest records off the bottom.
|
Top
|
|
|
|
#135808 - 2005-03-18 08:40 PM
Re: how to scrub a file?
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
here is some code, using FSO to return a files creation date, and then compairing the age of that date vs the current date and time.
i ma using the UDF's FlipCTime() and TimeConvert()
Code:
$f = 'logfile.log' $f = CreateObject("Scripting.FileSystemObject").GetFile($f)
$date = Split($f.DateCreated)[0] $date = Split($date,"/") $date = $date[2]+"/"+$date[0]+"/"+$date[1]
$time = timeconvert(Split($f.DateCreated)[1] +" "+ Split($f.DateCreated)[2])
$72h = 259200
If flipctime(@DATE,@TIME) - flipctime($date,$time) > $72h ? 'the file ' + $f.name + ' was created 72+ hours ago' EndIf
Bryce
|
Top
|
|
|
|
#135813 - 2005-03-18 10:40 PM
Re: how to scrub a file?
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
|
Top
|
|
|
|
#135815 - 2005-03-21 10:18 AM
Re: how to scrub a file?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
There is an extensive log maintenance "kit" here: http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=75703&page=&view=&sb=5&o=&vc=1
However, I think that this is a bit more fully featured than you need.
Here is a simple example of how you could do the size limit thing in KiXtart:
Code:
Function udfRotateLog($sPath,$iSize,Optional $iGenerations,Optional $bInitLog)
Dim $sMask,$i,$fd
If Not CInt($iGenerations) $iGenerations=1 EndIf
If Not CInt($bInitLog) $bInitLog=0 EndIf
$iSize=CDBL($iSize)
While Len($sMask)<Len($iGenerations) $sMask=CSTR($sMask)+"0" Loop
If GetFileSize($sPath)>=$iSize
; Sanity check - Ignore
; 1 Read Only
; 4 System
; 16 Direcroty
; 256 Temporary
; 4096 Offline
If (1+4+16+256+4096) & GetFileAttr($sPath) Exit 1 EndIf
If @ERROR Exit @ERROR EndIf
; Remove oldest generation
Del $sPath+"."+$iGenerations
; Rotate existing log files
For $i=$iGenerations To 2 Step -1
Move $sPath+"."+Right($sMask+($i-1),Len($iGenerations)) $sPath+"."+Right($sMask+$i,Len($iGenerations))
; Ignore "file not found"
If Not(@ERROR=0 OR @ERROR=2) Exit @ERROR EndIf
Next
; Now move primary log file
Move $sPath $sPath+"."+Right($sMask+1,Len($iGenerations))
If @ERROR Exit @ERROR EndIf
; Finally, create an empty log file if needed
If $bInitLog
$fd=FreeFileHandle()
If @ERROR Exit @ERROR EndIf
If Open($fd,$sPath,1+4) Exit @ERROR EndIf
$=Close($fd)
EndIf
EndIf
Exit 0
EndFunction
Parameters are:
- $sPath - Path to your log file.
- $iSize - When file reaches this size (in bytes) it is rotated. If you set this to zero, the file is always rotated.
- $iGenerations - How many generations of the file to keep
- $bInitLog - Set to true if you want to create an empty file once the log file has been rotated.
So to keep 50 MB of history, and create an empty file after the rotation:
Code:
$sLogFile="c:\foo\mylogfile.txt"
$iMaxSize=10*1024*1024 ; 10 Megabytes
$iMaxHistory=5
udfRotateLog($sLogFile,$sMaxSize,$iMaxHistory,1)
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 259 anonymous users online.
|
|
|