Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I have a text file which I would like to read from the end and display only a certain number of records. Is there any way of doing this? Thanks, Shahid
Post Follow-up to this messageInStrRev() -- Curt Christianson Owner/Lead Developer, DF-Software Site: http://www.Darkfalz.com Blog: http://blog.Darkfalz.com "Shahid Juma" <shahid319REMOVETHIS@hotmail.com> wrote in message news:ufsrvBS6EHA.1120@TK2MSFTNGP11.phx.gbl... > Hi, > > I have a text file which I would like to read from the end and display > only > a certain number of records. Is there any way of doing this? > > Thanks, > Shahid > >
Post Follow-up to this messageDoesn't this function just return the last occurence? I am not doing a search, what I want to do is read the last entry in the file and backwards towards the top.... Shahid "Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message news:uwvt2CS6EHA.2552@TK2MSFTNGP09.phx.gbl... > InStrRev() > > > -- > Curt Christianson > Owner/Lead Developer, DF-Software > Site: http://www.Darkfalz.com > Blog: http://blog.Darkfalz.com > > > "Shahid Juma" <shahid319REMOVETHIS@hotmail.com> wrote in message > news:ufsrvBS6EHA.1120@TK2MSFTNGP11.phx.gbl... > >
Post Follow-up to this messageHow large is the file? If not too big then you might consider reading in the entire file, doing a split on newline and then taking the last entries in the resulting array. Tim. "Shahid Juma" <shahid319REMOVETHIS@hotmail.com> wrote in message news:ufsrvBS6EHA.1120@TK2MSFTNGP11.phx.gbl... > Hi, > > I have a text file which I would like to read from the end and display only > a certain number of records. Is there any way of doing this? > > Thanks, > Shahid > >
Post Follow-up to this message<%
Const fRead = 1, fWrite = 2, fAppend = 8
Dim objFSO, objIStream
Dim strLine, strPrintLine, strFile
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
strFile = Server.MapPath("thefile.txt")
Set objIStream = objFSO.OpenTextFDile(strFile, fRead, False)
Do While NOT objIStream.AtEndOfStream
strLine = objIStream.ReadLine
strPrintLine = strLine + "<br/>" + strPrintLine
iCounter = iCounter + 1
Loop
objIStream.Close
Set objIStream = Nothing
Set objFSO = Nothing
Response.Write strPrintLine
%>
--
Regards
Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk
Keeping it FREE!
"Shahid Juma" <shahid319REMOVETHIS@hotmail.com> wrote in message
news:OZjPfIS6EHA.3700@tk2msftngp13.phx.gbl...
> Doesn't this function just return the last occurence? I am not doing a
> search, what I want to do is read the last entry in the file and backwards
> towards the top....
>
> Shahid
>
> "Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
> news:uwvt2CS6EHA.2552@TK2MSFTNGP09.phx.gbl...
>
>
Post Follow-up to this messageIs there a seperator in the file? space, comma, etc? If so just use Split and loop backwards through the array -- Curt Christianson Owner/Lead Developer, DF-Software Site: http://www.Darkfalz.com Blog: http://blog.Darkfalz.com "Shahid Juma" <shahid319REMOVETHIS@hotmail.com> wrote in message news:OZjPfIS6EHA.3700@tk2msftngp13.phx.gbl... > Doesn't this function just return the last occurence? I am not doing a > search, what I want to do is read the last entry in the file and backwards > towards the top.... > > Shahid > > "Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message > news:uwvt2CS6EHA.2552@TK2MSFTNGP09.phx.gbl... > >
Post Follow-up to this messageShahid Juma wrote:
> I have a text file which I would like to read from the end and
> display only a certain number of records. Is there any way of doing
> this?
I assume you don't want to reverse the contents of each line, but rather the
order of the lines:
var fso = Server.CreateObject("Scripting.FileSystemObject"),
src = fso.OpenTextFile("YourSourceFile.txt").ReadAll(),
rows = src.split("\r\n").reverse()
At this point, you can either write the text file back:
fso.CreateTextFile("Output.txt",true).Write(rows.join("\r\n"))
Or display it online:
<TABLE><TR><TD><%=rows.join("</TD><TR><TR><TD>")%></TD></TR></TABLE>
To get the last 10 lines only, modify the [rows] assignment:
rows = src.split("\r\n").slice(-10).reverse()
FileSystemObject
http://msdn.microsoft.com/library/e...br />
ence.asp
Array.join()
http://msdn.microsoft.com/library/e...56jsmthjoin.asp
Array.reverse()
http://msdn.microsoft.com/library/e...smthreverse.asp
Array.slice()
http://msdn.microsoft.com/library/e...6jsmthsplit.asp
--
Dave Anderson
Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Post Follow-up to this messageI wrote: > FileSystemObject > http://msdn.microsoft.com/library/e...ay.j oin() > http://msdn.microsoft.com/library/e...spArray.reverse()[colo r=darkred] >[/color] http://msdn.microsoft.com/library/e...y.sl ice()... For some reason, OE-QuoteFix did the above (though it looks fine in the source). It should look like this: FileSystemObject http://msdn.microsoft.com/library/e...br /> ence.asp Array.join() http://msdn.microsoft.com/library/e...56jsmthjoin.asp Array.reverse() http://msdn.microsoft.com/library/e...smthreverse.asp Array.slice() http://msdn.microsoft.com/library/e...6jsmthsplit.asp -- Dave Anderson Unsolicited commercial email will be read at a cost of $500 per message. Use of this email address implies consent to these terms. Please do not contact me directly or ask me to contact you directly for assistance. If your question is worth asking, it's worth posting.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.