-
May 12th, 2015, 11:10 AM
#1
Thread Starter
Hyperactive Member
Read a text file and grab OrderID
There a lot of OrderID within a big text file.
The OrderID format is like below OrderID is 16 characters after ADR*XYZ*)
ADR*XYZ*2015123456BH0001
ADR*XYZ*2015113955AB0002
How to code to grab them or insert into a table?
-
May 12th, 2015, 11:18 AM
#2
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
-
May 12th, 2015, 11:41 AM
#3
Thread Starter
Hyperactive Member
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?
-
May 12th, 2015, 11:43 AM
#4
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
-
May 12th, 2015, 12:04 PM
#5
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. ![Check](images/icons/completeclear.gif)
-
May 12th, 2015, 12:49 PM
#6
Thread Starter
Hyperactive Member
Re: Read a text file and grab OrderID
![Quote](/images/misc/quote_icon.png) Originally Posted by Toph
If the OrderID is all jaggered, you could also use Regex to parse them ![Smilie](images/smilies/smile.gif)
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*
-
May 12th, 2015, 12:58 PM
#7
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. ![Check](images/icons/completeclear.gif)
-
May 12th, 2015, 01:15 PM
#8
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 -
-
May 12th, 2015, 01:28 PM
#9
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
-
May 12th, 2015, 01:37 PM
#10
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.
![](http://i.imgur.com/3Ar7TlG.png)
Let me know if the pattern I used is not correct.
If you find my contributions helpful then rate them. ![Check](images/icons/completeclear.gif)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|