Incremental log file function...
Results 1 to 11 of 11

Thread: Incremental log file function...

  1. #1
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Question Incremental log file function...

    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

  2. #2
    Registered User ShadowKing's Avatar
    Join Date
    Dec 1999
    Location
    WA
    Posts
    743

    Post

    here is part a subroutine that I wrote for a vbs script that synchronizes folders. 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.
    Matt

    "If you have been tempted into evil, fly from it. It is not falling into the water, but lying in it, that drowns"

  3. #3
    Registered User ShadowKing's Avatar
    Join Date
    Dec 1999
    Location
    WA
    Posts
    743

    Post

    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

  4. #4
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Post

    I'm using VB6 SP4

  5. #5
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Post

    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.

  6. #6
    Registered User
    Join Date
    Jan 1999
    Location
    London, Great Britain
    Posts
    300

    Lightbulb

    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:

    Code:
    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 pade on MSDN for the details.

    HTH,
    I'd rather die peacefully in my sleep like my Grandfather,
    than screaming in terror like his passengers.
    Jim Harkins
    <a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>

  7. #7
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Post

    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

  8. #8
    Registered User
    Join Date
    Jan 1999
    Location
    London, Great Britain
    Posts
    300

    Post

    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:
    Code:
    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
     &nbsp; sFilename = App.Path
    Else
     &nbsp; sFilename = DestFolder
    End If
    
    
    
    If Right(sFilename, 1) <> "\" Then
     &nbsp; sFilename = sFilename & "\"
    End If
    
    
    
    lNumber = 1
    sFilename = Preface & sFilename
    sNumber = Right("000", 3 - (lNumber \ 10)) & lNumber & ".log"
    
    
    
    While Dir(sFilename & sNumber) <> ""
     &nbsp; lNumber = lNumber + 1
     &nbsp; sNumber = Right("000", 3 - (lNumber \ 10)) & lNumber & ".log"
    Wend
    
    
    
    GetLogName = sFilename & sNumber
    
    
    
    End Function
    You may find this works...

  9. #9
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Post

    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">

  10. #10
    Registered User
    Join Date
    Jan 1999
    Location
    London, Great Britain
    Posts
    300

    Post

    You'd be better off creating your number like this:

    Code:
    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 for full details, with the User Defined Numeric Formats page for help.

  11. #11
    Registered User
    Join Date
    Oct 2000
    Location
    Kansas City, MO
    Posts
    1,162

    Post

    Thanks man, I figured it out...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •