Results 1 to 10 of 10

Thread: Read a text file and grab OrderID

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Read a text file and grab OrderID

    There a lot of OrderID within a big text file.
    The OrderID format is like belowOrderID is 16 characters after ADR*XYZ*)

    ADR*XYZ*2015123456BH0001
    ADR*XYZ*2015113955AB0002

    How to code to grab them or insert into a table?

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Read a text file and grab OrderID

    As long as the format is always the same, you could read the file lines, then split each line and take the last element returned by the split.

    Code:
    Dim lines() as string = IO.File.ReadAllLines(fPAth)
    for each line as string in Lines()
         dim s() as string = line.Split("*"c)
         dim orderID as string = s(2)
         'do something with the oderID
    Next
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Read a text file and grab OrderID

    Thank you for help.
    The problem is that there are a few characters like "~", "-" after OrderID.
    How to code to read only 16 characters after "ADR*XYZ*" and insert them into array one by one?

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Read a text file and grab OrderID

    Take a look at the SubString method of a string

    Code:
    orderID = Line.SubString(8,16)
    I think that would be it, but you may need to fudgle with it a bit to get what you need.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Read a text file and grab OrderID

    If the OrderID is all jaggered, you could also use Regex to parse them
    If you find my contributions helpful then rate them.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Read a text file and grab OrderID

    Quote Originally Posted by Toph View Post
    If the OrderID is all jaggered, you could also use Regex to parse them
    File is something like below. OrderID are located anywhere in the file.

    O****MI*16659434~TRN*2*A257330~STC*~ADR*XYZ*2015123456BH0001~REF*BLT*831~DTP*472*RD8*20141216-20141216~HL*8*3*PT~NM1*QC*1*CRUZ*ILDEMARO****MI*ADR32624R01~TRN*2*A23~ADR*XYZ*2015113955AB0002~*A2:2 0*20150506*WQ*

  7. #7
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Read a text file and grab OrderID

    Okay, carefully describe the format of all OrderIDs. I'll hook you up with code to extract it .
    If you find my contributions helpful then rate them.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Read a text file and grab OrderID

    You should use regular expression as Toph suggested. Using combination of string manipulation methods will work, but it'll take you a lot more efforts and is prone to error.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Read a text file and grab OrderID

    Is that from a X12 277 transaction? X12 transactions have a hierarchy that you'll want to persist as well as dynamic delimiters that you need to account for.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  10. #10
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Read a text file and grab OrderID

    Here's some code I whipped up that works depending on the pattern I assumed the OrderID would be in.

    Code:
    Imports System.Text.RegularExpressions
    
    Module Module1
    
        Sub Main()
            Console.Title = "Hi"
    
            Dim txtData As String = IO.File.ReadAllText("C:\Users\MyUser\Desktop\items.txt")
            Dim orderIdList() As String = GetOrderIDs(txtData)
    
            For Each orderId In orderIdList
                Console.WriteLine("I found the OrderID: " & orderId)
                'You can then insert into table E.g. Insert Into OrderTable(OrderID) Values(order)
            Next
    
            Console.ReadLine()
        End Sub
    
        Private Function GetOrderIDs(txtData As String) As String()
            Dim listOfMatches As MatchCollection = Regex.Matches(txtData, "ADR\*XYZ\*\d{10}\w{2}\d{4}")
            Dim orderIdList As New List(Of String)
    
            For Each match As Match In listOfMatches
                orderIdList.Add(match.Value)
            Next
    
            Return orderIdList.ToArray()
        End Function
    
    End Module
    Proof it works.



    Let me know if the pattern I used is not correct.
    If you find my contributions helpful then rate them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width