Link to home
Start Free TrialLog in
Avatar of csharp_learner
csharp_learnerFlag for Singapore

asked on

Help on code debug Write log file

Hi,

What i want to achieve is to write into a txt file if the file exist and create if it does not exist.

I got an error on these 2 lines of code.
    oWrite.WriteLine("{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}", Now(),"String")
    oWrite.Close()

Is it an syntax error or do i have to include a reference?
Dim oFile As System.IO.File
    Dim oRead As System.IO.StreamReader
    Dim FILE_NAME As String
    Dim oWrite As System.IO.StreamWriter
    
    
    
FILE_NAME = "C:\logfile.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
'write into logfile.txt

    oWrite.WriteLine("{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}", Now(),"String")
    oWrite.Close()

MsgBox ("Text written to file")
Else
'Create file
oWrite = oFile.CreateText("C:\logfile.txt")
MsgBox ("File Created")
End If

Open in new window

Avatar of csharp_learner
csharp_learner
Flag of Singapore image

ASKER

The compile error is "Expected : ="
Avatar of Dirk Haest
You need to open a new streamWriter before writing to the file

See also http://www.dotnetperls.com/streamwriter-vbnet
Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        Dim FILE_NAME As String
        Dim oWrite As System.IO.StreamWriter



        FILE_NAME = "C:\logfile.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then
            'write into logfile.txt
            oWrite = New System.IO.StreamWriter(FILE_NAME)

            oWrite.WriteLine("{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}", Now(), "String")
            oWrite.Close()

            MsgBox("Text written to file")
        Else
            'Create file
            oWrite = oFile.CreateText("C:\logfile.txt")
            MsgBox("File Created")
        End If

Open in new window

Thanks for your prompt reply.

I got another compile error of "Expected end of statement" on line
oWrite = New System.IO.StreamWriter(FILE_NAME)
What coding are you using ? VB.NET or vbscript (you posted it in this zone).

I just tested the code here and it worked like a charm
Avatar of raysonlee
raysonlee

Use "C:\\logfile.txt"
Got another error
error.JPG
Are you using VB.NET ????


Write to file using vbscript: http://www.rgagnon.com/wshdetails/wsh-0003.html
i'm using vb script
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
Thank you, sorry for not informing it's vb script in advance.