Batch to delete text

Dell / OPTIPLEX GX620
April 22, 2009 at 02:01:15
Specs: Microsoft Windows XP Professional, 2.793 GHz / 3574 MB
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.


See More: Batch to delete text

Report •


#1
April 22, 2009 at 05:18:05
yes, definitely can be solved. show a sample of that log file, especially that date format.

Unix Win32 tools | Gawk for Windows


Report •

#2
April 22, 2009 at 06:16:00
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:44

The 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:47

The 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:14

The 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:00

The 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:00

The 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:04

The 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:24

The 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:32

The command completed successfully.

d:\l\test\var\pmcli\ifc\NEW\CCS\jobs\bjobo.200309.2232
1 file(s) moved.
.
**********FINISH**********


Many Thanks


Report •

#3
April 22, 2009 at 22:20:06
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 
	
Loop

on command line, type cscript /nologo myscript.vbs

Unix Win32 tools | Gawk for Windows


Report •

Related Solutions

#4
April 23, 2009 at 00:19:16
Wow, thanks ghostdog, I tried it on a test file and it does just what I wanted.
Many thanks for your help

Thank goodness I have found this fantastic forum I am learning such a lot from you all.


Report •


Ask Question