Solved

Splitting a master report file into individual reports

Posted on 2012-03-21
6
178 Views
Last Modified: 2012-03-22
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(strReportLine)
         Case "1"
                 swOutputFile.writeLine(strReportLine)

    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.
0
Comment
Question by:gwosgood
  • 3
  • 2
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37752650
1)  To close a streamwriter object you can use the member function Close
2)  Scope errors - declare the StreamWriter object outside of your loop.

pseudo code

StreamWriter sw = null
StreamReader sr = open your file for reading here
string sLine

start loop
read a line from sr into sLine

  case statement  --  check first char
     1 then close the currently open sw (IF IT IS NOT NULL)
        open the sw with the required name, write line to it

      2  Write line to sw
  end case
end loop
close sw (possibly should check it is non null - empty input file)
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 37752791
I think the case above should be for
    0 - close current output file and create new one
    1 - write line to report file

mlmcc
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37752894
ooops - well spotted.  :-)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Author Comment

by:gwosgood
ID: 37753367
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(fileNameOutput)
                    sw.WriteLine(strReportLine)
                Case "1"
                    sw.WriteLine(strReportLine)
            End Select

        Loop Until inputRTF.EndOfStream

        inputRTF.Close()
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37753803
I'm not certain for VB but does this work?
if sw = nothing then
0
 
LVL 2

Author Comment

by:gwosgood
ID: 37754215
The correct syntax to check for Null is

        If sw.Equals(StreamWriter.Null) = False Then sw.Close()
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now