Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
 
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 9-May-13 13:56pm
Edited 9-May-13 17:31pm
_Damian S_79.6K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
For Each currentField In currentRow
 
As an aside, don't forget to close your StreamWriter at some point.
  Permalink  
Comments
ekipongi at 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)

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 219
4 Mathew Soji 160
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 9 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100