|
-
Apr 25th, 2008, 12:30 PM
#1
Thread Starter
Hyperactive Member
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?
-
Apr 25th, 2008, 12:58 PM
#2
Frenzied Member
Re: Find Position in text file
 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...
-
Apr 25th, 2008, 01:07 PM
#3
Thread Starter
Hyperactive Member
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.
-
Apr 25th, 2008, 01:13 PM
#4
Frenzied Member
Re: Find Position in text file
You can use the .Substring method to find the characters at any position within a string...
-
Apr 25th, 2008, 09:20 PM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|