-
Dec 4th, 2021, 05:52 PM
#1
Thread Starter
Lively Member
VB .NET Split to catch exact value from txt file
This is my first post and I want to say hello to everyone.
I have one small project VB Visual Studio 2019 and the thing I've stuck on is this:
So my Win form app is looking inside the particular folder for any new files. When a new file is created, FileSystemWatcher raises an event in the background. The user isn't aware of that and that is how the app should work. At that particular time, the app needs to read that file and take string values from a text file.
I've made it (for the test purposes) to read all values (with comma delimiter defined ","), but I can't figure out how to take that one single string value at position 4 (after the fourth comma in the first line).
This is my code example, and MsgBox(currentfield) should give me that string to complete this task.
Code:
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(
"C:\test\" & filename)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentfield As String
For Each currentfield In currentRow
MsgBox(currentfield)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
And for example, this is the file the app reads. I need string value from that file = 65240
PHP Code:
1,017410,1,Ok;0069,65240,0092;
305,1,017410,2,Ok;1;1.000;0.00;340.00;4;P0Some answer;
305,1,017410,3,Ok;2;1.000;0.00;5.85;2;P17Some Answer;
51,1,017410,4,Ok;
53,1,017410,5,Ok;3;345.85;
53,1,017410,6,Ok;;;
55,1,017410,7,Ok;4200118580005;Company;Address;City;;;
56,1,017410,8,Ok;070,65240,0091;
-
Dec 4th, 2021, 06:23 PM
#2
Re: VB .NET Split to catch exact value from txt file
I haven't used the textFieldParser approach. I'd be inclined to just read in the string, Split on ","c. This would create an array of strings, and you'd want the fourth element of the array.
However, using the approach you have, do you see the value you want in the Messagebox? If so, then you've pretty nearly got it. Instead of a For Each loop, if you used a For Next loop, then the fourth element would be the one you wanted. If that is the case, then I would assume that the parser would offer up a method to get element N, so you could skip the loop entirely and just use that.
My usual boring signature: Nothing
-
Dec 4th, 2021, 08:43 PM
#3
Re: VB .NET Split to catch exact value from txt file
RedaFields will return a String array, just as String.Split would. Your question really has nothing to do with reading the file, which you appear to already be doing successfully. Your issue is how to work with arrays. As with any array, you get or set an element by specifying the zero-based index of that element. In this case:
vb.net Code:
Dim fifthElement = currentRow(4)
Get rid of this loop:
Code:
For Each currentfield In currentRow
MsgBox(currentfield)
Next
and replace it with this:
vb.net Code:
MessageBox.Show(currentRow(4))
and you should see that values you're after.
-
Dec 4th, 2021, 08:51 PM
#4
Re: VB .NET Split to catch exact value from txt file
I just noticed that you said this:
I can't figure out how to take that one single string value at position 4 (after the fourth comma in the first line).
If you're only after a single value, rather than values from every line, then I might be tempted to discard the TextFieldParser. The advantage that it offers is that it will handle field delimiters (in your case, commas) and record delimiters (line breaks) within quoted field values. If you don't need that then you can easily get away without using a TextFieldParser. If you only need one line, I'd suggest this:
vb.net Code:
Dim filePath = IO.Path.Combine("C:\test", filename)
Dim firstLine = IO.File.ReadLines(filePath).First()
Note the use of ReadLines and NOT ReadAllLines. The latter will still work but will read every line in the file. The former will only read the first line when you call First on the result.
Now that you have a String containing the first line, you can split it as Shaggy suggested:
vb.net Code:
Dim fields = firstLine.Split(","c)
That will give you a String array and you can index it as I described earlier.
-
Dec 5th, 2021, 04:39 PM
#5
Thread Starter
Lively Member
Re: VB .NET Split to catch exact value from txt file
Thank you both for your help. This is code that does what I need to.
Code:
Dim fname As String = e.FullPath
Dim filename As String = Path.GetFileName(fname)
Dim destinationPath As String = Path.Combine("C:\test2\", filename)
Dim filetoreadfrom As String = Path.Combine("C:\test\", filename)
Dim firstLine = IO.File.ReadLines(filetoreadfrom).First()
Dim fields = firstLine.Split(","c)
Dim fifthElement = fields(4)
If System.IO.File.Exists(fname) = True Then
Try
MessageBox.Show(fifthElement)
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
File.Copy(fname, destinationPath, True)
File.Delete(fname)
Else
MsgBox("File Does Not Exist")
End If
End Sub
-
Dec 5th, 2021, 05:56 PM
#6
Re: VB .NET Split to catch exact value from txt file
Why did you wrap the messagebox in an exception handler? Was that just a mistake? Wrapping file handling in an exception handler makes sense, but not just the messagebox.
You could consolidate these three lines:
Code:
Dim firstLine = IO.File.ReadLines(filetoreadfrom).First()
Dim fields = firstLine.Split(","c)
Dim fifthElement = fields(4)
Into one line, but you might prefer it the way you have it. I think putting it in two lines makes sense (consolidating the second and third), but I wouldn't consolidate the first line into that, because I'd feel it too awkward to read.
My usual boring signature: Nothing
-
Dec 5th, 2021, 06:15 PM
#7
Thread Starter
Lively Member
Re: VB .NET Split to catch exact value from txt file
Yes, the code is clumsy now, it is just for testing purposes. Now, when id does what I want to, I'll make it more clear and wrap it in a new WinForm, and make a few more adjustments regarding SystemFileWatcher (bug) that triggers multiple times when it shouldn't.
Right now, my App creates a file and then reads from it. Later on, the distribution version shall read the file only. A fiscal printer creates a file in the first place.
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
|