Solved vb script to copy last 10 lines from text file

July 11, 2014 at 04:23:25
Specs: Windows 7
Hi, I am looking for script which will read last 10 lines from existing text file and copy that lines in new text file.

message edited by villy


See More: vb script to copy last 10 lines from text file

Report •


#1
July 11, 2014 at 07:18:57
✔ Best Answer
I dunno vbs, but here's a plain vanilla bat:

::====== script starts here ===============
:: get last 10 lines
:: last10.bat 2014-07-11 21:06:13.62
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< myfile') do (
set LAST=%%a
)

set/a FIRST=LAST-9

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< myfile') do (
if %%a geq !FIRST! echo.%%b
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
July 11, 2014 at 11:41:55
Here's a "how-to", but it's kind of overkill for this situation:
http://www.computing.net/howtos/sho...

but here's a "stripped down" version:
'tail10.vbs
set fso=createobject("scripting.filesystemobject")
infile="c:\work\test"
file2="c:\work\test2"
delim=VBCRLFf 'default delimiter, may be changed to VBCR, VBLF, or etc.
'file access: 1=read, 2=write, 8=append
set a=fso.opentextfile(infile,1)
x=a.readall
test=instrrev(x,vbcrlf,10)
if test>0 then x=mid(x,test)
z=fso.opentextfile(file2,8)
'wscript.echo x
z.write x
z.close


Report •

#3
July 15, 2014 at 18:35:14
Thanks "Mechanix2Go" your script worked as expected. thanks.

Report •

Related Solutions

#4
July 15, 2014 at 18:36:26
Thanks for your reply "nbrane".

here is the script modified as requirement but failed to execute.

set fso=createobject("scripting.filesystemobject")
infile="c:\tmp\4.txt"
file2="c:\tmp\test.txt"
delim=VBCRLFf 'default delimiter, may be changed to VBCR, VBLF, or etc.
'file access: 1=read, 2=write, 8=append
set a=fso.opentextfile(infile,1)
x=a.readall
4.txt=instrrev(x,vbcrlf,10)
if 4.txt>0 then x=mid(x,4.txt)
z=fso.opentextfile(file2,8)
'wscript.echo x
z.write x
z.close


execution error.
"c:\tmp\tail10.vbs(8, 1) Microsoft VBScript compilation error: Expected statement"


Report •


Ask Question