Results 1 to 2 of 2

Thread: textstream/filesystemobject

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Location
    cleveland
    Posts
    30

    Question textstream/filesystemobject

    Okay;
    I have two files. File one contains header info. File two contains 8 records (13 lines per record).

    I need to append the contents of "Kronos.txt(file two) into Header.txt(file one). I have never used filesystemobject or textstream before. Can someone tell me what I am doing wrong here or am I going about this the wrong way?

    Thanks

    Public sub OpenMe()
    Dim mystr1 As String
    Dim FileSystemObject, TextStream As Object, tStream As Object

    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set TextStream = FileSystemObject.createtextfile("c:\testing\kronos.txt")
    Set TextStream = FileSystemObject.OpenTextFile("c:\testing\kronos.txt, ForAppending")
    Set tStream = FileSystemObject.createtextfile("c:\testing\header.txt")
    Set tStream = FileSystemObject.OpenTextFile("c:\testing\header.txt, ForReading")
    Do While Not EOF(2)
    mystr1 = TextStream.readline
    tStream.Writeline = mystr1
    Loop
    End sub
    I wanted to say something profound and
    all I came up with is this -->> "Eat dessert
    first, life is short"

  2. #2
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    I find it best to set a reference to the microsoft scripting runtime. It makes things much easier to deal with and you can autocomplete your methods/properties. Here's what I did:
    Code:
    'Set up the FileSystemObject and the TextStreams
    Dim fso As FileSystemObject
    Dim txtstream1 As TextStream
    Dim txtstream2 As TextStream
    
    Private Sub Form_Load()
        'Create our temp string so that we have some place to dump
        'the file, also setup the FileSystemObject
        Dim temptext As String
        Set fso = New FileSystemObject
        
        'Open our files for reading
        Set txtstream1 = fso.OpenTextFile("c:\testing\kronos.txt", ForReading)
        Set txtstream2 = fso.OpenTextFile("c:\testing\header.txt", ForReading)
        
        'Load in the files to our temp string and join them with a
        'carriage return + line feed combo
        temptext = txtstream1.ReadAll
        temptext = temptext & vbCrLf & txtstream2.ReadAll
        
        'close the files
        txtstream1.Close
        txtstream2.Close
        
        'reopen our header.txt file for writing.
        Set txtstream2 = fso.OpenTextFile("c:\testing\header.txt", ForWriting)
        
        'a quick debug to make sure everything is ok
        Debug.Print temptext
        
        'write our temp string out to the header file
        txtstream2.Write temptext
        
        'close the file
        txtstream2.Close
        
        'set our filesystem objects to nothing for cleanup.
        Set txtstream1 = Nothing
        Set txtstream2 = Nothing
        Set fso = Nothing
    End Sub
    It's fully commented so you shouldn't have much problems following along with what I did.
    -Excalibur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width