Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740

    Unanswered: elegant trim of logfile.txt

    my startup logfile.txt has the format

    "
    -- STARTING dd/mm/yyyy hh:mm:ss
    OK blah
    OK more blah
    ## problem blah

    .....arbitrary number of blah lines starting OK or ##
    -- MAIN
    "
    "
    -- STARTING dd/mm/yyyy hh:mm:ss

    ...and so on.

    the file cannot be allowed to grow forever, so i guess that at 25kB i should knock off the oldest 5kB

    it would be nice (cosmetically) if the trimmed file started on a 'boundary'. sure it is not important, sure i could write code to display a 'cosmetic' log review, but i'm looking for an efficient way to:

    find the logfile size
    if > 25kB then
    take it down to 20kB by killing the top (older) portion of the file...
    ...and have the trimmed file start elegantly with the lines
    "
    -- STARTING dd/mm/yyyy hh:mm:ss

    in the .txt file

    this doesn't seem an exotic thing to do, so before i spend some weekend hours re-discovering the wheel, maybe you have already done something similar: all suggestions welcome (this is a startup log and i cannot guarantee that tables are functioning: pointless to try to log missing table errors into missing tables)

    TIA izy
    currently using SS 2008R2

  2. #2
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011
    FileLen(strFile) returns a long integer of the length of strFile in bytes.

    Is there a reason you couldn't create a seperate little DB with a table that holds the log? This DB could be masked as a log file by removing it's file extension. You could access it through code using any parent software, including writing a little VB app to launch your Access app and log results in the seperate file.

    A quick way to do your cropping operation once in a while is to create a little Access DB that links to the log file as a linked text table. Write a function that extracts the date from "-- STARTING dd/mm/yyyy hh:mm:ss" and use an update query to delete all records (lines) earlier than your cutoff date.

    I'm not sure how fast it would be, but using a Do ... While Not EOF(1) Loop, have it open the current log for input and a new log for output. Run through each line and when it reaches a line where the date in "-- STARTING dd/mm/yyyy hh:mm:ss" is greater than your desired starting point, start transfering the remaining lines to the new file. Save the new file, kill the old file and rename the new file to the old name. ... of course, this probably qualifies as reinventing the wheel!

    have fun,
    tc

  3. #3
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    this is what i came up with.

    Code:
        If lngSize > 25000 Then               'if file over 25kB, knock it back
            Set lFile = fso.OpenTextFile(strLogFile, cstRead, False, 0)
            lFile.Skip (5000)                     'kill the top 5kB
            strBuf = lFile.ReadLine               'seed strBuf
            Do While Left$(strBuf, 6) <> "-- END" 'look for the end of a block
                strBuf = lFile.ReadLine               'discarding till found
            Loop
            strBuf = lFile.ReadAll                'read the rest of the file
            lFile.Close
            Set lFile = fso.OpenTextFile(strLogFile, cstWrite, True, 0)
            lFile.Write strBuf                    'overwrite the existing file
            lFile.Close
        End If
    had to change the block-end marker because there are several exits possible: to the main form; to e-mailer; to abort; etc. now they all begin with '-- END'

    at around 30 lines per startup this leaves me parsing an average of 15 lines ...no obvious lag while the file truncates.

    interestingly
    lFile.Size
    is documented in A2K3 help, but didn't want to play. i had to use FileLen()

    izy
    currently using SS 2008R2

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    With regards to size, the file (and folder) object has a size property, but your lFile variable, though not declared within, is a textstream object, which doesn't have such.

    set myfile = fso.getfile(strlogfile)
    debug.print myfile.size
    Roy-Vidar

Posting Permissions

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