I produce a backup log (a text file) by DateStamp>. How can I create a text file containing only the last 20 lines from the backup log?
Creating a string containing the entire backup log is simple:
ReadFile>Backup.log,File
The challenge is how to extract the last 20 lines from the string varialbe.
Last 20 Lines of a Log
Moderators: JRL, Dorian (MJT support)
How about this:
Let>LogF=d:\test.log
Let>OutFile=d:\new.log
VBSTART
Function NumLines(Filename)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(Filename, 1)
f.ReadAll
NumLines = f.Line
End Function
VBEND
VBEval>NumLines("%LogF%"),Lines
Let>thisline=Lines-20
If>thisline>0
Repeat>thisline
ReadLn>LogF,thisline,theline
WriteLn>OutFile,r,theline
Let>thisline=thisline+1
Until>thisline,Lines
else
MessageModal>Not enough lines
endif
Let>LogF=d:\test.log
Let>OutFile=d:\new.log
VBSTART
Function NumLines(Filename)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(Filename, 1)
f.ReadAll
NumLines = f.Line
End Function
VBEND
VBEval>NumLines("%LogF%"),Lines
Let>thisline=Lines-20
If>thisline>0
Repeat>thisline
ReadLn>LogF,thisline,theline
WriteLn>OutFile,r,theline
Let>thisline=thisline+1
Until>thisline,Lines
else
MessageModal>Not enough lines
endif
MJT Net Support
[email protected]
[email protected]
Hi Support,
I'm grateful to you for personally writing a sample VBscript which I need desparately. Your enlightening script highlights that VBscript begins where Macro Scheduler stops.
The idea behind the last 20-line log originates from the needs to send me various logs remotely from customers' file serves using the SMTPSendMail> command periodically. As time elapses for weeks, the size of the logs becomes enormous. All I'm interested is the file server status during the last 24 hours.
Thanks a lot for your generous help.
I'm grateful to you for personally writing a sample VBscript which I need desparately. Your enlightening script highlights that VBscript begins where Macro Scheduler stops.
The idea behind the last 20-line log originates from the needs to send me various logs remotely from customers' file serves using the SMTPSendMail> command periodically. As time elapses for weeks, the size of the logs becomes enormous. All I'm interested is the file server status during the last 24 hours.
Thanks a lot for your generous help.
You could do the same thing in MacroScript using ReadFile by counting the number of CRLF characters to determine the number of lines. Or you could use ReadLn in a first parse loop which just counts the number of lines until you reach EOF, but this would be slower. I just thought the VBScript method was cleaner.
MJT Net Support
[email protected]
[email protected]
Tail a file very quickly
Based upon some more advanced techniques I found here in the forum, here is some sample code which should be lightning fast even on huge files. I post it here to help any users which found this thread by searching. There is of course the tail command on Linux - and also a windows port of this program... however this is an efficient implementation in Macro Scheduler and VBScript.
[code]
VBSTART
Function LineCount(sFilespec)
Const ForAppending = 8
with CreateObject("Scripting.FileSystemObject")
LineCount = .OpenTextFile(sFilespec, ForAppending).Line
end with
end Function
VBEND
Let>filename=c:\cm1.txt
VBEval>LineCount("%filename%"),last_line_num
Let>line_num=%last_line_num%-20
Let>Tail_doc=
Repeat>line_num
ReadLn>%filename%,line_num,line
//This is just for safety sake...
If>line=##EOF##,finish
ConCat>%Tail_doc%,%CRLF%
ConCat>%Tail_doc%,line
Add>line_num,1
Until>line_num=last_line_num
Label>finish
MessageModal>%Tail_doc%
[/code]
[code]
VBSTART
Function LineCount(sFilespec)
Const ForAppending = 8
with CreateObject("Scripting.FileSystemObject")
LineCount = .OpenTextFile(sFilespec, ForAppending).Line
end with
end Function
VBEND
Let>filename=c:\cm1.txt
VBEval>LineCount("%filename%"),last_line_num
Let>line_num=%last_line_num%-20
Let>Tail_doc=
Repeat>line_num
ReadLn>%filename%,line_num,line
//This is just for safety sake...
If>line=##EOF##,finish
ConCat>%Tail_doc%,%CRLF%
ConCat>%Tail_doc%,line
Add>line_num,1
Until>line_num=last_line_num
Label>finish
MessageModal>%Tail_doc%
[/code]
Thanks for that adroege.
For anyone counting lines in truly huge files where speed is critical, the following two posts may be helpful:
Acquire line count of text file
http://www.mjtnet.com/forum/viewtopic.php?t=3069
[Bounty Won] Text Blob Line Counter Speed Challenge
http://www.mjtnet.com/forum/viewtopic.php?t=5408
Take care
For anyone counting lines in truly huge files where speed is critical, the following two posts may be helpful:
Acquire line count of text file
http://www.mjtnet.com/forum/viewtopic.php?t=3069
[Bounty Won] Text Blob Line Counter Speed Challenge
http://www.mjtnet.com/forum/viewtopic.php?t=5408
Take care
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
