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.
  2. 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
 '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
 '=======================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)

 '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

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

 '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)
 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
 'No files exists so exit sub
 MessageBox.Show("No Files exist", "No File", MessageBoxButtons.OK, MessageBoxIcon.Stop)
 Exit Sub
 End If
 End Sub 

The reson it's saying it's being used by another process is because it is still being accessed by your program when your done writing to or reading from the file you will need to close and flush the stream.

I have never tried this but you could try placing the streamreader or streamwriter at the top of the page so that way you can keep reading and writing to it without having to start a new streamreader/streamwriter in seperate functions/events and all you will need to so is just save the file then when you need to access it again just call the stream reader.

I hope that made sense.

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.