A better way of doing this?

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
The code below works perfectly, what it does is read each line of a file, then copies the entire contents of that file to another file. The idea of the program is to combine 500 or so files i have it at work into one big long file.

The purpose of this post, is to ask if theres a better way of doing his, rather than streamreader within the for loop, as i dont like having declarations mid-way through some code.


Code:
       sets up to write to a file
        Dim path As String = txtFolder.Text
        Dim strTextToFile As New System.IO.StreamWriter(txtFolder.Text + "\tbls.txt", False)
        Dim strNewFile As String

        sets up to read list of files
        Dim sr As StreamReader = New StreamReader(txtFolder.Text + "\tblslist.txt")

        Do While sr.Peek() >= 0
            MsgBox(sr.ReadLine)
            opens the reader for each file
            Dim reader As New System.IO.StreamReader(sr.ReadLine())
            Dim textfromfile As String = reader.ReadToEnd()

            Adds the files up to one big one
            strNewFile = strNewFile & textfromfile & Chr(13)
            reader.Close()

        Loop
        sr.Close()

        writes to file and closes
        strTextToFile.WriteLine(strNewFile)
        strTextToFile.Close()

        MsgBox("Document Finished!", MsgBoxStyle.Information)

Thanks.
 
why not try this ...
Code:
        Dim sReader As New IO.StreamReader(New IO.FileStream("C:\your_textfile_path.txt", IO.FileMode.Open))
        Dim sWriter As New IO.StreamWriter("C:\my_file.txt")

        sWriter.Write(sReader.ReadToEnd)
        sWriter.Close()
        sReader.Close()

        MessageBox.Show("Done!")
 
Back
Top