Solved

Grabbing Lines from Text Files that Change Format

Posted on 2006-11-09
9
280 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

11 Experts available now in Live!

Get 1:1 Help Now