Results 1 to 5 of 5

Thread: Find Position in text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Find Position in text file

    I have done a lot of searching for a code sample of reading a file line by line and extracting text based on its position but haven't come up with anything usefull. I can read each line with this:
    Code:
    ' Need to find the 1st and 10th position in the sample
    ' text file.
        Private Sub ReadFile()
            Dim oread As StreamReader = _
                File.OpenText("C:\sample.txt")
            While oread.Peek <> -1
                Debug.Write(oread.ReadLine() & vbNewLine)
            End While
            oread.Close()
        End Sub
    I have a text file that has no visable delimiter like a comman etc. The coloulmns are determined by the position in the file. For instance:

    1 2 3 4 5 6 7 8 9
    ----------------
    M i k e S m i t h

    The first name starts at position 1 and the last name starts at position 5 (I don't think it will line up right in the mock up above but that is the idea). Does anybody have a sample peice of code they could point me to?

  2. #2
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Find Position in text file

    Quote Originally Posted by FastEddie
    Code:
    ' Need to find the 1st and 10th position in the sample
    ' text file.
    Are you saying you want to find the characters that are in the 1st & 10th position of each line? I'm confused as to exactly what you're looking for...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Find Position in text file

    Yes I am (looking to the 1st & 10th position of each line). There are columns in the file but it is based on position and not a delimiter.

    I eventually need to import the file into SQL server so I need to import it directly (programmatically) based on position or create a delimiter insert it and use DTS to do the import.

  4. #4
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Find Position in text file

    You can use the .Substring method to find the characters at any position within a string...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Find Position in text file

    nbrege thank you that worked great.
    Code:
        ' This did the trick
        Private Sub ReadFile()
    
            Dim oread As StreamReader = _
                File.OpenText("C:\member.txt")
    
            Dim sName As String = Nothing
            Dim sLName As String = Nothing
    
            While oread.Peek <> -1
                Dim myLine As String = (oread.ReadLine)
                sName = myLine.Substring(24, 34)
                sLName = myLine.Substring(58, 17)
                Debug.Write(sName & sLName & vbNewLine)
            End While
    
            oread.Close()
    
        End Sub

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