Results 1 to 8 of 8
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fso Scripting (vb6)

    I need to log different events to track usage of my application.
    I found a useful function on FreeVBCode which I think will help me.
    The function should be called in code by :

    "WriteLog" but I am not sure what to add as the sLogEntry.
    Lets say I copy files into a folder using a listbox, I would like to call the function to write to the log of the files which have been copied.

    Syntax is my downfall here.

    Would it be something like:
    <pre>FileCopy lstImages.List(N), DP & FF
    WriteLog "Files Added" & " " & lstImages.List(N), DP & FF</pre>





    <pre>'set the maximum logsize to 2 Mb
    Const MaxLogSize = 2000000

    Public Sub WriteLog(sLogEntry As String)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim sLogFile As String, sLogPath As String, iLogSize As Long
    Dim fso, f

    On Error GoTo ErrHandler

    'Set the path and filename of the log
    sLogPath = App.Path & "" & App.EXEName
    sLogFile = sLogPath & ".log"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(sLogFile, ForAppending, True)

    'Get the size of the log to check if it's getting unwieldly
    iLogSize = GetLogSize(sLogFile)
    If iLogSize > MaxLogSize Then

    'If too big, back it up to to retain some sort of history
    fso.CopyFile sLogFile, (sLogPath & ".old"), True
    Set f = Nothing
    fso.DeleteFile sLogFile
    'And start with a clean log-file
    Set f = fso.OpenTextFile(sLogFile, ForAppending, True)

    End If

    'Append the log-entry to the file together with time and date
    f.WriteLine Now() & vbTab & sLogEntry

    ErrHandler:
    Exit Sub
    End Sub

    Private Function GetLogSize(filespec As String) As Long
    'Returns the size of a file in bytes. If the file does not
    'exist, it returns -1.

    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")

    If (fso.FileExists(filespec)) Then
    Set f = fso.GetFile(filespec)
    GetLogSize = f.Size
    Else
    GetLogSize = -1
    End If
    End Function</pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: fso Scripting (vb6)

    The sLogEntry argument can be anything you would like to record, in the form of a string. You can't use it in the form you suggest, because that consists of two separate arguments used by FileCopy: the source file and the target file. If you want to store the name of the target, use

    WriteLog "File added: " & DP & FF

    or if you want to record that you copied a file:

    WriteLog "File " & lstImages.List(N) & " copied to " & DP & FF

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: fso Scripting (vb6)

    Hans
    That works fine.

    If I wanted to log events over multiple forms, could I use the function as a standard module and just call it from where I like.
    Would the whole function including the declarations have to modified or would it be suffice just to write declarations in whichever form
    I wanted to use the function ?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: fso Scripting (vb6)

    Since the WriteLog procedure is already declared as Public, you can call it from anywhere in your application if you put it in a standard module (together with the line Const MaxLogSize = 2000000)

    Just use it in the form

    WriteLog "any text you would like to save in the log file"

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: fso Scripting (vb6)

    Pefect

    Thanks again Hans, much appreciated.

  6. #6
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: fso Scripting (vb6)

    Just an after thought.

    Ideally with many users of the application, each machine can write it's own log in a local folder, "C:MM-Utilities"

    But with rolling this application and training the users on a part time basis, I would like if possible to have all machines write to one log only.
    ie: each machine write to the network "L:MMPDFUtilities" so I could check the log for any anomilies or errors.

    My only thought is confliction.

    Is it advisable to use the WriteLog feature in this way, or should I stick with local writing for each machine ?

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: fso Scripting (vb6)

    Dave,

    If multiple users will be using the system at the same time, you will have a problem if two users try to write to the common log file at the same time. You could give each user his/her own log file on the network, in L:MMPDFUtilities. That would enable you to inspect all log files.

    In the code you're currently using, the path and name of the log file is specified in

    sLogPath = App.Path & "" & App.EXEName

    Instead of this, you could use the GetNetUser (or GetComputerName) function from <post#=357176>post 357176</post#>:

    sLogPath = "L:MMPDFUtilities" & GetUserName

    For instance, if you log in to the net as davew, sLogPath would be "L:MMPDFUtilitiesdavew" and so sLogFile would be "L:MMPDFUtilitiesdavew.log". For someone logging in as martinh, the log file would be "L:MMPDFUtilities"martinh.log", etc.

  8. #8
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: fso Scripting (vb6)

    Thanks Hans

    That is exactly what I was after.

    I had (am) already using the GetcomputerName function within the log to return "USER6 Logged On ............................. "

    But the sLogPath = "L:MMPDFUtilities" & GetComputerName was the part I needed.

    Thanks again. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

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