[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

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

0
csharp_learner
Asked:
csharp_learner
  • 5
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
csharp_learnerAuthor Commented:
Thank you, sorry for not informing it's vb script in advance.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now