Author Topic: BasicDataLogger2G not printing header on new file  (Read 897 times)

g.mccormick

  • Newbie
  • *
  • Posts: 29
    • View Profile
BasicDataLogger2G not printing header on new file
« on: September 15, 2021, 12:34:24 PM »
I am using BasicDataLogger2G and I have selected to create new file daily as well as include a header.  The log file that is created when I first launch the application has the header in it, however, all subsequent files (made new daily) do not have the header.  Am I missing something?

g.mccormick

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: BasicDataLogger2G not printing header on new file
« Reply #1 on: September 22, 2021, 11:11:21 AM »
I was able to do a work around. 
Code: [Select]
   Private Sub FileManipTmr_Tick(sender As Object, e As EventArgs) Handles FileManipTmr.Tick

        ManipTickCntr += 1

        lblManipTickCntr.Text = ManipTickCntr


        hour = DateAndTime.Now.Hour
        mins = DateAndTime.Now.Minute

        lblHour.Text = hour
        lblMins.Text = mins


        'Following runs every 30sec

        'We have an issue that when new files are created on a new day, the header information
        'is not getting added.

        Dim monStr, DayStr, YrStr, LogFileName, HeaderFileName As String


        'The log file from the data log includes the day, month, and year in the filename.
        'We know the root of the filename but need to construct the rest with day, month, yr.

        'Get the day, month, and year as strings.
        DayStr = DateAndTime.Now.ToString("dd")
        monStr = MonthName(DateAndTime.Now.Month, True)
        YrStr = DateAndTime.Now.Year

        'Construct the full .log file name.
        LogFileName = PowerMtrEvntLog.FileFolder + "\" + PowerMtrEvntLog.FileName + "-" + DayStr + "-" + monStr + "-" + YrStr + ".log"
        HeaderFileName = PowerMtrEvntLog.FileFolder & "\Header.txt"

        txtFilename.Text = LogFileName


       [color=red] 'We will check the file for a header only once a day. If the hour before hour 22(10pm) and min >= 5 and we haven't checked it yet.

          If (hour < 23) And (mins >= 5) And Not TestHeader Then
       

            TestHeader = True  'Set to true so that we don't go though here again until we reset the bit


            'First make sure that the file exists and if it does read the first line.
            If My.Computer.FileSystem.FileExists(LogFileName) Then

                'Read all of the lines from the file.  Each line will take one array element of line() array
                Dim line() As String = IO.File.ReadAllLines(LogFileName)

                'Get the first 9 characters of the first line which is held in line(0) as long as line(0) is greater than 9 chars long
                If line(0).Length > 9 Then
                    Dim lineChars As String = line(0).Substring(0, 9)
                    Dim testchar As String = "Date-Time"

                    'Evaluate against the expected value.
                    'If the first 9 chars Of the first line In the file Is Not "Data-Time" Then we need To add the header In
                    If Not lineChars = testchar Then

                        'Read in log file
                        Dim LogFileReader As String
                        LogFileReader = My.Computer.FileSystem.ReadAllText(LogFileName)

                        'Read in Headerfile
                        Dim HeaderFileReader As String
                        HeaderFileReader = My.Computer.FileSystem.ReadAllText(HeaderFileName)

                        'Combine and write back to logfile
                        IO.File.WriteAllText(LogFileName, HeaderFileReader & vbNewLine & LogFileReader)

                    End If
                End If
            End If 'End  If My.Computer.FileSystem.FileExists(LogFileName) Then

        End If

        'Reset the TestHeader bool. 
        If (hour > 22) And TestHeader Then
            TestHeader = False
        End If[/color]

        'We want to copy the .log file to .csv.  We will do this only once a day at 12:05.
        'Since a new file is generated daily, we need to remember the previous days file name.

        If (hour > 17) Then
            YesterdayFile = LogFileName
        End If

        'At roughly 1:05AM copy yesterdays file to .csv file

        If (hour > 0) And (hour < 2) And (mins >= 5) And Not MakeCsv Then
            MakeCsv = True

            CsvFile = YesterdayFile.Replace(".log", ".csv")

            If System.IO.File.Exists(YesterdayFile) Then
                My.Computer.FileSystem.CopyFile(YesterdayFile, CsvFile, overwrite:=True)
                txtcsvfilename.Text = CsvFile

            End If

        End If

        'reset the MakeCsv so that we will do the previous steps again tomorrow
        If (hour > 3) And MakeCsv Then
            MakeCsv = False
        End If


        'Email. At 12:10AM if the csv file exists, then we want to email it.
        If System.IO.File.Exists(CsvFile) And (hour = 1) And (mins >= 10) And Not AutoEmailLock Then
            AutoEmailLock = True
            SendEmail = True

        End If
        If (hour > 2) And AutoEmailLock Then
            AutoEmailLock = False
        End If


    End Sub