Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hellow,
I'm an intermediate VB.Net programmer and I need assistant to get a project done. I have a request as follows.

1. Confirm that source files exist (the text files)
Obtain value of field, Location in File_Locations table where code = 'TEST'. If the source files don't exists in this location then end the processing, else proceed to 2 below.
2.Build File Name
Obtain value of the these fields from Control_Table; File_Base, FileSeqNo, FileSuffix. And create the filename as File_Base_FileSeqNo_FileSuffix.
3 Write the Header and Footer details to file created in 2 above. These are new details like the date processed, etc.
4. Write the Actual Message to the file created in 2 above.
For each file in location where Code = 'TEST'
If row exists in Source file copy the contents separated by $ onto the file created in 2 above, and include the new header and footer details.

Note that there will be several files in the source location but each must be copied to a new file created as in 2 above.

I just did a bit of the coding as follows but I'm stuck with this so please assist.
Actually the code belwo is working but when it's trying to write to the file created in 2 it's saying the file is in use by another process and can't be accessed.

VB
Sub Process()
'Obtain the file location via the DB
        Dim imf As String = das.CheckIMFLocation("TEST")
        Dim imfp As String = das.CheckIMFLocation("TESTP")
        Dim intInFiles As Integer = 0
        'Checks for all the file in the location where code is IMF 
        Dim filename As String = Nothing
        'Dim thewholefile As String
        Dim di As New DirectoryInfo(imf)
        Dim fiArr As FileInfo() = di.GetFiles()
        'Dim fri As FileInfo

        '1. Check for inward source files
        diInward = New DirectoryInfo(imf)
        fiInward = diInward.GetFiles("*.txt")
        intInFiles = fiInward.Length

        ''checks at least a file is in the folder and gets the name of the availabe file
        ''and assign that to the variable
        'For Each fri In fiArr
        '    filename = fri.Name
        'Next
        'thewholefile = imf & "\" & filename

        'Obtain the value of the fields from control table and builds the file name
        Dim FESFileNameInwardBase As String
        Dim FESFilenameInwardCurSeqNo As String
        Dim FESFileNameInwardSuffix As String
        Dim ProcessedFileName As String
        Dim createfile As String
        Dim fs As FileStream = Nothing
        Dim objStreamWriter As StreamWriter

        FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base
        FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No
        FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix

        'Check if file exists on the location for IMF
        If intInFiles > 0 Then
            'For each file in the location, create a processed file for them at the processed location
            'and read the contents and copy to the new file named.

            ' 1. Create a file for the file read
            For Each disfri In fiArr
                'MessageBox.Show(disfri.Name)
                '=======================gets new name for the file each time ======================
                FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base 'SWI
                FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No '100001
                FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix 'TEST
                ProcessedFileName = FESFileNameInwardBase & "_" & FESFilenameInwardCurSeqNo & "_" & FESFileNameInwardSuffix
                '=======================End getting file name =====================================

                'create the file here first before you write to it
                createfile = imfp & "\" & ProcessedFileName & ".txt"
                fs = File.Create(createfile)
                fs.Flush()
                fs.Close()

                'Read each file and get the details separated by the $ sign
                Using MyReader As New TextFieldParser(imf & "\" & disfri.Name)
                    'Using MyReader As New TextFieldParser(disfri.Name)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters("$")

                    Dim currentRow As String()
                    While Not MyReader.EndOfData
                        Try
                            currentRow = MyReader.ReadFields()
                            Dim currentField As String
                            'This for loop reads each data in the file
                            For Each currentField In currentRow
                                MessageBox.Show(currentField)

                                'Insert here the code to write to the new file, each line read
                                'and then exit loop and read the next file and do the same.
                                objStreamWriter = New StreamWriter(createfile)
                                objStreamWriter.WriteLine(currentField)
                            Next
                        Catch ex As Exception
                            MessageBox.Show("Line " & ex.Message & _
                                            "is not valid and will be skipped.")
                        End Try
                    End While
                End Using
                'End of reading each file separated by $ sign
            Next disfri
        Else
            'No files exists so exit sub
            MessageBox.Show("No Files exist", "No File", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        End If
End Sub
Posted
Updated 9-May-13 16:31pm
v2

1 solution

You are attempting to open the same file in a StreamWriter multiple times (once for each record). Move the opening of the StreamWriter to before the line

VB
For Each currentField In currentRow


As an aside, don't forget to close your StreamWriter at some point.
 
Share this answer
 
Comments
ekipongi 12-May-13 20:51pm    
Thanks _Damien S_. I have tried your suggestion but the issue is still there. What I'd want to acheieve is that for each file in the inward file location copy the details to a new text file created in another location with new header and footer details. If there are 10 text files in the inward location then file processed would be 10 in a processed location.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900