Search textfile for records =< date range

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi all

I have a txtfile that contains customer info. each line in the file is a different customer. The line length is 581 characters long. Each line contains a date for when the product was last serviced the date starts at character 474 and is 8 characters long with the following format ##/##/## I have a textbox where the user can enter a date in the same format.
I am trying to create a program that searches through the text file for any customer record in which the service date is equal to or before the date in the textbox (i.e Service is due/overdue.) It should then record the customer name in another text file for processing later.

This is what I have so Far but is dosnt seem to work

VB.NET:
Dim File_NameIN As String = "C:\DRITXT\CUSTKEYA.txt"  ' Adds Source file location to a String Variable
        Dim File_NameOUT As String = "C:\DRITXT\LETTER.txt"   ' Adds Destination file location to a String Variable
        Dim objWriter As New System.IO.StreamWriter(File_NameOUT)
        Dim DateRange As String = txtDateRange.Text
        Dim ServiceDate As String

        For Each line As String In File.ReadAllLines(File_NameIN)

            ServiceDate = line.Substring(474, 8)

            If ServiceDate = DateRange Then
                Name = line.Substring(0, 30).ToLower
                objWriter.WriteLine(Name)
            End If
        Next line
        objWriter.Close()

I get this error "startIndex cannot be larger than length of string. Parameter name: startIndex" for the line ServiceDate = line.substring(474,8)

Can anyone shed some light on this

Many Thanks
 
The line length is 581 characters long.
I get this error "startIndex cannot be larger than length of string. Parameter name: startIndex" for the line ServiceDate = line.substring(474,8)
You must have at least one line that is shorter than that (474). You can look at content of line variable in debugger when it breaks. If you also need to know which line number it occurs in you can use a For Next loop and look at index. If you don't care whether or where there could be such lines you can check length of string before using it.
 
You must have at least one line that is shorter than that (474). You can look at content of line variable in debugger when it breaks. If you also need to know which line number it occurs in you can use a For Next loop and look at index. If you don't care whether or where there could be such lines you can check length of string before using it.

Thank you JohnH

My code now looks like this which appears to work, however it appears to be only searching on the first 2 numbers of the date. I need it to search based on the whole date. Where am I going wrong?

VB.NET:
 Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) Handles cmdSearch.Click

        Dim File_NameIN As String = "C:\DRITXT\CUSTKEYA.txt"  ' Adds Source file location to a String Variable
        Dim File_NameOUT As String = "C:\DRITXT\LETTER.txt"   ' Adds Destination file location to a String Variable
        Dim objWriter As New System.IO.StreamWriter(File_NameOUT)
        Dim DateRange As String = txtDateRange.Text
        Dim ServiceDate As String

        For Each line As String In File.ReadAllLines(File_NameIN)
            If line.Length = 581 Then
                ServiceDate = line.Substring(473, 8)


                If ServiceDate < DateRange Then
                    Name = line.Substring(0, 30).ToLower
                    ServiceDate = line.Substring(473, 8)
                    objWriter.WriteLine(Name & ServiceDate)
                End If
            End If
        Next line
        objWriter.Close()

    End Sub
 
Strings can not give you date comparisons. You must parse the date strings to Date objects, then you can compare these logically. Use Date.Parse or Date.ParseExact, or if it is possible the input may not be parsable use Date.TryParse/TryParseExact.

"txtDateRange.Text", sounds like a TextBox, maybe you should give user a friendlier UI for choosing a Date value, like DateTimePicker (Forms) control perhaps?
 
Strings can not give you date comparisons. You must parse the date strings to Date objects, then you can compare these logically. Use Date.Parse or Date.ParseExact, or if it is possible the input may not be parsable use Date.TryParse/TryParseExact.

"txtDateRange.Text", sounds like a TextBox, maybe you should give user a friendlier UI for choosing a Date value, like DateTimePicker (Forms) control perhaps?

Ah that would make sense. But I am unsure how to do this with my current code. I have searched online but I havent found anything that I can make sense of.
 
If your date string is system default format it is simple to use Date.Parse:
Dim datevalue = Date.Parse(stringvalue)

Date data type is represented by the System.DateTime structure, so you will find all the documentation for Date members there: DateTime Structure (System)
 
If your date string is system default format it is simple to use Date.Parse:
Dim datevalue = Date.Parse(stringvalue)

Date data type is represented by the System.DateTime structure, so you will find all the documentation for Date members there: DateTime Structure (System)

Sorry to be a pain, really wanna get my head round this.

This is what I have now

VB.NET:
Dim File_NameIN As String = "C:\DRITXT\CUSTKEYA.txt"        ' Adds Source file location to a String Variable
        Dim File_NameOUT As String = "C:\DRITXT\LETTER.txt"         ' Adds Destination file location to a String Variable
        Dim objWriter As New System.IO.StreamWriter(File_NameOUT)
        Dim DateRange As String = txtDateRange.Text                 ' Assigns the date range entered in to the txtbox to a variable
        Dim DateRange1 = Date.Parse(DateRange)
        Dim ServiceDate As String
        Dim DateFormat As String = ("dd/mm/yy")

        For Each line As String In File.ReadAllLines(File_NameIN)


            If line.Length = 581 Then
                ServiceDate = line.Substring(473, 8).ToString
                Dim datevalue = Date.Parse(ServiceDate)
                If datevalue <= DateRange1 Then
                    Name = line.Substring(0, 30).ToLower
                    ServiceDate = line.Substring(473, 8)
                    objWriter.WriteLine(Name & ServiceDate)
                End If
            End If

        Next line
        objWriter.Close()

I now get this error "String was not recognized as a valid DateTime." my date format is dd/mm/yy
 
Are you sure about that? dd/MM/yy is a default format for UK, and when I try Parse with that culture it correctly parses such string dates. What culture is your computer configured as? Here in this example I'm parsing as default UK:
        Dim datestring = "22/11/99"
        Dim c = Globalization.CultureInfo.GetCultureInfo("en-GB")
        Dim d = Date.Parse(datestring, c) 'd.Day:22, d.Month:11, d.Year:1999
 
Are you sure about that? dd/MM/yy is a default format for UK, and when I try Parse with that culture it correctly parses such string dates. What culture is your computer configured as? Here in this example I'm parsing as default UK:
        Dim datestring = "22/11/99"
        Dim c = Globalization.CultureInfo.GetCultureInfo("en-GB")
        Dim d = Date.Parse(datestring, c) 'd.Day:22, d.Month:11, d.Year:1999

Think I may have found the problem.

If one of the lines has no Service date, I.e the system has never been serviced would this through up the error?

Thank you for all your help with this, I am learning loads
 
If one of the lines has no Service date, I.e the system has never been serviced would this through up the error?
Of course, if string is not parsable as a Date then Parse will throw exception. There could be different ways of validating your data lines, but as mentioned Date.TryParse has the specific purpose of trying to parse as date when it is possible it could not be a date, and do this without throwing exception. This is especially important if you allow user to type a date string in a textbox, since user could type anything.
        Dim datestring = "22/11/99" 
        Dim d As Date
        If Date.TryParse(datestring, d) Then
            'd.Day:22, d.Month:11, d.Year:1999
        Else
           ' could not parse input as date
        End If
 
Of course, if string is not parsable as a Date then Parse will throw exception. There could be different ways of validating your data lines, but as mentioned Date.TryParse has the specific purpose of trying to parse as date when it is possible it could not be a date, and do this without throwing exception. This is especially important if you allow user to type a date string in a textbox, since user could type anything.
        Dim datestring = "22/11/99" 
        Dim d As Date
        If Date.TryParse(datestring, d) Then
            'd.Day:22, d.Month:11, d.Year:1999
        Else
           ' could not parse input as date
        End If

Problem solved :liplick::liplick:

Thank you for all your help.
 
Back
Top