Hellow,
I'm an intermediate VB.Net programmer and I need assistant to get a project done. I have a request as follows.
- 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. - 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