?
Solved

Grabbing Lines from Text Files that Change Format

Posted on 2006-11-09
9
Medium Priority
?
292 Views
Last Modified: 2010-04-23
With the help of an excellent individual I was able to extract sections of a text file based on my input box and it reads all the lines downward. The difference was each line item started with a " - " and the header did not.
Now in a real scenario, I have a text file that comes with different customer information and orders in to. I need to grab all line in between two words. For instance, here's the actual file. Sorry it's kinda long, but I couldnt explain this good.

11/07/06          TOMS PO Report (81510 v430)                     Pg: 1  
PO No.: 3095525861
============================================================
Original, Stand Alone Order            PO No.: 3095525861
PO Date: 10/29/2006
_____________________________________________________________________________
Internal Vendor Number: 738402911
_____________________________________________________________________________
Allowance, H560 - 1%, based on Base Price Amount
Off Invoice
_____________________________________________________________________________
Terms Type: Basic Discount Offered     Terms Basis: Invoice Date
Terms Discount: 1%                     Discount Days Due: 10  
Terms Net Days: 30  
_____________________________________________________________________________
     Requested Ship Date: 11/06/2006
_____________________________________________________________________________
                                         Transportation Method/Type: Backhaul
_____________________________________________________________________________
Letters or Notes: SPECIAL INSTRUCTIONS
-----------------------------------------------------------------------------
APPOINTMENTS REQUIRED 72HRS IN ADVANCE. CARRIER TO UNLOAD,
SORT, AND SEGREGATE ON DOCK.
_____________________________________________________________________________
     Supplier: DFRRT
_____________________________________________________________________________
 Buying Party: LOCAL STORE
        GLP #: 0f074343314
_____________________________________________________________________________

              =============== LINE ITEM DATA ===============          
=============================================================
                   Unit      Buyer's
Item     Quantity  Price     Item #         UA
------   --------  --------  -------------  -------------
00001    40 CA     $3.51     00943468      007440125
-----------------------------------------------------------------------------
Pack: 60
_____________________________________________________________________________
 

11/07/06          TOMS PO Report (8150 v430)                     Pg: 2  
PO No.: 4095525861
============================================================
This above starts the next customer......

Heres my current code.

  Dim sr As New IO.StreamReader("c:\test3.txt")
        Dim linein As String
        linein = sr.ReadLine
        Do Until linein Is Nothing
            If linein.StartsWith("PO No.:") Then
                TextBox1.Text = (linein) & vbCrLf
                While sr.Peek <> -1
                    linein = sr.ReadLine()
                    If linein.Length > 0 AndAlso linein.Substring(0, 2) <> " " Then
                        TextBox1.AppendText(linein & vbCrLf)
                    Else
                        Exit While
                    End If
                End While
                Exit Do
            End If
            linein = sr.ReadLine()
        Loop
        sr.Close()
0
Comment
Question by:DFCRJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17911002
Hi  DFCRJ;

You state, "I need to grab all line in between two words. For instance, here's the actual file."

Are the two words always the same two words?
If the two words are different all the time how do we know that the words are?
What do you want to do with the line you pull out?
What version of Visual Studio .Net, 2003 or 2005?

Fernando
0
 

Author Comment

by:DFCRJ
ID: 17913278
I'm sorry, all that information and I failed to give specifics. VS2005 is what I'm on.
The two words that are always the same (or at least in the two weeks worth I checked) is
PO No.:

That would start the header, everything in between them I need to go to that one customer.
PO No.:
xxx
xx
xxx

xxxxxx

PO No.:

It does have a date field that starts it actually, but of course it changes on every file we receive and is determine when the file was received on our end.  The
PO NO.: was consistent.

thx
0
 
LVL 5

Accepted Solution

by:
xersoft earned 2000 total points
ID: 17914380
I'd use the date to determine the orders. Try this code. It's .net 2.0. Let me know if it's not quite right, I can take another stab at it.



    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Dim file As New IO.FileInfo(Me.txtFile.Text)
        If Not file.Exists Then Throw New IO.FileNotFoundException(file.FullName)


        Dim reader As New IO.StreamReader(file.OpenRead)
        Dim orders As New List(Of String)

        Try
            Dim CurrentOrder As String = ""
            While Not reader.EndOfStream
                'read a line and determine if it's a new order
                Dim line As String = reader.ReadLine


                If IsLineStartOfOrder(line) Then
                    'if this is the start of an order then note the last order
                    If Not String.IsNullOrEmpty(CurrentOrder) Then
                        orders.Add(CurrentOrder)
                        CurrentOrder = String.Empty
                    End If

                    'start remembering this new order
                    CurrentOrder = line & vbCrLf
                Else
                    'this is not the start of a new order
                    'add the line to the current order we are working on
                    CurrentOrder &= line & vbCrLf
                End If
            End While

            'clean up
            reader.Close() : reader.Dispose() : reader = Nothing
        Catch ex As Exception
            Throw
        Finally
            'ensure clean up
            If Not reader Is Nothing Then
                reader.Close() : reader.Dispose() : reader = Nothing
            End If
        End Try


        MessageBox.Show("Orders in file: " & orders.Count, "Done", MessageBoxButtons.OK)
    End Sub

    Private Function IsLineStartOfOrder(ByVal Line As String) As Boolean
        'valadate input
        If String.IsNullOrEmpty(Line) Then Return False


        'first character a number?
        If IsNumeric(Line.Substring(0, 1)) Then
            'split on space
            Dim Parts() As String = Line.Split(" "c)
            If Parts.Length > 0 Then
                'see if the first part is a date
                'the only time the first part will be a date is if
                'it's the start of a new order
                If IsDate(Parts(0)) Then
                    Return True
                End If
            End If
        End If

        'not the start of an order
        Return False
    End Function
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:xersoft
ID: 17914396
oops

My above code can be tested by placing a button and a textbox on a new form. Name the button btnGo and the text box txtFile. Set the text property of the textbox to the name of the file with orders for example "c:\test.txt". Then drop the above code into the form.

sorry for not being more clear about that in my last post. I should have written a function that takes a file name and returns a list of orders. That would be more useful.
0
 

Author Comment

by:DFCRJ
ID: 17914614
I've done it and I received the message box with 48 orders in the file. A couple of things I'm wondering about, is that counting the number of times the date is found? The PO Date is in the file 14 times, where does the order get displayed to?
Just curious.
0
 
LVL 5

Expert Comment

by:xersoft
ID: 17915009
Your finding 48 orders in a file that contains only 14? That would indicate to me that my test to see if we are on a new order is not correct. I relize you probably can't post the actual data, but that would be most helpful.

To view the orders try this after the   MessageBox.Show("Orders in file: " & orders.Count, "Done", MessageBoxButtons.OK) line:

for each str as string in orders
      MessageBox.Show(str)
next


That will allow you to see each order. The orders are stored in the list called orders and can be accessed as if they were in an array.

Order 1:
MessageBox.Show(orders.item(0))

Order 2:
MessageBox.Show(orders.item(1))

and so on.


I would be interested to see what orders this procedure is finding that are not actual orders, assuming there are only 14 orders in the file but the code is finding 48.
0
 

Author Comment

by:DFCRJ
ID: 17915140
I owe you an apology. I have three test files here i was working with and I was looking one I cut and pasted.
The original does contain 48 and it's working perfect!!
I'm so sorry for given you wrong information.
Thank you so much for helping me.. RJ
0
 
LVL 5

Expert Comment

by:xersoft
ID: 17915210
One thing I'd like to note: The notes section could cause problems.

Letters or Notes: SPECIAL INSTRUCTIONS
-----------------------------------------------------------------------------
APPOINTMENTS REQUIRED 72HRS IN ADVANCE. CARRIER TO UNLOAD,
SORT, AND SEGREGATE ON DOCK. PLEASE DO NOT DELIVER AFTER
11/03/06 OR THE PRODUCT WILL NOT BE USED.


In a note like that you will get invalid results. This may or may not be a problem for you. If it is a problem I can work out another solution.
0
 

Author Comment

by:DFCRJ
ID: 17915324
I think it will be fine, I'll run it a few days and see what happens.
Thanks for so much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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