Solved

Grabbing Lines from Text Files that Change Format

Posted on 2006-11-09
9
281 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
  • 4
  • 4
9 Comments
 
LVL 62

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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

15 Experts available now in Live!

Get 1:1 Help Now