gwosgood
asked on
Splitting a master report file into individual reports
Greetings experts,
I am looking for an efficient way to input a master file and parse the contents into individual reports based on dynamic headings. A master file could include anywhere from 1 to 20 individual reports, and in any combination. Each line of text within the master file is prefixed with an identifier code, '0' representing report titles, and '1' representing report detail.
The master report is layed out as such:
0Report 1
1Report 1 detail line
1Report 1 detail line
1Report 1 detail line
0Report 2
1Report 2 detail line
1Report 2 detail line
0Report 3
1Report 3 detail line
..ect
I would like to write a parse function that transforms the 1 master file into individual files. The report titles are dynamic to the master file. Below is some sample psuedocode outlining my desired approach:
Private Sub SplitReport(byVal reportname as String)
dim srInput as new StreamReader(reportname)
dim chrPrefix as Char = ""
dim strReportLine as String = ""
dim strReportName as String = ""
dim tempRead as string = ""
do
tempInput = srInput.ReadLine
chrPrefix = tempInput.substring(0,1) ' first character
strReportLine = tempInput.substring(1) ' rest of line
switch (chrPrefix)
case "0"
' create new StreamWriter object here using Report
strReportName = strReportLine.substring(0, 8) & ".txt" ' first 8 characters is report name
dim swOutputFile as New StreamWriter("C:\temp\" strReportName)
swOutputFile.writeLine(str ReportLine )
Case "1"
swOutputFile.writeLine(str ReportLine )
End Select
loop until srInput.EndOfStream
srInput.Close
End Sub
My problem is that 1) I dont know how to close the file at the end of Report1 and the beginning of Report2, and 2) I get errors with variable scope since I defined the StreamWriter within a case. Any suggestions would be very helpful at this point.
Thank you for your advice and suggestions.
I am looking for an efficient way to input a master file and parse the contents into individual reports based on dynamic headings. A master file could include anywhere from 1 to 20 individual reports, and in any combination. Each line of text within the master file is prefixed with an identifier code, '0' representing report titles, and '1' representing report detail.
The master report is layed out as such:
0Report 1
1Report 1 detail line
1Report 1 detail line
1Report 1 detail line
0Report 2
1Report 2 detail line
1Report 2 detail line
0Report 3
1Report 3 detail line
..ect
I would like to write a parse function that transforms the 1 master file into individual files. The report titles are dynamic to the master file. Below is some sample psuedocode outlining my desired approach:
Private Sub SplitReport(byVal reportname as String)
dim srInput as new StreamReader(reportname)
dim chrPrefix as Char = ""
dim strReportLine as String = ""
dim strReportName as String = ""
dim tempRead as string = ""
do
tempInput = srInput.ReadLine
chrPrefix = tempInput.substring(0,1) ' first character
strReportLine = tempInput.substring(1) ' rest of line
switch (chrPrefix)
case "0"
' create new StreamWriter object here using Report
strReportName = strReportLine.substring(0,
dim swOutputFile as New StreamWriter("C:\temp\" strReportName)
swOutputFile.writeLine(str
Case "1"
swOutputFile.writeLine(str
End Select
loop until srInput.EndOfStream
srInput.Close
End Sub
My problem is that 1) I dont know how to close the file at the end of Report1 and the beginning of Report2, and 2) I get errors with variable scope since I defined the StreamWriter within a case. Any suggestions would be very helpful at this point.
Thank you for your advice and suggestions.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ooops - well spotted. :-)
ASKER
what is the proper syntax for testing if sw is null?
here is my code snipet so far:
Dim inputRTF As New StreamReader(strFilename)
Dim sw As StreamWriter
Dim fileNameOutput As String = "C:\temp\"
Dim chrPrefix As Char = ""
Dim strReportLine As String = ""
Dim inputText As String = ""
Do
inputText = inputRTF.ReadLine
chrPrefix = inputText.Substring(0, 1)
strReportLine = inputText.Substring(1)
Select Case (chrPrefix)
Case "0"
fileNameOutput = fileNameOutput & inputText.Substring(1, 8) & ".TXT"
If sw.Null = False Then sw.Close()
sw = New StreamWriter(fileNameOutpu t)
sw.WriteLine(strReportLine )
Case "1"
sw.WriteLine(strReportLine )
End Select
Loop Until inputRTF.EndOfStream
inputRTF.Close()
here is my code snipet so far:
Dim inputRTF As New StreamReader(strFilename)
Dim sw As StreamWriter
Dim fileNameOutput As String = "C:\temp\"
Dim chrPrefix As Char = ""
Dim strReportLine As String = ""
Dim inputText As String = ""
Do
inputText = inputRTF.ReadLine
chrPrefix = inputText.Substring(0, 1)
strReportLine = inputText.Substring(1)
Select Case (chrPrefix)
Case "0"
fileNameOutput = fileNameOutput & inputText.Substring(1, 8) & ".TXT"
If sw.Null = False Then sw.Close()
sw = New StreamWriter(fileNameOutpu
sw.WriteLine(strReportLine
Case "1"
sw.WriteLine(strReportLine
End Select
Loop Until inputRTF.EndOfStream
inputRTF.Close()
I'm not certain for VB but does this work?
if sw = nothing then
if sw = nothing then
ASKER
The correct syntax to check for Null is
If sw.Equals(StreamWriter.Nul l) = False Then sw.Close()
If sw.Equals(StreamWriter.Nul
0 - close current output file and create new one
1 - write line to report file
mlmcc