Group,

You've helped me write a routine that merges and formats a text file when multiple files exist. The code is as follows:

                If fileCount = 3 Then
                    RestranName = getRestranName(0)
                    RestranName2 = getRestranName(1)
                    RestranName3 = getRestranName(2)
                    Dim readtxt() As String = File.ReadAllLines(RestranName)
                    'Deleted the actual file.
                    File.Delete(RestranName)
                    'Now time to read the array elements and save them in a file.
                    For i As Integer = readtxt.GetLowerBound(0) To readtxt.GetUpperBound(0) - 4
                        'Appending the line to the text file
                        My.Computer.FileSystem.WriteAllText(RestranName, readtxt(i), True)
                        If i < readtxt.GetUpperBound(0) - 4 Then
                            'Appending a new line into the text file.
                            My.Computer.FileSystem.WriteAllText(RestranName, vbCrLf, True)
                        End If
                    Next
                    My.Computer.FileSystem.WriteAllText(RestranName, pageCode, True)


                    Me.Cursor = Cursors.Default
                    Dim readtxt2() As String = File.ReadAllLines(RestranName2)
                    'Deleted the actual file.
                    File.Delete(RestranName2)
                    'Now time to read the array elements and save them in a file.
                    For i As Integer = readtxt2.GetLowerBound(0) To readtxt2.GetUpperBound(0) - 4
                        'Appending the line to the text file
                        My.Computer.FileSystem.WriteAllText(RestranName2, readtxt2(i), True)
                        If i < readtxt2.GetUpperBound(0) - 4 Then
                            'Appending a new line into the text file.
                            My.Computer.FileSystem.WriteAllText(RestranName2, vbCrLf, True)
                        End If
                    Next
                    My.Computer.FileSystem.WriteAllText(RestranName2, pageCode, True)
                    File.AppendAllText(RestranName, System.IO.File.ReadAllText(RestranName2))
                    File.AppendAllText(RestranName, File.ReadAllText(RestranName3))
                    File.Delete(RestranName2)
                    File.Delete(RestranName3)
                End If

                ' setting the path name to save the converted file
                fileSave = "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\" & hotelFolder & "\Restran\" & propertyNo & "Restran.txt"
                ' Getting date extension to save the file in a history 

                yesterday = Today.AddDays(-1)
                day = yesterday.Day.ToString
                month = yesterday.Month.ToString
                year = yesterday.Year.ToString
                year = year.Substring(2, 2)
                If yesterday.Day < 10 Then
                    day = "0" & day
                End If
                    If yesterday.Month < 10 Then
                    month = "0" & month
                End If
                copyFile = "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\" & hotelFolder & "\Restran\Restran History\" & propertyNo & " Restran " & month & day & year & ".txt"
                ' This reads the text file for conversion
                txtLine = My.Computer.FileSystem.ReadAllText(RestranName)
                ' This begins to add the carriage returns in the appropriate places
                txtLine = Replace(txtLine, pageCode, vbCrLf & pageCode)
                txtLine = Replace(txtLine, vbLf, vbCrLf)
                txtLine = Replace(txtLine, vbCr & vbCr, vbCr)
                txtLine = Replace(txtLine, """", "")
                ' This writes the line to the file
                My.Computer.FileSystem.WriteAllText(fileSave, txtLine, False)
                System.IO.File.Copy(fileSave, copyFile)
                ' This deletes the old unconverted file
                System.IO.File.Delete(RestranName)

My latest issue has to do whether the program is actually stopped or not. During run-time, I'm getting a notice at the top of the user interface (next to the program name) that the program has stopped. This is only happening on days were there are multiple text files to merge.

Given what the code is doing on these days, it's taking several minutes to go through the routine. You probably need to know the routine is looping multiple times to capture the next property number in the sequence. Generally after the 1st or second property (truthfully I can't actually tell when it's saying it's stopped), is when I get the notice that it's stopped. But then again, has it really? I say that as it seems to be going through the routine but I never get the message that says it's completed the tasks.

I'm sure someone has seen this before. Any thoughts on how to make sure everything runs correctly and completely? FYI... it runs perfectly and very quickly when there is only one text file to format in each property folder.

Thoughts?

Thanks for the help.

Don

The usual approach is to add

Debug.WriteLine("stuff")

lines at strategic places inside the loop, then run the code to see what is happening. Either that or step through the code in the debugger with breakpoints.

By the way, a shorter method of adding a leading zero is the Format function as in

Dim month As String = Format(yesterday.Month, "00")

It sounds like the Windows message that occurs during long running tasks. This occurs because you are executing the long running task on the main thread. The long running operation can occur for multiple reasons. One reason is because it takes that long to complete. Another reason could be resource contention. You should use BackgroundWorkerThread for long running operations.

See the following:
How to use BackGroundWorker to Create Threads

Nothing in that code looks like it requires the complexity of a background thread, especially considering the ability of the OP. Debugging a single threaded program can be difficult enough.

Don - would it be possible for you to upload all three data files?

Hi Don

For a colleague still at work I was happy to do a somewhat similar job as yours.
I used an extra dialogwindow with a label with as text a notice which file was being processed. This dialog also had a progressbar control.
Maybe this is something you might be looking for?

Based on his level of expertise, I would suggest NOT adding complexity by involving additional controls, especially ones that did not come with VB. Adding print statements (in this case Debug.WriteLine) is the simplest method.

If your program fails, how can you re-run it if you delete the original files or overwrite them?

Sorry for the delay in responding. It's been an extremely busy week.

I believe the idea of debug.writeline is the best approach to finding where it may be stopping. I didn't think the program was overly complex either, but when it is deleting lines and merging more than two or three files, it really boggs down. On Monday I'll upload the three individual files that are to be merged. I'll also do a "after" merge upload for you to see.

Thinking out loud - I'll also drop the whole code into a text file so you can see everything I've done. Feel free to offer ideas on how to improve any portion of the program. Clearly I'm very inexperienced and have minimal training (two online courses in VB at my local college). I'd love to improve to the point that I might be useful to someone!

Group, thanks for all you do. You're the best!

Don

Group,

I'm attaching the three files that are to be merged. The fourth is the merged file. While merging these files I again got the message that the program wasn't responding. However it eventually went through - just not as quickly as I thought it should.

I'm also attaching the complete code. Feel free to critique it. I'm sure there are things that I could have done more efficiently and cleaner. Feel free to offer suggestions.

Thanks for your interest. I certainly appreciate your help!

Don

I can't run this locally because you supplied only the code that runs the form. I am not going to create all the controls manually and guess as to their placement or grouping. Please zip the project folder and post it.

The simplest code to just merge the files would be

Dim filelist() As String = {"d:\temp\don\20150228.txt", "d:\temp\don\20150301.txt", "d:\temp\don\20150302.txt"}

Dim sw As New System.IO.StreamWriter("d:\temp\don\out.txt", False)

For Each file As String In filelist

    Dim lines() As String = System.IO.File.ReadAllLines(file)

    For i = 0 To UBound(lines) - 4
        sw.WriteLine(lines(i))
    Next

Next

sw.WriteLine("")
sw.WriteLine("")
sw.WriteLine("                                                       End of Report")
sw.Close()

Note that I just dummied in three files. If you create a list of file names by scanning the folder and use the same loop then it should work no matter how many files are in the list.

commented: Pro speaking here! +15

Maybe also replace line 29: If Len(Trim(txt.Text)) Then with
If Len(Trim(txt.Text)) > 0 Then

Please turn "Option Explicit" on in your VS. It's not a good idea to use variables without declaring them.

In VS 2008/2010:
Click "Tools" in menu bar
Select "Options"
Double-click "Projects and Solutions" to expand it
Click "VB Defaults"
Change "Option Explicit" to "On"
Click "OK"

Also, in looking through your code, it appears that some of your code has gotten out of hand. It isn't apparent what the purpose of "RestranInformationManager.txt" and "RestranFileConversion.txt" are, nor did you include them. It seems that perhaps it is providing information that can be extracted by reading directory contents (ie: folder names and/or filenames)--according to some of your other posts.

What are the 20 textboxes and 20 labels for?
Labels:
-lblProp1
-lblProp2
...
-lblProp20

TextBoxes:
-tbxProp1
-tbxProp2
...
-tbxProp20

It looks like you are using 7 buttons--some of which seem might be better as options in a MenuStrip.

Buttons:
-btnEditPropList
-btnLockPropList
-btnClearAllProp
-btnPropEditor
-btnConvertFiles
-btnInfoMgr
-btnExitSub

It appears that you may be using the wrong encoding when reading and writing the files.

File.ReadAllText Method (String)
...This method attempts to automatically detect the encoding of a file based on the presence of byte order marks...

Byte order mark
...The UTF-8 representation of the BOM is the byte sequence 0xEF, 0xBB, 0xBF...

The following post: What are the differences between Linux and Windows .txt files (Unicode encoding) references some tools that can be used to determine file encoding. I used File for Windows on the original text file you posted (20150228.txt), and it showed "ASCII text". I also used "xxd" (a Cygwin download) which showed the following: 466f757220506f696e747320...

More easily read: 46 6f 75 72 20 50 6f 69 6e 74 73 20...

If you look these (hex codes) up in an ASCII table, you'll see the following:
46=F
6f=o
75=u
72=r
20=space
50=P
6f=o
69=i
6e=n
74=t
73=s
20=space

Note: As you can see, the original file (20150228.txt) doesn't contain a byte order mark.

When I used I used File for Windows on the "converted" (786Restran.txt) file you posted , it showed "UTF-8 Unicode (with BOM) text, with CRLF line terminators". I also used "xxd" (a Cygwin download) which showed the following: efbbbf466f757220506f696e747320...

More easily read: ef bb bf 46 6f 75 72 20 50 6f 69 6e 74 73 20...

As referenced above, "ef bb bf" (efbbbf) is the byte order mark for UTF-8, which is basically pre-pended to what was in the original file--46 6f 75...is how the original file (20150228.txt) begins.

So, it appears that you should be specifying the file encoding when reading/writing these files. Use the following File.ReadAllText Method (String, Encoding) instead -- where Encoding = Encoding.ASCII

LikeWise, File.ReadLines Method (String, Encoding) -- where Encoding = Encoding.ASCII.

FileSystem.ReadAllText Method (String, Encoding) -- where Encoding = Encoding.ASCII

File.WriteAllText Method (String, String, Encoding) -- where Encoding = Encoding.ASCII

FileSystem.WriteAllText Method (String, String, Boolean, Encoding) -- where Encoding = Encoding.ASCII

...Encoding...Default is UTF-8...

Therefore, if encoding isn't specified, it's defaulting to UTF-8.

Note: An easier way to see if the encoding is UTF-8, is to open the file in Notepad. Then in the menu bar, click "File. Select "Save As". To the left of the "Save" button you'll see "Encoding". If it says "ANSI", then the encoding is "ASCII". If it says, "UTF-8", then the file encoding is UTF-8. Now, click "Cancel".

After reading through some of your posts, I have some suggestions:

Rather than doing this:

        Dim propertyNo As String = "1540"
        Dim propertyNo1 As String = String.Empty
        Dim strLength As Integer = propertyNo.Length

        If strLength = 2 Then
            propertyNo1 = "000" & propertyNo
        End If
        If strLength = 3 Then
            propertyNo1 = "00" & propertyNo
        End If
        If strLength = 4 Then
            propertyNo1 = "0" & propertyNo
        End If
        If strLength = 5 Then
            propertyNo1 = propertyNo
        End If

You can do this:

        Dim propertyNo As String = "1540"
        Dim propertyNo1 As String = String.Empty

        'convert property number to 5 digits
        If IsNumeric(propertyNumber) Then
            'convert propertyNumber to an integer 
            'so it can be converted
            'to a 5-digit string using "ToString"
            propertyNo1 = Convert.ToInt32(propertyNumber).ToString("D5")
        End If

Also, the following code should be all that you need to "merge" your files. Unfortunately, there were some things that I was unable to determine, based on the information that you've provided so far--such as the directory name / filename structure. This would be necessary to determine which parts can be automated and which parts require user input. I was also unable to determine if you want the user to be able to decide which files to merge or which properties to merge. Additionally, I was uncertain if a merged file should only contain information for that particular property, or if more than one property can be part of a single (merged) file.

MergeFile:

Private Sub MergeFile(ByVal inputFilename As String, ByVal inputFileEncoding As Encoding, ByVal outputFilename As String, ByVal outputFileEncoding As Encoding, ByVal addFormFeedToFirstPage As Boolean, ByVal removeEndOfReport As Boolean)
    'read input file data into a string array
    Dim fileDataArr() As String = File.ReadAllLines(inputFilename, inputFileEncoding)

    'add form feed to first page of all files 
    'except the first one;
    'addFormFeedToFirstPage should be set to 
    'True for all files except the first one
    If addFormFeedToFirstPage Then
        'Chr(12) is ASCII code 0C or 12
        fileDataArr(0) = Chr(12) & fileDataArr(0)
    End If

    'if necessary, remove lines at end of report - 
    '"End of Report" and some blank lines
    If removeEndOfReport Then
        ReDim Preserve fileDataArr(fileDataArr.Length - 4)
    End If

    'add Newline (carriage return) to each line
    Dim outputDataStr As String = Join(fileDataArr, System.Environment.NewLine)

    'append data to output file
    File.AppendAllText(outputFilename, outputDataStr, outputFileEncoding)

End Sub

MergeRestranFiles:

Public Sub MergeRestranFiles(ByVal filenameList As List(Of String), ByVal inputFileEncoding As Encoding, ByVal outputFilename As String, ByVal outputFileEncoding As Encoding)
    Dim addFormFeedToFirstPage = False

    'delete previous output file
    If System.IO.File.Exists(outputFilename) Then
        System.IO.File.Delete(outputFilename)
    End If

    'use loop to merge files
    For i As Integer = 0 To filenameList.Count - 1

        'when merging files, add Form Feed (0C) 
        'to beginning of all files except the 
        'first one
        If i = 0 Then
            addFormFeedToFirstPage = False
        Else
            addFormFeedToFirstPage = True
        End If

        'merge files
        If i < filenameList.Count - 1 Then
            'merge file and remove "End Of Report" /
            'extra blank lines
            MergeFile(filenameList(i), inputFileEncoding, outputFilename, outputFileEncoding, addFormFeedToFirstPage, True)
        Else
            'merge file and keep "End of Report" / 
            'extra blank lines
            MergeFile(filenameList(i), inputFileEncoding, outputFilename, outputFileEncoding, addFormFeedToFirstPage, False)
        End If
    Next

    Debug.WriteLine("Saved to: " & outputFilename)
End Sub

Usage:

Dim filenameList As New List(Of String)

filenameList.Add("C:\Restran Conversion\1540\20150228.txt")
filenameList.Add("C:\Restran Conversion\1540\20150301.txt")
filenameList.Add("C:\Restran Conversion\1540\20150302.txt")

'merge files in filenameList
MergeRestranFiles(filenameList, Encoding.ASCII, outputFilename, Encoding.ASCII)

See my post in Character Identification regarding the symbol that can be seen at the beginning of some of the lines (that contain "Page Number:") when viewing your original files (ex: "20150228.txt") in Notepad.

Sub code is attached.

I made some modifications to the code above, made "inputFileEncoding" and "outputFileEncoding" properties instead of parameters, added error handling/logging capabilities, and added more documentation.

Need the following imports:
Imports System.IO
Imports System.Text

Add the following to the module:

Public Property InputFileEncoding As Encoding = Encoding.ASCII
Public Property OutputFileEncoding As Encoding = Encoding.ASCII

Public Property LogFilename As String = "RestranLog.txt"

'holds last error message
Private _errMsg As String = String.Empty

'StreamWriter for log file
Private _swLogFile As StreamWriter = Nothing

Private _outputFilename As String = String.Empty

MergeFile:

Private Sub MergeFile(ByVal inputFilename As String, ByVal outputFilename As String, ByVal addFormFeedToFirstPage As Boolean, ByVal removeEndOfReport As Boolean)
    Dim fileDataArr() As String = Nothing
    Dim logMsg As String = String.Empty

    Try
        'read input file data into a string array
        fileDataArr = File.ReadAllLines(inputFilename, InputFileEncoding)

        'add form feed to first page of all files 
        'except the first one addFormFeedToFirstPage 
        'should be set to True for all files except
        'the first one
        If addFormFeedToFirstPage Then
            'Chr(12) is ASCII code 0C or 12
            fileDataArr(0) = Chr(12) & fileDataArr(0)
        End If

        'if necessary, remove lines at end of report - 
        '"End of Report" and some blank lines
        If removeEndOfReport Then
            'resize array removing last 4 elements (lines)
            ReDim Preserve fileDataArr(fileDataArr.Length - 4)
        End If

        'add Newline (carriage return) to each line
        Dim outputDataStr As String = Join(fileDataArr, System.Environment.NewLine)

        'append data to output file
        File.AppendAllText(outputFilename, outputDataStr, OutputFileEncoding)

        'make log file entry
        logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " File: " & inputFilename & " merged to: " & outputFilename
        _swLogFile.WriteLine(logMsg)

    Catch ex As Exception
        _errMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: MergeFile(1) -- " & "Input file: '" & inputFilename & "' Output file: '" & outputFilename & "' ErrMsg: " & ex.Message

        'make log file entry
        _swLogFile.WriteLine(_errMsg)
        Debug.WriteLine(_errMsg)
    End Try
End Sub

MergeRestranFiles:

Public Sub MergeRestranFiles(ByVal inputFilenameList As List(Of String), ByVal outputFilename As String)
    Dim addFormFeedToFirstPage As Boolean = False
    Dim outputFileDir As String = String.Empty
    Dim logMsg As String = String.Empty

    Try
        're-intialize
        _errMsg = String.Empty

        'open StreamWriter to log file (for appending)
        _swLogFile = New StreamWriter(LogFilename, True)

        'get directory name
        outputFileDir = Path.GetDirectoryName(outputFilename)

        'delete previous output file
        If File.Exists(outputFilename) Then
            File.Delete(outputFilename)
        End If

        'create outputFileDir if it doesn't exist
        If Not Directory.Exists(outputFileDir) Then
            Directory.CreateDirectory(outputFileDir)
        End If

        'use loop to merge files
        For i As Integer = 0 To inputFilenameList.Count - 1

            'when merging files, add Form Feed (0C) to 
            'beginning of all files except the first one
            If i = 0 Then
                addFormFeedToFirstPage = False
            Else
                addFormFeedToFirstPage = True
            End If

            'merge files
            If i < inputFilenameList.Count - 1 Then
                'merge file and remove "End Of Report" /
                'extra blank lines
                MergeFile(inputFilenameList(i), outputFilename, addFormFeedToFirstPage, True)
            Else
                'merge file and keep "End of Report" / 
                'extra blank lines
                MergeFile(inputFilenameList(i), outputFilename, addFormFeedToFirstPage, False)
            End If
        Next

        If String.IsNullOrEmpty(_errMsg) Then
            logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Info: Operation completed successfully."
        Else
            logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: Operation failed."
        End If

        'make log file entry
        _swLogFile.WriteLine(logMsg)

        Debug.WriteLine(logMsg)
    Catch ex As Exception
        _errMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: MergeRestranFiles -- " & ex.Message

        'make log file entry
        _swLogFile.WriteLine(_errMsg)
        Debug.WriteLine(_errMsg)

    Finally

        'close log file StreamWriter
        If Not _swLogFile Is Nothing Then
            _swLogFile.Close()
        End If
    End Try
End Sub

Usage:

Dim inputFilenameList As New List(Of String)
Dim propertyNumber As String = String.Empty
Dim outputFilename As String = "C:\Restran Conversion\1540\RestranConverted.txt"

inputFilenameList.Add("C:\Restran Conversion\1540\20150228.txt")
inputFilenameList.Add("C:\Restran Conversion\1540\20150301.txt")
inputFilenameList.Add("C:\Restran Conversion\1540\20150302.txt")

MergeRestranFiles(inputFilenameList, outputFilename)
'MergeRestranFilesA(inputFilenameList, outputFilename)

Here is an alternate version of "MergeFile and "MergeRestranFiles"--they leave a StreamWriter open to the output file until all writing is complete (eliminating multiple opening/closing of the same output file).

MergeFile:

Private Sub MergeFile(ByVal inputFilename As String, ByVal swOutputFile As StreamWriter, ByVal addFormFeedToFirstPage As Boolean, ByVal removeEndOfReport As Boolean)
    Dim fileDataArr() As String = Nothing
    Dim logMsg As String = String.Empty

    Try
        'read input file data into a string array
        fileDataArr = File.ReadAllLines(inputFilename, InputFileEncoding)

        'add form feed to first page of all files 
        'except the first one addFormFeedToFirstPage 
        'should be set to True for all files except
        'the first one
        If addFormFeedToFirstPage Then
            'Chr(12) is ASCII code 0C or 12
            fileDataArr(0) = Chr(12) & fileDataArr(0)
        End If

        'if necessary, remove lines at end of report - 
        '"End of Report" and some blank lines
        If removeEndOfReport Then
            'resize array removing last 4 elements (lines)
            ReDim Preserve fileDataArr(fileDataArr.Length - 4)
        End If

        'add Newline (carriage return) to each line
        Dim outputDataStr As String = Join(fileDataArr, System.Environment.NewLine)

        'append data to output file
        swOutputFile.WriteLine(outputDataStr)

        'make log file entry
        logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " File: " & inputFilename & " merged to: " & _outputFilename
        _swLogFile.WriteLine(logMsg)

    Catch ex As Exception
        _errMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: MergeFile(2) -- " & "Input file: '" & inputFilename & "' Output file: '" & _outputFilename & "' ErrMsg: " & ex.Message

        'make log file entry
        _swLogFile.WriteLine(_errMsg)
        Debug.WriteLine(_errMsg)
    End Try

End Sub

I've named this verion of "MergeRestranFiles" "MergeRestranFilesA".

MergeRestranFilesA:

Public Sub MergeRestranFilesA(ByVal inputFilenameList As List(Of String), ByVal outputFilename As String)
    Dim addFormFeedToFirstPage As Boolean = False
    Dim outputFileDir As String = String.Empty
    Dim swOutputFile As StreamWriter = Nothing
    Dim logMsg As String = String.Empty

    Try
        're-intialize
        _errMsg = String.Empty

        'open StreamWriter to log file (for appending)
        _swLogFile = New StreamWriter(LogFilename, True)

        'get directory name
        outputFileDir = Path.GetDirectoryName(outputFilename)

        'delete previous output file
        If File.Exists(outputFilename) Then
            File.Delete(outputFilename)
        End If

        'create outputFileDir if it doesn't exist
        If Not Directory.Exists(outputFileDir) Then
            Directory.CreateDirectory(outputFileDir)
        End If

        'set _outputFilename; needed for MergeFile
        _outputFilename = outputFilename

        'open StreamWriter to output file (for appending)
        swOutputFile = New StreamWriter(outputFilename, True)

        'use loop to merge files
        For i As Integer = 0 To inputFilenameList.Count - 1

            'when merging files, add Form Feed (0C) to 
            'beginning of all files except the first one
            If i = 0 Then
                addFormFeedToFirstPage = False
            Else
                addFormFeedToFirstPage = True
            End If

            'merge files
            If i < inputFilenameList.Count - 1 Then
                'merge file and remove "End Of Report" /
                'extra blank lines
                MergeFile(inputFilenameList(i), swOutputFile, addFormFeedToFirstPage, True)
            Else
                'merge file and keep "End of Report" / 
                'extra blank lines
                MergeFile(inputFilenameList(i), swOutputFile, addFormFeedToFirstPage, False)
            End If
        Next

        If String.IsNullOrEmpty(_errMsg) Then
            logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Info: Operation completed successfully."
        Else
            logMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: Operation failed."
        End If

        'make log file entry
        _swLogFile.WriteLine(logMsg)

        Debug.WriteLine(logMsg)
    Catch ex As Exception
        _errMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & " Error: MergeRestranFilesA -- " & ex.Message

        'make log file entry
        _swLogFile.WriteLine(_errMsg)
        Debug.WriteLine(_errMsg)

    Finally
        'close output file StreamWriter
        If Not swOutputFile Is Nothing Then
            swOutputFile.Close()
        End If

        'close log file StreamWriter
        If Not _swLogFile Is Nothing Then
            _swLogFile.Close()
        End If
    End Try
End Sub

Usage for "MergeRestranFilesA" is the same as "MergeRestranFiles".

Sub code attached below.

@Don: Your code is too some lengthy and it is not possible to create a project for testing your codes and fide the causes of exceptions.
But I just tried to modify your codes for File Conversion. The codes are

    Private Sub btnConvertFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnConvertFiles.Click
        Me.Text = "Restran File Conversion (Running)"

        Dim getRestranName() As String

        ' setting the path to find the file to convert
        '   Reading for File information
        infoManager = "C:\Restran Conversion\RestranInformationManager.txt"
        FILE_NAME = "C:\Restran Conversion\RestranFileConversion.txt"
        Dim i As Integer = 1

        If System.IO.File.Exists(infoManager) = True Then
            Dim objReader As New System.IO.StreamReader(infoManager)
            Do While objReader.Peek() <> -1
                lineRead = objReader.ReadLine()
                If i = 1 Then
                    filePath = lineRead
                End If
                If i = 2 Then
                    filePrefix = lineRead
                End If
                If i = 3 Then
                    fileSuffix = lineRead
                End If
                If i = 4 Then
                    ResListPath = lineRead
                End If
                i = i + 1
            Loop
            objReader.Close()
        Else
            Select Case MessageBox.Show("The Information Manager,  failed to connect" & "Do you want to continue?", "File Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                Case Windows.Forms.DialogResult.No
                    Exit Sub
            End Select
        End If

        Dim pageCode As Char = Microsoft.VisualBasic.ChrW(&H2640)

        Me.Cursor = Cursors.WaitCursor

        Dim propertyCount As Integer = File.ReadAllLines(FILE_NAME).Length
        ' Begin Updating and converting the multiple properties.
        propNo = 1
        Do While propNo > propertyCount
            For Each crtl As Control In Me.Controls
                If (crtl.Name = "tbxProp" & propNo) Then
                    propertyNo = crtl.Text
                    Exit For
                End If
            Next

            If propertyNo.Length < 5 Then
                propertyNo1 = CStr(Format(Val(propertyNo), "00000"))
                propertyNo2 = CStr(Format(Val(propertyNo), "0000"))
            Else
                propertyNo1 = propertyNo
                propertyNo2 = propertyNo
            End If

            folderName = filePath & "\" & filePrefix & propertyNo1 & fileSuffix
            If Not System.IO.Directory.Exists(folderName) Then
                Select Case MessageBox.Show("The folder selected" & Environment.NewLine & "'" & folderName & "'" & Environment.NewLine & " does not exist. To continue, click 'Yes'.", "Folder Error", MessageBoxButtons.YesNo)
                    Case Windows.Forms.DialogResult.No
                        Select Case MessageBox.Show("Reset the 'Property List'?", "Folder Error", MessageBoxButtons.YesNo)
                            Case Windows.Forms.DialogResult.Yes
                                Call TextBoxVisible()
                                Exit Do
                        End Select
                End Select
            Else
                getRestranName = System.IO.Directory.GetFiles(folderName)
                fileCount = My.Computer.FileSystem.GetFiles(folderName).Count
                If fileCount = 0 Then
                    MessageBox.Show("'" & folderName & "' does not contain a RESTRAN to be converted.", "Property #" & propertyNo, MessageBoxButtons.OK)
                    Continue Do
                End If
                If fileCount > 1 Then

                    For x As Integer = getRestranName.GetLowerBound(0) To getRestranName.GetUpperBound(0)
                        If fileCount > 1 Then
                            Dim readtxt() As String = System.IO.File.ReadAllLines(getRestranName(x))
                            File.Delete(getRestranName(x))

                            For y As Integer = readtxt.GetLowerBound(0) To readtxt.GetUpperBound(0) - 4
                                My.Computer.FileSystem.WriteAllText(getRestranName(0), readtxt(y), True)
                                If y < readtxt.GetUpperBound(0) - 4 Then
                                    'Appending a new line into the text file.
                                    My.Computer.FileSystem.WriteAllText(getRestranName(0), vbCrLf, True)
                                End If
                            Next
                            If x < fileCount - 1 Then
                                'Appending the spacial charactor and feed a new line
                                My.Computer.FileSystem.WriteAllText(getRestranName(0), vbCrLf & pageCode, True)
                            End If
                        End If
                    Next

                End If

                ' This finds the folder to where to drop the saved restran file

                Using reader As New StreamReader("O:\IPSDATA\Property Folder Names.txt")
                    While Not reader.EndOfStream
                        Dim line As String = reader.ReadLine()
                        If line.Contains(propertyNo2) Then
                            hotelFolder = line
                            Exit While
                        End If
                    End While
                End Using

                ' setting the path name to save the converted file
                fileSave = "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\" & hotelFolder & "\Restran\" & propertyNo & "Restran.txt"
                ' Getting date extension to save the file in a history 

                Dim yesterday As Date
                Dim day As String = ""
                Dim month As String = ""
                Dim year As String
                Dim copyfile As String
                Dim txtline As String

                yesterday = DateAdd(DateInterval.Day, -1, Today.Date)
                day = CStr(Format(yesterday.Day, "00"))
                month = CStr(Format(yesterday.Month, "00"))
                year = yesterday.Year.ToString
                year = year.Substring(2, 2)
                copyfile = "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\" & hotelFolder & "\Restran\Restran History\" & propertyNo & " Restran " & month & day & year & ".txt"
                ' This reads the text file for conversion
                txtline = My.Computer.FileSystem.ReadAllText(getRestranName(0))
                ' This begins to add the carriage returns in the appropriate places
                txtline = Replace(txtline, vbLf, vbCrLf)
                txtline = Replace(txtline, vbCr & vbCr, vbCr)
                txtline = Replace(txtline, """", "")
                ' This writes the line to the file
                My.Computer.FileSystem.WriteAllText(fileSave, txtline, False)
                System.IO.File.Copy(fileSave, copyfile)
                ' This deletes the old unconverted file
                System.IO.File.Delete(getRestranName(0))
            End If
            propNo += 1
        Loop

        'Next propNo
        Me.Cursor = Cursors.Default
        Dim result3 As DialogResult = MessageBox.Show("File conversion has completed.", "Conversion Complete", MessageBoxButtons.OK)
        Me.Text = "Restran File Conversion"
    End Sub

Hope it can help you.

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.