1

I'm creating an application that runs several times a day. The following code below is a small section that starts logging text into .txt file.

Sub Main()


    Dim logFile As String
    Dim logFolderPath As String


    logFile = "C:\Users\myname\Desktop\testfolder\test.txt" 
    logFolderPath = System.IO.Path.GetDirectoryName(logFile) 'log Folder's path

    'creates folder to allow logging if path does not exist 
    If System.IO.Directory.Exists(logFolderPath) = False Then
        System.IO.Directory.CreateDirectory(logFolderPath)
    End If


    Dim todaysdate As String = String.Format("{0:MM/dd/yyyy hh:mm tt}", DateTime.Now)
    Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\myname\Desktop\testfolder\test.txt", True)
    file.WriteLine("----------Initiation----------")



 'more code below doing stuff and logging errors

After the program is runs sveral times the .txt is in this format:

enter image description here

How can I delete certain sections of the text file once they x amount of days old? Or at the very least, delete sections towards the top?

I have found several posts but for deleting the whole .txt file itself but not for deleting its contents

EDIT: The dates in the picture are all the same because I just ran the program several times for the purpose of showing what it would look like. The main point is that the dates are different.

Roger Rivas
  • 45
  • 1
  • 6

2 Answers2

0

You cannot know which line in the file is the correct place to truncate without reading the line itself, so you have to read the lines one by one to check its datetime values. There are good posts about truncating that you can refer such as this. Alternatively, you can create an additional text file (like an index in databases) which contains key-value pairs such as date-line index values. Then, you can point to the right position in the content of the file without reading the content. However; this will increase the complexity of your file handling so you should avoid this alternative unless it is vital for your requirements.

ali
  • 1,301
  • 10
  • 12
0

If your file isn't going to grow too large you can to define a unique token you want to use determine the start of each block.

For your example, you could use ----------END----------

Once you have that token, you can split the contents of the file into an array.

In each array element you'll have to determine the date for that element. Since you know ----------END---------- will be you token deliminator we can use the last line in each block to read the date from.

Since we know that last block will in the format `[space]m/d/y[space]h:m:s:z' We can read the date for the block by splitting the line using a space character and then using the second to last element in that date as our array.

You can accomplish this with a few lines of code:

Dim fileText = fileContent.Trim
Dim token As String = "----------END----------" '- -vbNewLine & vbNewLine
Dim output = (String.Join(token, (From block In Split(fileText, token).Where(
             Function(b)
                 Dim blockLines() = Split(b.Trim, vbLf)
                 If blockLines.Length > 2 Then
                     Dim parts = Split(blockLines.Last(), " ")
                     Dim dt As Date = parts(parts.Length - 2)
                     Return Now.Subtract(dt).Days < 7
                 End If

             End Function).ToArray())) & token).Trim()

File.WriteAllText("Output.txt", output)

Of course, you'll want to add error handling and other checks.

Here's full code to mock your input:

Public Sub TruncFile()
    ' mock the content
    Dim fileContent As String =
<text>'-----------Initiation----------
blah blur blah blur 7/1/2017 2:23:pm
blah blur blah blur 7/1/2017 2:23:pm
blah blur blah blur 7/1/2017 2:23:pm
blah blur blah blur 7/1/2017 2:23:pm
blah blur blah blur 7/1/2017 2:23:pm
blah blur blah blur 7/1/2017 2:23:pm
----------END----------

----------Initiation----------
blah blur blah blur 7/2/2017 2:23:pm
blah blur blah blur 7/2/2017 2:23:pm
blah blur blah blur 7/2/2017 2:23:pm
blah blur blah blur 7/2/2017 2:23:pm
blah blur blah blur 7/2/2017 2:23:pm
blah blur blah blur 7/2/2017 2:23:pm
----------END----------

----------Initiation----------
blah blur blah blur 7/3/2017 2:23:pm
blah blur blah blur 7/3/2017 2:23:pm
blah blur blah blur 7/3/2017 2:23:pm
blah blur blah blur 7/3/2017 2:23:pm
blah blur blah blur 7/3/2017 2:23:pm
blah blur blah blur 7/3/2017 2:23:pm
----------END----------

----------Initiation----------'
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
----------END----------

----------Initiation----------
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
----------END----------
'
<text>.Value
    Dim fileText = fileContent.Trim
    Dim token As String = "----------END----------" '- -vbNewLine & vbNewLine
    Dim output = (String.Join(token, (From block In Split(fileText, token).Where(
                 Function(b)
                     Dim blockLines() = Split(b.Trim, vbLf)
                     If blockLines.Length > 2 Then
                         Dim parts = Split(blockLines.Last(), " ")
                         Dim dt As Date = parts(parts.Length - 2)
                         Return Now.Subtract(dt).Days < 7
                     End If

                 End Function).ToArray())) & token).Trim()

    Console.WriteLine(output)

End Sub

Result:

----------Initiation----------'
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
blah blur blah blur 7/10/2017 2:23:pm
----------END----------

----------Initiation----------
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
blah blur blah blur 7/11/2017 2:23:pm
----------END----------

If you file is going to large, then you'll have to process lines one by one and keep a buffer and state to track where in the file you are and incrementally write your buffer to a new file and then clear it out.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41