Link to home
Start Free TrialLog in
Avatar of HPvH
HPvHFlag for Netherlands

asked on

VBS Create CSV Log file

Hi,

I need a function who write a csv log file.
I want to call my function like this:
WriteLog(DateTime,Value1,Value2,Value3)
Every time I call WriteLog, it should append the data to c:\temp\LogFile.csv

Thanks!
HPvH
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Try this code below, it give options on whether overwrite a file or append to a file:

Public Sub WriteFileText(ByVal FileName As String, ByVal Source As String, Optional noCrlf As Boolean = False, Optional Append As Boolean = False)
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    If Append Then
        Open FileName For Append As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    Else
        Open FileName For Output As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    End If
    Exit Sub
EHandler:
    'ShowErrMsg
    On Error Resume Next
    Close #Handle
End Sub
Avatar of MorDrakka
MorDrakka

Hi,

Try the following!

You can set the objFSO in the main script. You can set them in the function too but then he opens and closes them each time you enter the function.
DateTime, Value1, Value2, Value3 are all expected to be variables you get on your own :P

Regards,

M
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objOutputFile = objFSO.CreateTextFile("log.txt", True)
 
 
Function Writelog(DateTime, Value1, Value2, Value3)
objOutputfile.writeline DateTime & "," & Value1 & "," & Value2 & "," & Value3
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial