Hello, I have written a code which opens, edits, and saves text files by taking a start date and an end date from the user (the text files are named and located according to their date). The code is functional (I've tried it on small spans and small files) but the actual text files I want to implement this on are about 24,500 lines long (and there is one for every day of the year from 1999 till the present). I am looking to optimize the code as much as possible, I have heard that not updating the textbox for every line read would make things better but I do not know how to implement it other than this way. Any suggestion would be helpful, please keep in mind I am a beginner at VB.
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			
			
				VB.NET:
			
		
		
		Imports System.IO
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim watch As Stopwatch = Stopwatch.StartNew()   'Starts stopwatch
        Dim Reader As System.IO.StreamReader        'Declares the Reader
        Dim Writer As System.IO.StreamWriter        'Declares the Writer
        Dim i As Date                               'Loop counter (by date)
        Dim j As Integer                            'Loop counter
        Dim k As Integer                            'Files notFound counter
        Dim StartDate As Date                       'Date to begin compiling text files
        Dim EndDate As Date                         'Date to stop compiling text files
        Dim SaveTo As String                        'Name of the file that will be saved
        Dim Yearv As String                         'Year value
        Dim Monthv1 As Integer                      'Month value
        Dim Monthv As String                        'Month value
        Dim Dayv1 As Integer                        'Day value
        Dim Dayv As String                          'Day value
        Dim DayofWeek As String                     'Weekday value
        Dim YearMonthv As String                    'Year and Month value
        Dim CurrentDate As String                   'Date of the file being worked on
        Dim line As String                          'Holds individual lines from the Reader
        Dim found As Boolean                        'Marks instances when a file does not exist
        Dim array() As String = {"All files present."}   'Holds text to be printed
        Try
            StartDate = "#" & txtStartDate.Text & "#"   'User enters start & end date in textboxes
            EndDate = "#" & txtEndDate.Text & "#"
        Catch                                           'Catches invalid dates and exits with message
            MsgBox("Date(s) not valid.")
            Exit Sub
        End Try
        If StartDate > EndDate Then                     'Catches unordered dates and exits with message
            MsgBox("Start date cannot be after end date.")
            Exit Sub
        End If
        i = StartDate                               'Hold StartDate for counting
        j = 0                                       'Intializes loop counter
        k = 0                                       'Intialize number of times a file hasn't been found
        found = True                                'File is assumed to exist unless not found
        TextBox1.Clear()                            'Clears textboxes
        notFound.Clear()
        While (i <= EndDate)                        'Loop runs from start date to end date
            j = j + 1                               'Count loops
            Yearv = Str(i.Year)                     'Assigning Year, Month, Day, and Day of Week to different elements
            Monthv1 = i.Month
            Dayv1 = i.Day
            DayofWeek = WeekdayName(Weekday(i))
            If Monthv1 < 10 Then                                'Converts 1 to 01 for Month no. (due to naming convention of the files)
                Monthv = "0" + Trim(Str(Monthv1))
                YearMonthv = Yearv + Monthv                     'Corrects YearMonthv accordingly
            Else
                Monthv = Trim(Str(Monthv1))
                YearMonthv = Trim(Trim(Yearv) + Trim(Monthv))
            End If
            If Dayv1 < 10 Then                                      'Converts 1 to 01 for Day no. (due to naming convention of the files)
                Dayv = "0" + Trim(Str(Dayv1))
            Else
                Dayv = Str(Dayv1)
            End If
            CurrentDate = Trim(Yearv) + Trim(Monthv) + Trim(Dayv)   'Sets current date as string (due to naming convention of the                                                                                              'files)
            SaveTo = "C:\Documents and Settings\Walid\Desktop\September\" _
                & Trim(YearMonthv) & "\a" & Trim(CurrentDate) & ".txt"                          'Name of file to be saved
            Try                                                                                 'Checking if file to be opened exists
                Reader = File.OpenText("C:\Documents and Settings\Walid\Desktop\September\" _
                            & Trim(YearMonthv) & "\c" & Trim(CurrentDate) & ".txt")             'Name of file to be opened
            Catch
                ReDim Preserve array(k)
                array(k) = SaveTo
                k = k + 1                                                                       'Counts number of files not found
                found = False                                                                   'Current date has no associated file
            End Try
            If found = True Then                                        'Reads only if file exists
                Do Until Reader.EndOfStream                             'Reads through the open file line by line to last line
                    line = Reader.ReadLine                              'Holds individual line for the iteration
                    If line.Contains("VOL") Then                        'Looks for the header
                        line = "Time,DET#,VOL,OCC,Date of:,Day,Type,"   'Modifies the header
                    Else
                        line = line + Trim(Yearv) + " " + Trim(Monthv) + " " _
                            + Trim(Dayv) + "," + Trim(DayofWeek) + ",c"             'Adds info to all other lines
                    End If
                    TextBox1.Text = TextBox1.Text & line & vbCrLf                   'Assigns the current line to the textbox
                Loop                                                                'Reiterate
                Reader.Close()                                                      'Closes the reader
            End If
            If found = True Then                        'Only saves files whose read files exists
                Writer = My.Computer.FileSystem. _
                OpenTextFileWriter(SaveTo, True)        'Opens text document
                Writer.WriteLine(TextBox1.Text)         'Writes the text in the text box
                Writer.Close()                          'Closes the Writer
            End If
            TextBox1.Clear()                            'Clears textbox
            found = True                                'Reintialized as true
            i = i.AddDays(1)                            'Increments date by one day
        End While
        watch.Stop()                                    'Stops stopwatch
        TextBox1.Text = "Program Complete. A total of " & (j - k) & _
            " text files compiled." & vbCrLf & k & _
            " files were not found or written." & vbCrLf & _
            "Program took " & Str(watch.Elapsed.TotalSeconds) & _
            " second to run."                                           'Output text
        For Each item In array
            notFound.Text += item & vbCrLf
        Next item
    End Sub 
	 
 
		 
 
		 
 
		