Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is it possible to write a batch file that will read every line of a text file and when it finds a date that is older than one year, will delete all lines above that date.
The reason I am asking is that I have a lot of .log files and I only need to keep anything that has happened in the past year so I thought batch could help me with the housekeeping.

Hi ghostdog, thanks for your interest, below is a simple example of the content of one of the .log files. Other files are more complicated (and contain sensitive information) but the date format remains the same:
.
**********START**********
.
Current time at \\SCDCDC3 is 18/03/2009 11:44The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo.180309.1143
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 18/03/2009 11:47The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo18114706.180309.1147
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 18/03/2009 12:14The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo18121332.180309.1213
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 19/03/2009 08:00The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo.190309.0800
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 20/03/2009 08:00The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo.200309.0800
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 20/03/2009 15:04The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo20150405.200309.1504
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 20/03/2009 15:24The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo20152455.200309.1524
1 file(s) moved.
.
**********FINISH**********
.
.
**********START**********
.
Current time at \\SCDCDC3 is 20/03/2009 22:32The command completed successfully.
d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo.200309.2232
1 file(s) moved.
.
**********FINISH**********
Many Thanks

here's a vbscript. (not the full solution).
Set objFS = CreateObject("Scripting.FileSystemObject") strFile = "c:\test\file.txt" flag=0 Set objFile = objFS.OpenTextFile(strFile,1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine,"FINISH") >0 And flag=1 Then flag=0 End If If InStr(strLine,"START") >0 Then flag=1 End If If flag=1 And ( InStr(strLine,"Current time") > 0) Then ArrDate = Split(strLine) strDate=ArrDate(UBound(ArrDate) -1) strTime = ArrDate(UBound(ArrDate)) If DateDiff("d",strDate,Now ) > 365 Then flag=0 Else WScript.Echo strLine End If ElseIf flag=1 then WScript.Echo strLine End If Loopon command line, type cscript /nologo myscript.vbs

Wow, thanks ghostdog, I tried it on a test file and it does just what I wanted.
Many thanks for your helpThank goodness I have found this fantastic forum I am learning such a lot from you all.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |