1

I have a text file delimited by pipes. I want to read the value at the fifth pipe but I cannot figure out how to do that. All I can do is read each section of the array. Can't find examples on this.

    EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENEDCOMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.||150||                                                                                                                              
    EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150||
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Noob2Java
  • 213
  • 1
  • 7
  • 18
  • What do you mean? Splitting each line into an array and reading the fifth value is exactly what you need to meet your goal...so what's wrong with that method, or what about that method isn't working for you? – soohoonigan Sep 30 '16 at 14:18
  • 2
    Possible duplicate of [CSV File Imports in .Net](http://stackoverflow.com/questions/1898/csv-file-imports-in-net) – GSerg Sep 30 '16 at 17:43
  • Here is a couple lines of the file that will most likely be used. In this one the part number is before the 2nd pipe. The value is before the 10th pipe. EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.||150|| EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150|| – Noob2Java Sep 30 '16 at 17:44
  • Well that's the first problem right off the bat, the code is searching a particular segment for the part number, mine was searching the third. It's not going to find anything if the part number is in fact in the second segment. I'm going to replace my answer with a working example that I've just tested – soohoonigan Sep 30 '16 at 17:53

1 Answers1

0

This example is using a text file with these two lines in it:

Line1: EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.|‌​|150||

Line2: EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150‌​||

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fp as string = "" 'enter the full path to your file here
    Dim value as string = GetValueForPart(fp, Me.TextBox1.Text)
    MsgBox(value) 'in this example, value is set to "6.237" when textbox input is "TR2999-01G"
End Sub

Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String
    If Not File.Exists(filepath) Then Return Nothing
    If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
    Dim ret As String = Nothing
    Using sr As New StreamReader(filepath)
        Do While sr.Peek >= 0
            Dim line() As String = sr.ReadLine.Split(CChar("|"))
            If line IsNot Nothing AndAlso line.Count >= 5 Then
                If line(1).Equals(SearchPartNum) Then
                    ret = line(9)
                    Exit Do
                End If
            End If
        Loop
    End Using
    Return ret
End Function

I just tested this, all you need to do is enter your full filepath on the second line

Community
  • 1
  • 1
soohoonigan
  • 2,342
  • 2
  • 10
  • 18
  • This is what I need, thanks. Now I am working on trying to search for a part number in the same file that resides before the 3rd pipe and then read the 5th value in that line. – Noob2Java Sep 30 '16 at 14:52
  • This is getting me on the right track... I appreciate the help. How do I call this function from a button? – Noob2Java Sep 30 '16 at 15:17
  • Just dim a variable to hold its return and call the function, if the part number is found in the textfile then it will assign the value to your variable. All you need to do is pass the function the filepath so it knows where to look, and the part number that it's looking for as shown above – soohoonigan Sep 30 '16 at 15:21
  • I'm putting 'value' as the part number and get an error on line 'If line(2).equals'. I can't seem to post my code correctly on this site. – Noob2Java Sep 30 '16 at 15:52
  • well, there's nothing syntactically wrong with that code, I've tested it and it runs fine...so without any further information on what your exception is, I can't really do much more. I would suggest you set a breakpoint at the begining of the function and step through it line by line, hover over the variables at each step and check their values, and when it errors, post the exception – soohoonigan Sep 30 '16 at 16:48
  • This is what i'm using right now. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click value = GetValueForPart("C:\Scripts\test.txt", Me.TextBox1.Text) End Sub Public Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String Dim ret As String = Nothing Using sr As New StreamReader("C:\Scripts\Test.txt") Do While sr.Peek >= 0 Dim line() As String = sr.ReadLine.Split(CChar("|")) If line(2).Equals(value) Then ret = line(4) Exit Do End If Loop End Using Return ret End Function' – Noob2Java Sep 30 '16 at 17:21
  • what exception is being thrown? – soohoonigan Sep 30 '16 at 17:23
  • the only exception I can think of that would get raised there is arrayoutofbounds or null reference. in either case, that means that there aren't enough fields on the line because the line isn't formatted like the function is expecting it to be. I'll add a check to the function to make sure there're enough fields to test for that. if that doesn't work then it must be that your textfile is actually formatted differently then the sample I showed highlighted in yellow above – soohoonigan Sep 30 '16 at 17:33
  • I don't think I am writing in the variables correctly. So in my file I have part number TR999-08 (before 3rd pipe) that I want to search for which I enter in TextBox1. Press the button and have it show me the value before the 5th pipe. – Noob2Java Sep 30 '16 at 17:34
  • edit your question and add a sample of a couple of lines from your text file, with one of the lines including the part number you're searching for, and I'll see if I can find out what problem you're having – soohoonigan Sep 30 '16 at 17:38
  • I put two lines of the text that will be used – Noob2Java Sep 30 '16 at 18:01
  • This is working GREAT! Thanks a lot for your time and help on this, I really appreciate it. – Noob2Java Sep 30 '16 at 19:33
  • I am back to this with a small error I came up with during testing. I found that in this text file, I can have two or maybe even three of the same part number on more than one line. If I have two TR299-08 lines, how can I find them both? The value I am reading before the fifth pipe is a quantity so ultimately I would like to add them together when it finds more than one line. – Noob2Java Oct 13 '16 at 16:28