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.