Importing text file delimited by separator in VB
Here are 2 examples on how to get a text value with separators. The first example show you how to split a text line based on a character: space, tab, coma and so on. The second sample use delimiters with more that one char.
These samples are perfect if you want to make a text files with keys and values. I offend use this to make options files, parameters files, customizations files, and any types of custom files.
You could improve the sample if you have multiples arguments or columns. You could add some encoding detection and delete all the MsgBox inside the sample.
I hope you find this sample useful. If you want to download the project sample, you could click on the link: SampleTextwithSeperator
''' <summary> ''' This function contains 3 examples ''' ''' this example is useful if you want to get a value base from a name ''' like a parameters file. ''' ''' feel free to share this example. ''' </summary> ''' <remarks></remarks> Private Sub test() Dim aString As String = "" Dim aSeperator As String = "" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Exemple : read all the file , line by line, until End Of Stream MsgBox("You need to select a text file, for each lines, it will display the line") Using oOpenFileDialog As New OpenFileDialog() oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream" oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" oOpenFileDialog.FilterIndex = 1 oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) oOpenFileDialog.Multiselect = False If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then If IO.File.Exists(oOpenFileDialog.FileName) Then Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName) Do 'read all the file until End Of Stream aString = oStreamReader.ReadLine MsgBox(aString) Loop Until oStreamReader.EndOfStream = True End Using End If End If End Using '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Exemple : read all the file , line by line, until End Of Stream 'but if seperated by a SPACE, then display the two values separately 'it could be any other char MsgBox("You need to select a text file, for each lines, it will display each value seperated by a SPACE") Using oOpenFileDialog As New OpenFileDialog() oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream, ***SEPERATED BY A CHAR***" oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" oOpenFileDialog.FilterIndex = 1 oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) oOpenFileDialog.Multiselect = False If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then If IO.File.Exists(oOpenFileDialog.FileName) Then Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName) Do 'read all the file until End Of Stream aString = oStreamReader.ReadLine MsgBox(aString.Split(" ")(0)) MsgBox(aString.Split(" ")(1)) Loop Until oStreamReader.EndOfStream = True End Using End If End If End Using '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Exemple : read all the file , line by line, until End Of Stream 'but if seperated by a STRING = "<>", then display the two values separately 'the string could be any length. MsgBox("You need to select a text file, for each lines, it will display each value seperated by any String (2 char and more), for exemple : '==' ") Using oOpenFileDialog As New OpenFileDialog() oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream" oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" oOpenFileDialog.FilterIndex = 1 oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) oOpenFileDialog.Multiselect = False If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then If IO.File.Exists(oOpenFileDialog.FileName) Then Do aSeperator = InputBox("What will be the seperator to detect inside each lines?", "please enter a sperator") Loop Until aSeperator IsNot Nothing And aSeperator.Length > 0 Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName) Do 'read all the file until End Of Stream aString = oStreamReader.ReadLine If aString.IndexOf(aSeperator) > 0 Then 'important to check if the seperator exist inside before using the SPLIT method MsgBox(aString.Split(New String() {aSeperator}, System.StringSplitOptions.None)(0)) MsgBox(aString.Split(New String() {aSeperator}, System.StringSplitOptions.None)(1)) End If Loop Until oStreamReader.EndOfStream = True End Using End If End If End Using End Sub |
This email is free from viruses and malware because avast! Antivirus protection is active. |
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :