{"id":9204,"date":"2021-12-01T09:04:31","date_gmt":"2021-12-01T09:04:31","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=9204"},"modified":"2021-12-01T09:04:53","modified_gmt":"2021-12-01T09:04:53","slug":"vbscript-to-obtain-the-last-n-lines-from-a-text-file","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/show\/vbscript-to-obtain-the-last-n-lines-from-a-text-file\/844.html","title":{"rendered":"Vbscript to Obtain the Last N Lines from a Text File"},"content":{"rendered":"<p>Tail.vbs borrows shamelessy from unix&#8217; tail command, and more or less performs the same function: to obtain the last N lines of a textfile (the tail).<\/p>\n<p><b>Usage 1<\/b>\u00a0(content is obtained from the filename given on the commandline):<br \/>\n<i>[cscript] TAIL[.vbs] path+filename startline [numberlines]\u00a0<\/i><br \/>\nwhere &#8216;startline&#8217; is counted from the end of the file, and numberlines is how many lines to obtain after that\u00a0 point, and defaults to &#8220;through end-of-file&#8221;<br \/>\nIf your filename happens to be a number, be sure to include a full or relative path in front of it so it will be interpreted as a filename. F\/e, if you have a file called 6, or 3.2, refer to it like: .\\3.2 or c:\\x\\6<\/p>\n<p><b>Usage 2<\/b>\u00a0(content is obtained through the pipe)<br \/>\n<i>&#8230; | CSCRIPT TAIL.VBS startline [numberlines]\u00a0<\/i><br \/>\nSame idea, but CSCRIPT must be used to launch the vbs, it will not run implicitly.<\/p>\n<p>testfile:<br \/>\n3 from end<br \/>\n2 from end<br \/>\n1 from end<\/p>\n<p>examples:<br \/>\ntail testfile 3 1 == 3 from end<br \/>\ntail testfile 3\u00a0\u00a0 == 3 from end [crlf]2 from end[crlf]1 from end<br \/>\ntail testfile 1\u00a0\u00a0 == 1 from end<br \/>\ntype testfile | cscript tail.vbs 3 1 == 3 from end<\/p>\n<p><i>&#8216;======== begin vbscript &#8220;tail&#8221;<br \/>\nset fso=createobject(&#8220;scripting.filesystemobject&#8221;)<br \/>\nargcnt=wscript.arguments.count<br \/>\nif argcnt&gt;0 then file=wscript.arguments(0)<br \/>\nif argcnt&gt;1 then tail=wscript.arguments(1)+1<br \/>\ntailend=0<br \/>\nselect case argcnt<br \/>\ncase 1<br \/>\n&#8216;one arg, only interp as line number, use stdin as file<br \/>\nif not isnumeric(file) then wscript.quit<br \/>\nb=&#8221;&#8221;<br \/>\non error resume next<br \/>\nb=wscript.stdin.readall<br \/>\nif b=&#8221;&#8221; then wscript.quit<br \/>\ntail=file+1<br \/>\ncase 2<br \/>\n&#8216;two args: file and line, or two lines and stdin for file<br \/>\nif isnumeric(file) then &#8216;two numbers<br \/>\nb=&#8221;&#8221;<br \/>\non error resume next<br \/>\nb=wscript.stdin.readall<br \/>\nif b=&#8221;&#8221; then wscript.quit<br \/>\ntail=file+1<br \/>\ntailend=tail-wscript.arguments(1)<br \/>\nelse<br \/>\nset a=fso.opentextfile(file,1) &#8216;non-number, use as file<br \/>\nb=a.readall<br \/>\nend if<br \/>\ncase 3<br \/>\n&#8216;easiest: file, line1, line2<br \/>\ntailend=tail-wscript.arguments(2)<br \/>\nset a=fso.opentextfile(file,1)<br \/>\nb=a.readall<br \/>\nend select<br \/>\np=-1<br \/>\nfor i=1 to tail<br \/>\nif i=tailend+1 then p2=p<br \/>\np=instrrev(b,vbcrlf,p)<br \/>\nif p=0 then<br \/>\np=1<br \/>\nexit for<br \/>\nend if<br \/>\nnext<br \/>\nif p2&gt;0 then c=mid(b,p,p2-p) else c=mid(b,p)<br \/>\nwscript.echo mid(c,3)<br \/>\n&#8216;======= end vbscript<\/i><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":2},"question-category":[55],"question_tags":[],"class_list":["post-9204","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/9204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=9204"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=9204"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=9204"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=9204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}