Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make and edit text log file 2

Status
Not open for further replies.

Bullsandbears123

Technical User
Feb 12, 2003
291
0
0
US
Does any body know how to make a text file in VB then edit it. For example I need to make a log file then add data to it everytime there is an error. So I need the following
1) How to make a new text file and label it .log
2) How to add information to it I need the commands to add a line of information.

Thanks, I do not know where to start.

 
This creates a DOS batch file but the general principal applies

BatLine1 = "ECHO OFF" & Chr(13) & Chr(10) & "ECHO OFF" & Chr(13) & Chr(10) & "copy " & accloc & "testfile.txt lpt1:" & Chr(13) & Chr(10)
BatLine1 = BatLine1 & "EXIT" & Chr(13) & Chr(10) & Chr(13) & Chr(10)
Open accloc & PrintProg For Output As #1 ' Open file for output.
Write #1, BatLine1
Close #1

Check out the Open command in the help files as there is an APPEND function which will allow text to be added

 
Here are a couple of Subs that do just what you need.

The first routine (create_log_file) creates a text file called errorlog.log in the c:\ directory - change the name and path to suit yourself.

The second routine (write_errors) opens the log file, adds a new line of text and then closes the file.

Code:
Sub create_log_file()

' Create File System object
    Set SysObj = CreateObject("Scripting.FileSystemObject")
' Create log file
    Set TextStream = SysObj.CreateTextFile("c:\errorlog.log", True)
' Close logfile
    TextStream.Close

End Sub

Sub write_errors(error_message As String)

' Create File System object
    Set SysObj = CreateObject("Scripting.FileSystemObject")
' Get log file
    Set TextFile = SysObj.GetFile("c:\errorlog.log")
' Open log file for appending
    Set TextStream = TextFile.OpenAsTextStream(8, -2)
' Write error message to log file
    TextStream.WriteLine error_message
' Close log file
    TextStream.Close

End Sub


Note the second sub requires you to send the error message as an argument - if you don't know how to do this then post back and I'll show you how.

Regards

Damien
 
When you say argument, are you referring to a string
example
dim mystringmessage as string

textstream.writeline mystringmessage


Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top