File System + File I/O in VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Revision as of 11:01, 2 February 2008 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Write/Create a new file using FileSystemObject

  • must add 'reference' to "Microsoft Scripting Runtime"
 Dim fso As New FileSystemObject, fsoStream As TextStream
 Dim rec1, strLine, strFileSpec
 Dim cnt0 As Integer, tag1 As Integer
 strFileSpec = Environ("HOMEPATH") & "\My Documents\kaching.log"
 strLine = "kaching incident " & Date & " " & Format(time, "HH:mm:ss") & vbCrLf
 Set fsoStream = fso.CreateTextFile(strFileSpec, True)
 fsoStream.Write strLine
 fsoStream.Close
 Set fsoStream = Nothing: Set fso = Nothing

 

Write if exists, create if doesn't exist using FileSystemObject

 If fso.FileExists(strFileSpec) Then
   Set fsoStream = fso.OpenTextFile(strFileSpec, 2)
 Else
   Set fsoStream = fso.CreateTextFile(strFileSpec, True)
 End If
 
 fsoStream.Write strLine
 fsoStream.Close
 Set fsoStream = Nothing: Set fso = Nothing
 ' strFileSpec, 1 : ForReading 2 : ForWriting  8 : ForAppending