Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to copy last 100 lines of log file to new file...

Status
Not open for further replies.

CyberReal

ISP
Oct 27, 2003
2
US
Started a new job in an environment much bigger than before, so I need scripting to help.

I have Win2000 server running Oracle, which creates a log file that the DBA would like to read once in a while. We don't want to open the log file for fear that it would then be inaccessible by the app. So, scripting a few commands to copy the log file to a new file came to mind. However, we only want the last 100 lines of the file (like Unix tail -f 100, for example). How can we use the copy script but only grab the last one hundred lines of the current log file? And, I want the file name to be defined using the NOW command instead of replacing the newly created file each time.

Oh yeah, happy Monday to all.
 
Try something like this:
Code:
Const ForReading=1,ForWriting=2
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile("\path\to\logfile.txt",ForReading)
myArr=Split(f.ReadAll,vbCrLf)
f.Close
myOut="\path\to\" & Left(Replace(Now,"/","-"),10) & ".txt"
Set f=fso.OpenTextFile(myOut,ForWriting,True)
s=UBound(myArr)-100: If s<0 Then s=0
For i=s To UBound(myArr)
  f.WriteLine myArr(i)
Next
f.Close

Hope This Help
PH.
 
I'm sorry to come in the middle, but I would like to know if I can do the same thing using the above script,
instead of the last 100 lines, I would like to grab only the lines that are from line 50 to line 100, and line 300 to line 500.
how would I do that?
 
Try something like this:
Code:
Const ForReading=1,ForWriting=2
Set fso=CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f=fso.OpenTextFile(&quot;\path\to\input.txt&quot;,ForReading)
myArr=Split(f.ReadAll,vbCrLf)
f.Close
s=UBound(myArr)
If s<500 Then WScript.Echo &quot;Too small&quot;: WScript.Quit
Set f=fso.OpenTextFile(&quot;\path\to\out.txt&quot;,ForWriting,True)
For i=49 To 99
  f.WriteLine myArr(i)
Next
For i=299 To 499
  f.WriteLine myArr(i)
Next
f.Close

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top