Write text to File-VB 6.0

The following code snippet would allow one to write text to a file in VB 6.0. This can help if some one wants to log any kind of information to a log file.

The function basically accepts 3 parameters

1. The text/content of the line that needs to be written. 2. The path name where the file is existing / is to be created eventually. 3. The name of the file.

Private Function WriteToFile(strLineToWrite As String, _ strPathName As String, _

strFileName As String) As Boolean

Dim FileN As Integer Dim FileExists As String

Dim DirExists As String

FileN= FreeFile

‘–Check for the directory existance
DirExists = Dir(strPathName, vbDirectory)

If DirExists = “” Then MkDir (strPathName)

End If

FileExists = Dir(strPathName & “\” & strFileName, vbNormal)

‘–Check for the file existance ‘–If it’s not there create it and write the line of text into it

If FileExists = "" Then Open strPathName & "\" & strFileName For Output As FileN Print #FileN, strLineToWrite Close FileN

Else

‘–If it is there then append data to end of file

Open strPathName & "\" & strFileName For Append As FileN Print #FileN, strLineToWrite Close FileN

End If

WriteToFile= True

End Function

150 150 Burnignorance | Where Minds Meet And Sparks Fly!