-
Apr 19th, 2022, 08:50 AM
#1
Thread Starter
Addicted Member
VB.NET read text file data between two dates
Hi All.. how can I read data from a text file between two dates? Right now I am just going back X-days from today with this from a comma delimited text file:
Code:
Dim lines = IO.File.ReadAllLines(filename)
For Each line In lines
Dim Data = line.Split(","c)
If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
Thanks in advance.
Example Data:
Code:
8-879003-02_BTM,B1022,741,55,4/18/2022 6:33:49 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,857,59,4/18/2022 6:33:59 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1024,55,4/18/2022 6:34:02 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,284,669,4/18/2022 6:34:13 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,544,673,4/18/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,724,677,4/18/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1000,667,4/18/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1039,672,4/18/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_TOP,B1023,930,175,4/18/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
8-879003-02_TOP,B1023,845,232,4/18/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
8-879003-02_TOP,B1023,489,375,4/18/2022 6:51:39 AM,LiTran,Missing Part,Color [Yellow],A2523217,
4-946020-12_BTM,-5345,66,265,4/18/2022 9:27:38 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5345,510,231,4/18/2022 9:28:24 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5345,485,324,4/18/2022 9:29:00 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5345,567,136,4/18/2022 9:29:36 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
4-946020-12_BTM,-5345,593,469,4/18/2022 9:30:05 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
4-946020-12_BTM,-5346,65,268,4/18/2022 9:32:03 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5346,509,234,4/18/2022 9:32:56 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5346,493,311,4/18/2022 9:33:48 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5346,567,460,4/18/2022 9:35:50 AM,thvan,Solder Bridge,Color [Chartreuse],A2524939,
4-946020-12_BTM,-5347,70,275,4/18/2022 9:37:06 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5347,513,235,4/18/2022 9:37:55 AM,thvan,Insuff Solder,Color [Orange],A2524939,
Last edited by mikeg71; Apr 19th, 2022 at 09:06 AM.
-
Apr 19th, 2022, 08:57 AM
#2
Re: VB.NET read text file data between two dates
Maybe you could show us some example data. Will the data be sequential by date? If so, in ascending or descending order?
-
Apr 19th, 2022, 09:07 AM
#3
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Thanks for the reply jmcilhinney, I have edited my post. All data is in date order descending.
-
Apr 19th, 2022, 10:30 AM
#4
Re: VB.NET read text file data between two dates
This code works for me:
vb.net Code:
Private Sub ReadLinesByDateRange(startDate As Date, endDate As Date)
Dim filePath = Path.Combine(Environment.CurrentDirectory, "TextFile1.txt")
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)
Dim lines = File.ReadLines(filePath).
SkipWhile(Function(line) getDateTime(line) < startDate).
TakeWhile(Function(line) getDateTime(line) < endDate)
For Each line In lines
Console.WriteLine(line)
Next
End Sub
Note that, in that code, startDate is inclusive and endDate is exclusive. In my testing, I used this data:
Code:
8-879003-02_BTM,B1022,741,55,4/18/2022 6:33:49 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,857,59,4/18/2022 6:33:59 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1024,55,4/19/2022 6:34:02 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,284,669,4/19/2022 6:34:13 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,544,673,4/20/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,724,677,4/20/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1000,667,4/21/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1039,672,4/21/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_TOP,B1023,930,175,4/22/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
8-879003-02_TOP,B1023,845,232,4/22/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
8-879003-02_TOP,B1023,489,375,4/23/2022 6:51:39 AM,LiTran,Missing Part,Color [Yellow],A2523217,
4-946020-12_BTM,-5345,66,265,4/23/2022 9:27:38 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5345,510,231,4/24/2022 9:28:24 AM,thvan,Insuff Solder,Color [Orange],A2524939,
4-946020-12_BTM,-5345,485,324,4/24/2022 9:29:00 AM,thvan,Insuff Solder,Color [Orange],A2524939,
and called the method like this:
vb.net Code:
ReadLinesByDateRange(#4/20/2022#, #4/23/2022#)
and got this output:
Code:
8-879003-02_BTM,B1022,544,673,4/20/2022 6:34:17 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,724,677,4/20/2022 6:34:21 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1000,667,4/21/2022 6:34:26 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_BTM,B1022,1039,672,4/21/2022 6:34:29 AM,LiTran,Wrong Prep,Color [DarkOrchid],A2523217,
8-879003-02_TOP,B1023,930,175,4/22/2022 6:35:12 AM,LiTran,No Solder,Color [LightSkyBlue],A2523217,
8-879003-02_TOP,B1023,845,232,4/22/2022 6:37:24 AM,LiTran,Missing Part,Color [Yellow],A2523217,
so records on or after the startDate and before the endDate.
Note that my code calls ReadLines rather than ReadAllLines. The latter will read every line first and return them in an array, which you could then process. The former will basically return each line for processing before reading the next. One advantage of that is that you can stop reading any time that you know that you don't need any more data. That's a boon for big files in particular, especially when the data you need is near the beginning.
-
Apr 19th, 2022, 10:48 AM
#5
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Thanks for your help on this.. I am going to try it out now. Would this be something that I could easily use with a date/time picker?
-
Apr 19th, 2022, 10:53 AM
#6
Re: VB.NET read text file data between two dates
Originally Posted by mikeg71
Would this be something that I could easily use with a date/time picker?
Of course. The method takes two Date values as arguments. Where you get those Dates from is completely up to you, e.g. the Value properties of two DateTimePicker controls.
-
Apr 20th, 2022, 05:17 PM
#7
Lively Member
Re: VB.NET read text file data between two dates
I think your looking for concatenate ex:
Code:
Dim words = "hello I'm text"
Dim endwords = "ending text"
Dim datenow = Date.Now()
Dim spacetoconcat
Spacetoconcat = words + datenow.tostring() + endwords
'theres spacer methods in the vb
May be & to
-
Apr 20th, 2022, 05:24 PM
#8
Re: VB.NET read text file data between two dates
Originally Posted by mmx88_ValidUser
I think your looking for concatenate ex:
Code:
Dim words = "hello I'm text"
Dim endwords = "ending text"
Dim datenow = Date.Now()
Dim spacetoconcat
Spacetoconcat = words + datenow.tostring() + endwords
'theres spacer methods in the vb
May be & to
Maybe sit the next couple plays out...
-
Apr 20th, 2022, 05:26 PM
#9
Lively Member
Re: VB.NET read text file data between two dates
Originally Posted by OptionBase1
Maybe sit the next couple plays out...
I'm gonna play with out like two dogs caged yo
-
Apr 25th, 2022, 01:00 PM
#10
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Hi jmcilhinney... I am struggling a bit trying to remove what I need from my old code. How can I fit yours in? I am going to use a new form for a 'datetimepicker' to get two dates. I can then grab those variables, but I think I am just getting myself confused with this as I have not yet used a datetimepicker. I was able to get the datepicker values from the new form, now I just need to get it all sorted. Here is what I have now for how I have been doing it:
Code:
Dim filename = IO.Path.Combine(Environment.CurrentDirectory, "Defect_Files", "Defects.txt")
Dim HowMnyDays = InputBox(vbCrLf & "How Many Days Back Do You Want To Review?", "Days to Review")
If HowMnyDays Is Nothing Then Exit Sub
Try
If (IO.File.Exists(filename)) Then
Dim lines = IO.File.ReadAllLines(filename)
For Each line In lines
Dim Data = line.Split(","c)
If (Data.Length > 0 AndAlso Data(0) = ToggleFile) Then
If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
TextBox1.Text = "ALL-DEFECTS"
Dim fromTextFile As String = Data(7)
Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)
Dim dynamicButton As New Button With {
.Location = New Point(CInt(Data(2)), CInt(Data(3))),
.Height = 7,
.Width = 7,
.FlatStyle = FlatStyle.Flat,
.BackColor = Drawing.Color.FromName(color),
.ForeColor = Drawing.Color.FromName(color)
}
-
Apr 25th, 2022, 10:12 PM
#11
Re: VB.NET read text file data between two dates
You have this:
vb.net Code:
Dim lines = IO.File.ReadAllLines(filename)
For Each line In lines
I have this:
vb.net Code:
Dim lines = File.ReadLines(filePath).
SkipWhile(Function(line) getDateTime(line) < startDate).
TakeWhile(Function(line) getDateTime(line) < endDate)
For Each line In lines
I'm not sure what isn't clear about what you need to change.
-
Apr 26th, 2022, 04:22 PM
#12
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Ok, this is what I have right now in my button code:
Code:
Private Sub BtnCreateMap_Click(sender As Object, e As EventArgs) Handles BtnCreateMap.Click
If PictureBox1.Image Is Nothing Then
MsgBox("No CCA image to create Heat-Map", vbExclamation, "")
Exit Sub
End If
Dim filename = IO.Path.Combine(Environment.CurrentDirectory, "Defect_Files", "HeatMap_Defects.txt")
Dim HowMnyDays = InputBox(vbCrLf & "How Many Days Back Do You Want To Review?", "Days to Review")
PickDate.Show() <<< DatePicker Form <<<
If HowMnyDays Is Nothing Then Exit Sub <<< This should be replaced by DatePicker values <<<
Try
If (IO.File.Exists(filename)) Then
Dim lines = IO.File.ReadAllLines(filename)
For Each line In lines
Dim Data = line.Split(","c)
If (Data.Length > 0 AndAlso Data(0) = ToggleFile) Then
If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
TextBox1.Text = "DEFECT MAP"
Dim fromTextFile As String = Data(7)
Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)
Dim dynamicButton As New Button With {
.Location = New Point(CInt(Data(2)), CInt(Data(3))),
.Height = 7,
.Width = 7,
.FlatStyle = FlatStyle.Flat,
.BackColor = Drawing.Color.FromName(color),
.ForeColor = Drawing.Color.FromName(color)
}
If (Data.Length > 4) Then
ToolTip1.SetToolTip(dynamicButton, $"SN: {Data(1)} '{Data(6)}'")
End If
If PictureBox1.Visible = True Then
PictureBox1.Select()
If Not color.ToString = "Empty" Then
PictureBox1.Controls.Add(dynamicButton)
End If
End If
If PictureBox2.Visible = True Then
PictureBox2.Select()
If Not color.ToString = "Empty" Then
PictureBox2.Controls.Add(dynamicButton)
End If
End If
End If
End If
Next
End If
End Sub
So from my DatePicker form values, how do I call the function you created? When I tried to merge all of my button code in with your function, I get an error about converting a string to an integer when trying to look at another value within the split file. I hope I am making sense trying to explain this.
Here is my DatePicker Form code:
Code:
Public Class PickDate
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.dpFrom = DatePickerFROM.Value
Form1.dpTo = DatePickerTO.Value
Dim result As TimeSpan = Form1.dpFrom.Subtract(Form1.dpTo)
Dim ds As Integer = result.TotalDays
TextBox1.Text = ds
MsgBox(DatePickerFROM.Value & " - " & DatePickerTO.Value)
End Sub
End Class
-
Apr 26th, 2022, 08:19 PM
#13
Re: VB.NET read text file data between two dates
If you are using a separate form with two DateTimePickers on it then you should display that as a modal dialogue and get the date values via properties, e.g. in the second form:
vb.net Code:
Public ReadOnly Property StartDate As Date
Get
Return startDatePicker.Value.Date
End Get
End Property
Public ReadOnly Property EndDate As Date
Get
Return endDatePicker.Value.Date
End Get
End Property
and in the first form:
vb.net Code:
Using dialogue As New Form2
If dialogue.ShowDialog() = DialogResult.OK Then
ReadLinesByDateRange(dialogue.StartDate, dialogue.EndDate)
End If
End Using
-
Apr 28th, 2022, 11:08 AM
#14
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Sorry, I am still getting lost between the two forms. I put your code in my 'DatePicker' form and I do get the form to popup, then I select two dates. What do I need to do in order to get the values automatically after selecting them on the datepicker form? I am guessing that needs to somehow replace my original line here:
Code:
If (CDate(Data(4)) > DateTime.Now.AddDays(-CInt(HowMnyDays))) Then
Never dealing with a datepicker, I feel totally lost on this.
-
Apr 29th, 2022, 01:12 AM
#15
Re: VB.NET read text file data between two dates
You're trying to do every6thing backwards. Stop! Think about the logic first and then write code to implement that logic. If that means redoing something you've already done then so be it.
Start by thinking about the date input only. What do you need? You need a form with two date controls that will pass that input back to the caller. Do that and that only. I've already told you what to do. Do that now. Once that's done, the date controls become completely irrelevant to the question of how to read lines from a file. You simply display the dialogue and get the date values, as I have shown you how to do, and then pass those values to the code that reads the file, as I've shown you how to do. getting the date input and using the date input are two co0mpletely separate issues so start treating them that way. One of the main reasons that beginners have problems is that they treat everything as one big problem instead of several little ones. The smaller the problem, the easier it is to solve, so break the big problem down into several smaller ones and solve each one independently. using date input is the same regardless of how you get it and getting date input is the same regardless of how you use it, so neither subproblem is at all dependent on the other. That also means that no question posted on this or similar sites should be asking about both.
-
Apr 29th, 2022, 08:46 AM
#16
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
thanks jmcilhinney, I see your point on this and appreciate you explaining it. I am going to look closely at this over the weekend and break it down piece by piece.
-
May 18th, 2022, 09:56 AM
#17
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Ok, I am just now finally getting back to this. I can't seem to get beyond this error and do not understand what can be causing it. The data is the same with the same date format etc.
Attachment 184875
-
May 18th, 2022, 10:01 AM
#18
Lively Member
Re: VB.NET read text file data between two dates
Wow some of this code examples look like trash from vb6
-
May 18th, 2022, 08:12 PM
#19
Re: VB.NET read text file data between two dates
Originally Posted by mikeg71
Ok, I am just now finally getting back to this. I can't seem to get beyond this error and do not understand what can be causing it. The data is the same with the same date format etc.
Attachment 184875
Your attachment is invalid. That's the site's fault rather than yours - there's a bug with inline attachments - but that's irrelevant anyway. There's no need to attach anything. Post the relevant code as text, formatted as code. Post error message as the text they are. Attachments just make things harder, even when they do work. They have their uses but basic code snippets and error messages is not one of them.
-
May 19th, 2022, 12:36 PM
#20
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
Thanks for the info on that, I will make sure I don't do attachments knowing that.
Here is the portion from your original code and my modified version (which seems to work). I don't understand why this makes a difference, but when I run the original, I get "string not recognized as a valid date/time"
Code:
'modified line
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
'your original line
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)
-
May 19th, 2022, 02:53 PM
#21
Thread Starter
Addicted Member
Re: VB.NET read text file data between two dates
I seem to have made it beyond the datepicker, but now I have another glitch. When trying to get my color from the text file, I get the error in the marked line below. Is this because I am re-splitting the already split file?
Code:
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
Dim lines = File.ReadAllLines(filePath).
SkipWhile(Function(line) getDateTime(line) < startDate).
TakeWhile(Function(line) getDateTime(line) < endDate)
For Each line In lines
MsgBox(line)
TextBox1.Text = "ALL DEFECTS"
Dim fromTextFile As String = line(7)
Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1) <<<< NEW ERROR: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Dim dynamicButton As New Button With {
.Location = New Point(CInt(line(2).ToString), CInt(line(3).ToString)),
.Height = 7,
.Width = 7,
.FlatStyle = FlatStyle.Flat,
.BackColor = Drawing.Color.FromName(color),
.ForeColor = Drawing.Color.FromName(color)
}
-
May 19th, 2022, 08:16 PM
#22
Re: VB.NET read text file data between two dates
Originally Posted by mikeg71
Thanks for the info on that, I will make sure I don't do attachments knowing that.
Here is the portion from your original code and my modified version (which seems to work). I don't understand why this makes a difference, but when I run the original, I get "string not recognized as a valid date/time"
Code:
'modified line
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
'your original line
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/dd/yyyy h:mm:ss tt", Nothing)
Look at the difference between them. One is specifying a 2-digit daya and the other is specifying a 1-digit day. Is you day 2 digits? Did you read the documentation about date/time formatting? If you have a day value less than 10 and it does not have a leading 0 on it then that is not valid if using "dd" for the day.
-
May 19th, 2022, 08:20 PM
#23
Re: VB.NET read text file data between two dates
Originally Posted by mikeg71
I seem to have made it beyond the datepicker, but now I have another glitch. When trying to get my color from the text file, I get the error in the marked line below. Is this because I am re-splitting the already split file?
Code:
Dim getDateTime = Function(line) Date.ParseExact(line.Split(","c)(4), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
Dim lines = File.ReadAllLines(filePath).
SkipWhile(Function(line) getDateTime(line) < startDate).
TakeWhile(Function(line) getDateTime(line) < endDate)
For Each line In lines
MsgBox(line)
TextBox1.Text = "ALL DEFECTS"
Dim fromTextFile As String = line(7)
Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1) <<<< NEW ERROR: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Dim dynamicButton As New Button With {
.Location = New Point(CInt(line(2).ToString), CInt(line(3).ToString)),
.Height = 7,
.Width = 7,
.FlatStyle = FlatStyle.Flat,
.BackColor = Drawing.Color.FromName(color),
.ForeColor = Drawing.Color.FromName(color)
}
Use the debugger. The error message is telling you that the index you're using is out of range so look at the index you're using and the valid range to see why it's wrong. The index is hard-coded right there in your code: 1. Obviously there is not element at index 1 in that array. Did you look at the array you got back from Split to see what it contained, or even the line you were splitting beforehand? It would seem not, but why not? If you don't know how to use the debugger then stop what you're doing and learn that first because you need to have debugged your code before posting a question.
-
May 21st, 2022, 06:54 PM
#24
Lively Member
Re: VB.NET read text file data between two dates
Remember events are like structures there purpose is fast and accurate memory access.
-
May 21st, 2022, 07:47 PM
#25
Re: VB.NET read text file data between two dates
Originally Posted by mmx88_ValidUser
Remember events are like structures there purpose is fast and accurate memory access.
mmx88_ValidUser, would you please keep your gibberish in Chit Chat!
Tags for this Thread
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
|