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

csharp_learnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

csharp_learnerAuthor Commented:
The compile error is "Expected : ="
0
DhaestCommented:
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

0
csharp_learnerAuthor Commented:
Thanks for your prompt reply.

I got another compile error of "Expected end of statement" on line
oWrite = New System.IO.StreamWriter(FILE_NAME)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DhaestCommented:
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
0
raysonleeCommented:
Use "C:\\logfile.txt"
0
csharp_learnerAuthor Commented:
Got another error
error.JPG
0
DhaestCommented:
Are you using VB.NET ????


Write to file using vbscript: http://www.rgagnon.com/wshdetails/wsh-0003.html
0
csharp_learnerAuthor Commented:
i'm using vb script
0
DhaestCommented:
That explains a lot. You are using VB.NET coding !

Try this
function Write_in_File(File, Content) 
        Dim newFS 
            Set newFS = Createobject("Scripting.FileSystemObject") 
            Set File_Name = newFS.OpenTextFile(File, 2, True) 
            File_Name.Write Content 
            File_Name.Close 
            Set File_Name = Nothing 
            Set newFS = Nothing 
End function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
csharp_learnerAuthor Commented:
Thank you, sorry for not informing it's vb script in advance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.