Solved:Read words from text File
Hi all
I need to fetch numbers one by one from text file, suppose i have a text file with different serial numbers, like;
SHG9001231 SHG9001232
SHG9001233 SHG9001234 SHG9001235
SHG9001236
CNC343407
CNC343408
CNC343409
there is no sequence like tab delimited, comma delimited or CL/LF, so it need to search on every prefix series and fetch entire serial number. But i have no clue how to write code in vb.net 2005.
Re: Read words from text File
you could use regex:
vb Code:
Dim testStr As String = "SHG9001233 SHG9001234 SHG9001235"
Dim rx As New Regex("[A-Z]{3}\d+")
For Each m As Match In rx.Matches(testStr)
MsgBox(m.Value)
Next
don't forget to import regex:
vb Code:
Imports System.Text.RegularExpressions
Re: Read words from text File
Have a look into the System.io namespace, TextReader object and ReadLine method
Re: Read words from text File
There are many ways to read the file.
I don't have access to vb.net right now so please correct syntax errors if any...
The System.IO.StreamReader class supports several methods for reading text files. Here is one of them...
Code:
'~~> Amend to relevant path and file name
oRead = oFile.OpenText("C:\MyFile.txt")
While oRead.Peek <> -1
'~~> Read entire line
strEntireLine = oRead.ReadLine()
End While
oRead.Close()
And here is the code which you can integrate with the above code to get the serial numbers....
Code:
'~~> SPLIT Example
Module Module1
Sub Main()
'~~> We want to split this input string
Dim strSample As String = "SHG9001233 SHG9001234 SHG9001235"
'~~> Split string based on spaces
Dim words As String() = strSample.Split(New Char() {" "c})
Dim strword As String
For Each strword In words
Console.WriteLine (strword)
Next
End Sub
End Module
'~~>Output
'SHG9001233
'SHG9001234
'SHG9001235
Give it a try... If you get stuck simply post the code that you tried and we will definitely help you...
Re: Read words from text File
Thanks paul your trick is quite good and i hv gotten idea, but one thing more to do that i have to read entire serial number with its prefix, this chunk of code returns 7 character. can you plz guide me about "\d+" how it works, and if want to retrieve more characters after prefix what should i do?
Thanks in advance
Re: Read words from text File
\d+ = 1 or more numeric digits
so no change necessary
Re: Read words from text File
Thanks to all, actually i have text file in following format and i have to fetch only serial numbers, which starts from characters like, "SGH","CNC" and can be many others in different format, this is the one of many other text file formats.
Shipment: APKT30502582
Item Product No Description Serial No (Asset Tag, Ethernet Mac Address)
0100 FY686AV HP Compaq dx2810 Small Form Factor
SGH9270L10 (, 00237DC968FD)
SGH9270L11 (, 00237DC9683D)
SGH9270L12 (, 00237DC96B9E)
SGH9270L14 (, 00237DC96502)
SGH9270L15 (, 00237DC968D0)
SGH9270L16 (, 00237DC968D9)
SGH9270L17 (, 00237DC9662F)
SGH9270L19 (, 00237DC968EB)
SGH9270L1B (, 00237DC96414)
SGH9270L1C (, 00237DC96833)
SGH9270L1D (, 00237DC968C4)
SGH9270L1F (, 00237DC965B4)
SGH9270L1G (, 00237DC9648C)
SGH9270L1H (, 00237DC965A8)
SGH9270L1J (, 00237DC96AB3)
SGH9270L1K (, 00237DC96854)
SGH9270L1L (, 00237DC965D3)
SGH9270L1M (, 00237DC96617)
SGH9270L1N (, 00237DC96892)
SGH9270L1P (, 00237DC96B64)
SGH9270L1Q (, 00237DC968F1)
SGH9270L1R (, 00237DC96826)
SGH9270L1S (, 00237DC968E8)
SGH9270L1T (, 00237DC9691D)
SGH9270L1V (, 00237DC95E1B)
SGH9270L1W (, 00237DC96B96)
SGH9270L1X (, 00237DC96638)
SGH9270L1Y (, 00237DC9690D)
SGH9270L1Z (, 00237DC965A4)
SGH9270L20 (, 00237DC968E0)
SGH9270L21 (, 00237DC965B0)
SGH9270L22 (, 00237DC9661E)
SGH9270L23 (, 00237DC965DC)
SGH9270L24 (, 00237DC96BA9)
SGH9270L25 (, 00237DC96594)
SGH9270L26 (, 00237DC965C7)
SGH9270L27 (, 00237DC9680D)
SGH9270L28 (, 00237DC968BD)
SGH9270L29 (, 00237DC963A6)
SGH9270L2B (, 00237DC965AA)
SGH9270L2C (, 00237DC7E040)
SGH9270L2D (, 00237DC965E8)
SGH9270L2G (, 00237DC965C1)
SGH9270L2H (, 00237DC96B92)
SGH9270L2J (, 00237DC968CB)
SGH9270L2K (, 00237DC9682E)
SGH9270L2L (, 00237DC96585)
SGH9270L2M (, 00237DC965B8)
SGH9270L2N (, 00237DC96828)
SGH9270L2P (, 00237DC7E05D)
SGH9270L2Q (, 00237DC7E05E)
SGH9270L2S (, 00237DC7E085)
SGH9270L2T (, 00237DC7DFF4)
SGH9270L2V (, 00237DC965E0)
SGH9270L2W (, 00237DC8567A)
SGH9270L2X (, 00237DC965D8)
SGH9270L2Y (, 00237DC8553D)
SGH9270L2Z (, 00237DC857C2)
SGH9270L30 (, 00237DC85553)
SGH9270L31 (, 00237DC85544)
SGH9270L32 (, 00237DC8591C)
SGH9270L33 (, 00237DC857C6)
SGH9270L34 (, 00237DC85967)
SGH9270L35 (, 00237DC857D4)
SGH9270L36 (, 00237DC85858)
SGH9270L37 (, 00237DC857EE)
SGH9270L38 (, 00237DC853DD)
SGH9270L39 (, 00237DC7DFC1)
SGH9270L3B (, 00237DC85268)
SGH9270L3C (, 00237DC857FE)
SGH9270L3D (, 00237DC7DF9F)
SGH9270L3F (, 00237DC8553C)
SGH9270L3G (, 00237DC8547C)
SGH9270L3H (, 00237DC8584F)
SGH9270L3J (, 00237DC853B2)
SGH9270L3K (, 00237DC8595B)
SGH9270L3L (, 00237DC85286)
SGH9270L3M (, 00237DC85249)
SGH9270L3N (, 00237DC853E9)
SGH9270L3P (, 00237DC8552B)
SGH9270L3Q (, 00237DC85657)
SGH9270L3R (, 00237DC8584D)
SGH9270L3S (, 00237DC8524D)
SGH9270L3T (, 00237DC85199)
SGH9270L3V (, 00237DC85824)
SGH9270L3W (, 00237DC7E083)
SGH9270L3X (, 00237DC85501)
SGH9270L3Y (, 00237DC85270)
SGH9270L3Z (, 00237DC85801)
SGH9270L40 (, 00237DC85567)
SGH9270L41 (, 00237DC8581A)
SGH9270L42 (, 00237DC8537A)
SGH9270L43 (, 00237DC85551)
SGH9270L44 (, 00237DC857F1)
SGH9270L46 (, 00237DC85830)
Shipment: APKT30502582
Item Product No Description Serial No
0300 KB744AS HP Pavilion w1707 Monitor
CNC921NX9B CNC921NXBL CNC921NXN5 CNC921NXPD CNC921NXPP CNC921NXPS CNC921NXPV
CNC921NXPX CNC921NXQ2 CNC921NXQN CNC921NXQR CNC921NXQS CNC921NXRG CNC921NXRH CNC921NXSF
CNC921NXSN CNC921NXSQ CNC921NXSR CNC921NXSS CNC921NXST CNC921NXSZ CNC921NXT0 CNC921NXT1 CNC921NXTH
CNC921NXTY CNC921NXV0 CNC921NXV2 CNC921NXV4 CNC921NXV5 CNC921NXV6 CNC921NXVH CNC921NXVS CNC921NXVV
CNC921NXWF CNC921NXWY CNC921NXX7 CNC921NXX8 CNC921NXX9 CNC921NXXB CNC921NXXC CNC921NXXK CNC921NXXL
CNC921NXXW CNC921NXXY CNC921NXXZ CNC921NXY4 CNC921NXY6 CNC921NXY7 CNC921NXY8 CNC921NXYB CNC921NXYD
CNC921NXYF CNC921NXYK CNC921NXYV CNC921NXZ1 CNC921NXZ2 CNC921NXZ3 CNC921NXZ4 CNC921NXZ5
CNC921NXZ6 CNC921NXZ9 CNC921NXZB CNC921NXZD CNC921NXZF CNC921NXZM CNC921NXZR CNC921NY0G
CNC921NY0L CNC921NY0N CNC921NY0Q CNC921NY0R CNC921NY0W CNC921NY0X CNC921NY0Y CNC921NY11 CNC921NY12
CNC921NY14 CNC921NY15 CNC921NY17 CNC921NY18 CNC921NY19 CNC921NY1B CNC921NY1D CNC921NY1F CNC921NY1G
CNC921NY1J CNC921NY1S CNC921NY1W CNC921NY1X CNC921NY1Y CNC921NY1Z CNC921NY22 CNC921QD3G CNC921QD3H
CNC921QD3J
Re: Read words from text File
Dim rx As New Regex("[A-Z]{3}\d+")
=
find 3 A-Z characters followed by 1 or more numeric digits
Re: Read words from text File
now i've seen the actual file format, it might be better to split the serial numbers on the " " as sid suggested
Re: Read words from text File
Paul as u suggested sid's solution, but one thing confusing me that in this case all other irrelevant text words will be fetched those have " " in prefix. I have an idea, if i ask from user enter first 3 or 4 characters as serial numbers' prefix and the length of the serial numbers. Logic may be something like that computer find all numbers those start with given prefix and read until given length. Because one text file may contains different serial number's prefix and length.
Re: Read words from text File
Add a listbox and a button to a form and paste this code...
All the serials will be loaded to the listbox when you click the button... Please amend it as per your requirements for example you will have to change the text filename and path...
Hope this helps....
Code:
Imports System
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
'~~> Open the relevant file
Dim path As String = "E:\Serial.txt"
Dim sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
SplitText(sr.ReadLine())
Loop
sr.Close()
End Sub
Private Sub SplitText(ByVal strSample As String)
'~~> Split string based on spaces
Dim words As String() = strSample.Split(New Char() {" "c})
Dim strword As String
For Each strword In words
Dim subString As String = Microsoft.VisualBasic.Left(strword, 3)
'~~> If it meets your criteria then add it to a listbox
If subString = "SGH" Or subString = "CNC" Then ListBox1.Items.Add(strword)
Next
End Sub
End Class
Solved: Read words from text File
Thanks Sid, it works fine.