Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

better way to copy and increment log files

Status
Not open for further replies.

robFSS

IS-IT--Management
Apr 29, 2003
106
US
hey all

there's got to be a better way to accomplish this - I'm lousy with arrays...

as the log file for my script grows too large, I want to copy the current log to a backup file and start a fresh one. I could see myself keeping up to 10 logs. the original log is "redactc.log" and the backups are to be "redactc.log.1", "redactc.log.2", etc.

here's how I'm doing it now - Is there any way to run this sub more dynamically to allow for a greater number of backups without writing 10+ "If" statements?

see code snippet below

thanks,
rob

Code:
dim oFSO, strPath, strFile, oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
strPath = "c:\logs"
strFile = "\redactc.log"

Sub logsize
	'get size of logfile
	set oFile = oFSO.GetFile(strPath & strFile)
	
	If ofile.size > 10000 Then
		
		If oFSO.FileExists(strPath & strFile & ".2") Then
			oFSO.DeleteFile strPath & strFile & ".2"
		End IF
		
		IF oFSO.FileExists(strPath & strFile & ".1") Then
			oFSO.CopyFile strPath & strFile & ".1", strPath & strFile & ".2", True
			oFSO.DeleteFile strPath & strFile & ".1"	
		End If
		 
		If oFSO.FileExists(strPath & strFile) Then
			oFSO.CopyFile strPath & strFile, strPath & strFile & ".1", True
			oFSO.DeleteFile strPath & strFile
		End If
	
	End If
	Set oFile = Nothing
End Sub

~~~shake and bake~~~!!!!
 
Use a For...Next statement

Code:
For intLogNum = 1 To 10
	WScript.Echo strPath & strFile & "." & intLogNum
Next

Instead of the WScript.Echo you would do a single If...statement

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4eva -

I took your approach and tweaked it to suit my desired goal. I had to use a -1 step in the for...next statement to get what I wanted.

snippet is below if you're interested. comments welcome.
-r

Code:
Sub logver[COLOR=green]
	'get size of logfile [/color]
	Set oFile = oFSO.GetFile(strPath & strFile)[COLOR=green]
		'is the log larger than the desired size?  
		'msgbox "filesize is: " & oFile.size [/color]
		If oFile.size > 1181 Then 
			Dim counter 
			For counter = 9 to 1 step -1	
				If oFSO.FileExists(strPath & strFile & "." & counter) Then
					oFSO.CopyFile strPath & strFile & "." & counter, strPath & strFile & "." & counter + 1, True	
				End If		
			Next 
			oFSO.CopyFile strPath & strFile, strPath & strFile & ".1", True
			oFSO.DeleteFile strPath & strFile
		End If
	Set oFile = Nothing
End Sub


!!!~~~shake and bake~~~!!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top