Group,

I can't seem to append multiple files into one text file. Unfortunately it's creating the first file but is not creating or appending the additional files into the first file. My code looks like this:

                getRestranName = System.IO.Directory.GetFiles(folderName)
                Dim counter As Integer = My.Computer.FileSystem.GetFiles(folderName).Count

                fileCount = Convert.ToInt32(counter)
                fileCount = fileCount - 1
                hotelFolder = CStr(getFolderNameGD("O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels", propertyNo2 & "*"))

                ' Begin by setting the loop to read each file in folder
                For i = 0 To fileCount
                    fileName = ""
                    Dim objReader1 As New System.IO.StreamReader(getRestranName(i))
                    fileName = getRestranName(i)
                    If System.IO.File.Exists(fileName) Then
                        Do While objReader1.Peek() <> -1
                            txtLine = objReader1.ReadLine()
                            programName = Trim(Microsoft.VisualBasic.Left(txtLine, 14))

                            If lineNo = 3 And programName = "(res.restran)" Then
                                restranSave = "C:\Restran Conversion\Restran\" & propertyNo & "restran.txt"
                                If i = 0 Then      'This reads the first file in the folder.
                                    lblProgName.Text = "Copying " & fileName & "......"
                                    System.IO.File.Copy(fileName, restranSave, True)
                                    objReader1.Dispose()
                                    objReader1.Close()
                                    lblProgName.Text = "Saved temp file " & restranSave & "......"
                                    System.IO.File.Delete(fileName)
                                Else    'This should read any the second through the last file and append to the first (called "restranSave")
                                    System.IO.File.AppendAllText(restranSave, fileName)
                                    objReader1.Dispose()
                                    objReader1.Close()
                                    System.IO.File.Delete(fileName)
                                End If
                            End If

                            lineNo = lineNo + 1
                            If lineNo = 4 Or lineNo > 4 Then
                                lineNo = 1
                                Exit Do
                            End If

                     Loop    'Loop to read the next line of the "i" file

                    End If

                Next i            'Takes you to the next file in the same folder 

            End If

Does it appear I'm missing something? The first file gets read and copied to "restranSave". But the second, third (and so on) are not getting appended to "restranSave". Help!

As always, thanks for your assistance.

Don

Recommended Answers

All 8 Replies

Line 28 looks suspect to me.

https://msdn.microsoft.com/en-us/library/system.io.file.appendalltext%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 writes:
"Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file."

That's not what your comment on line 27 says you want to do in line 28.

That word, it doesn't appear to mean what you think it means.

Line 23 & 24:
Can you close an objReader1 after it is disposed? Same on lines 29&30

@rproffitt and @ddanbe, you prompt me to ask a couple of questions:

Line 20 is saying (I hope) that If i = 0. "i" should be the first file in the folder. I want that first file saved as "restranSave" (a path). Line 27 is the Elseto line 20. In other words, "If i > 0" (meaning the 2nd, 3rd, etc. file in the folder) then I want to append this the the "restranSave" file path. Is that NOT what I'm doing? Should I be coding this different?

I will eliminate the "Dispose" and just stick with "Close" at 23 and 29.

As always, thanks for your help.

Don

I am not a fan of special case programming. It clutters up the code. Why not create an empty file outside the loop and then just append all of the files? That way you don't need separate code for "first file" and "all of the other files".

Rev. Jim, do you think it would be a problem to do this? The program runs Monday thru Friday. During the week there is only one file to update. Monday morning is the only day (generally) that there are multiple file to merge. FYI... I'm all for simple!

Thanks again for your input.

Don

If there is only one file then the loop will execute once and append the single file.

commented: Simple enough! +15

A much simpler solution would be something like

Dim srcFolder As String = "d:\temp\don"
Dim ConcatFile As String = "d:\temp\don\concat.txt"

For Each file As String In My.Computer.FileSystem.GetFiles(srcFolder, FileIO.SearchOption.SearchTopLevelOnly, "file*.txt")
    Dim text As String = System.IO.File.ReadAllText(file)
    System.IO.File.AppendAllText(ConcatFile, text)
Next

Yes, this causes the output file to be opened and closed multiple times, but the simplicity of the code far outweighs the very minor performance hit. Modify it as appropriate for your directory and file structure. The third parameter in GetFiles is a file pattern to restrict the input to files of a specific naming pattern. Use *.* for all files (but then you'll likely have to put the Concat file in a different folder). Another advantage is that AppendAllText will create the ConcatFile if it doesn't already exist.

Rev Jim,

Brilliant. I see also I wasn't reading the text file to append it to the other file.

Thanks for the help. This is awesome!

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.