Click to See Complete Forum and Search --> : Incremental log file function...
opiate
June 7th, 2001, 10:32 PM
Can someone give me ideals on how to code a function that will create log files each time my app runs and increment the log file name each time it creates a new one. Like so…
0001.log
0002.log
0003.log
0004.log
0005.log
I’m creating a program that pulls data from one database and insert the data into another. Each time a new record is inserted while looping through a recordset, my app will write the insert statement to the log file.
Thanks for any help.
Corey
ShadowKing
June 7th, 2001, 11:02 PM
here is part a subroutine that I wrote for a vbs script that synchronizes folders (http://www.mattlowe.com/files/distmaster.vbs). It was for a production system, so I had to handle all the errors and do reporting. This sub does basically what you want, except it has a preceding name for the file (like Joe(7).log). Anyway I have bolded the parts that should interest you. If you will tell me the language you want it in, I could probably see what I could do...
Sub saveLog(myLog, logSpot)
On Error Resume Next
If Not Right(logSpot, 1) = "\" Then logSpot = logSpot & "\"
myFile = logSpot & "SynchLog.htm"
If fs.FileExists(myFile) Then
m = 1
While fs.FileExists(logSpot & "SynchLog(" & m & ").htm")
m = m + 1
Wend
myFile = logSpot & "SynchLog(" & m & ").htm"
End If
Set Handle = fs.OpenTextFile(myFile, 2, True) 'save the file
Handle.WriteLine "A bunch of HTML code that doesn't work on Windrivers forums..."
Handle.Close
If Err.Number <> 0 Then 'The user entered an invalid location to save the error
If showError Then MsgBox "Error saving. File saved to c:\SynchLog.htm", , "Default Log Location"
myFile = "c:\SynchLog.htm"
Set Handle = fs.OpenTextFile(myFile, 2, True) 'save the new file
Handle.WriteLine "A bunch of HTML code that doesn't work on Windrivers forums..."
Handle.Close
End If
On Error goto 0
End Sub
Hope that helps.
ShadowKing
June 7th, 2001, 11:18 PM
Here is a stright VB Function that will do what you want. You can even copy and paste it into your project and just call it...
Private Function GetLogName(Preface As String, Optional DestFolder As String) As String
Dim n As Integer 'counter
If DestFolder = "" Then DestFolder = App.Path 'Check to see if they passed the variable
If Right(DestFolder, 1) <> "\" Then DestFolder = DestFolder & "\" 'Format it properly
n = 1
While Dir(DestFolder & Preface & Right("000", 3 - (n \ 10)) & n & ".log") <> ""
n = n + 1
Wend
GetLogName = Preface & Right("000", 3 - (n \ 10)) & n & ".log"
End Function
opiate
June 7th, 2001, 11:18 PM
I'm using VB6 SP4
opiate
June 7th, 2001, 11:39 PM
so I just call it like so
set fs = CreateObject("Scripting.FileSystemObject")
set a = fs.CreateTextFile((GetLogName, Preface, DestFolder), False)
I don't under stand how I call it. What is Preface? Sorry, I'm not very good with functions.
antonye
June 8th, 2001, 05:39 AM
There are two ways to attack this:
1. Create a filename to a certain scheme then see if it exists, incrementing until you find one that doesn't
or
2. Search for the last filename, then increment it
The advantage with (1) is that it will be relatively easy to code, but the problem is that as you fill your directory up with filenames, it will take longer to find the next file.
The advantage with (2) is that it will be quick to return your next filename, but a bit more tricky to code.
A good solution would be to narrow down the amount of searching by using the date as a prefix to the front of the file.
For example, your log files would be in the format:
LOG_YYYYMMDD_NN
Where:
YYYY = Year
MM = Month
DD = Day
NN = an incremented number
So your routine would look something like:
Function CreateLogFilename() as String
Dim sFilename as String
Dim lNumber as Long
Dim fso as Scripting.FileSystemObject
' create an FSO object
Set fso = CreateObject("Scripting.FileSystemObject")
' generate a filename first
sFilename = "C:\Logs\LOG_" & Format(Now(), "YYYYMMDD" & "_"
' start the number at 1
lNumber = 1
' see if this file exists
While NOT fso.FileExists(sFilename & lNumber & ".log")
' it does, so increment the number
lNumber = lNumber + 1
wend
' this file doesn't exist, so return it
CreateLogFilename = sFilename & lNumber & ".log"
end function
See the FileSystemObject (http://msdn.microsoft.com/library/default.asp?URL=/library/devprods/vs6/vbasic/vbenlr98/vaobjFileSystemObject.htm) pade on MSDN for the details.
HTH,
opiate
June 8th, 2001, 07:43 AM
here is what I have and it's not working
Const Preface = "SLXIMPORT"
Const DestFolder = "C:\TEMP"
Private Sub Command1_Click()
strfile = GetLogName(Preface, DestFolder)
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(strfile, False, False)
a.Write "Test"
a.Close
Set fs = Nothing
Set a = Nothing
End Sub
Private Function GetLogName(Preface As String, Optional DestFolder As String) As String
Dim n As Integer 'counter
If DestFolder = "" Then DestFolder = App.Path 'Check to see if they passed the variable
If Right(DestFolder, 1) <> "\" Then DestFolder = DestFolder & "\" 'Format it properly
MsgBox DestFolder
n = 1
While Dir(DestFolder & Preface & Right("000", 3 - (n \ 10)) & n & ".log") <> ""
n = n + 1
Wend
MsgBox DestFolder & Preface & Right("000", 3 - (n \ 10)) & n & ".log"
GetLogName = Preface & Right("000", 3 - (n \ 10)) & n & ".log"
End Function
The function looks to the DestFolder to calculate the filename. Then when it creates the file it creates it in the app.path.
I get runtime "58" file exist. If I do not specify the Preface or DestFolder it works but creates the files in app.path
antonye
June 8th, 2001, 10:09 AM
DestFolder = DestFolder & "\"
I don't think you can do this because, by default, you're passing over DestFolder as By Value (ByVal) rather than By Reference (ByRef).
You could tidy your code like this instead:
Private Function GetLogName(ByVal Preface As String, Optional DestFolder As String) As String
Dim sFilename as String
Dim sNumber as String
Dim lNumber As Long
If DestFolder = "" Then
sFilename = App.Path
Else
sFilename = DestFolder
End If
If Right(sFilename, 1) <> "\" Then
sFilename = sFilename & "\"
End If
lNumber = 1
sFilename = Preface & sFilename
sNumber = Right("000", 3 - (lNumber \ 10)) & lNumber & ".log"
While Dir(sFilename & sNumber) <> ""
lNumber = lNumber + 1
sNumber = Right("000", 3 - (lNumber \ 10)) & lNumber & ".log"
Wend
GetLogName = sFilename & sNumber
End Function
You may find this works...
opiate
June 8th, 2001, 05:13 PM
antonye,
I'm using your example with a few changes. I put the date before the incremental characters. It looks something like so..
[Preface] [Date] [sNumber]
SLXImport - 06/8/01 - 001.log
SLXImport - 06/8/01 - 002.log
SLXImport - 06/8/01 - 003.log
This works great but there is a bug. It will only increment to..
SLXImport - 06/8/01 - 39.log
If I change...
sNumber = Right("000", 3 - (sNumber\10)) & sNumber & ".log"
..to
sNumber = Right("0000", 4 - (sNumber\10)) & sNumber & ".log"
This will allow for the increment to reach..
SLXImport - 06/8/01 - 49.log
Workaround..
If the date changes it will start back over to
SLXImport - 06/9/01 - 0001.log.
I'm just curious to see how we could fix this. If I reach 39 log files in 1 day I will be screwed.
Any ideals?
Thanks for all your help <IMG SRC="smilies/biggrin.gif" border="0">
antonye
June 9th, 2001, 04:48 PM
You'd be better off creating your number like this:
sNumber = Format(lNumber, "0000")
This will format your number as a string into 4 digits, with any leading zeros if required.
This is a much neater way of doing it!
See the MSDN Format function (http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vafctFormat.htm) for full details, with the User Defined Numeric Formats (http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vafmtuserdefinednumericformats.htm) page for help.
opiate
June 12th, 2001, 11:24 PM
Thanks man, I figured it out...