For Programmers: Free Programming Magazines  


Home > Archive > ASP > December 2004 > Reading File in Backwards









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading File in Backwards
Shahid Juma

2004-12-23, 3:55 pm

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


Curt_C [MVP]

2004-12-23, 3:55 pm

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...
> 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
>
>



Shahid Juma

2004-12-23, 3:55 pm

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...
> 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...
>
>



Tim Williams

2004-12-23, 3:55 pm

How 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
>
>



Steven Burn

2004-12-23, 3:55 pm

<%
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...
>
>



Curt_C [MVP]

2004-12-23, 8:55 pm

Is 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...
>
>



Dave Anderson

2004-12-23, 8:55 pm

Shahid 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...meReference.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...hslicearray.asp
Array.split()
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.


Dave Anderson

2004-12-23, 8:55 pm

I wrote:
> FileSystemObject
>

http://msdn.microsoft.com/library/e...e.aspArray.join()
>

http://msdn.microsoft.com/library/e...spArray.reverse()
>

http://msdn.microsoft.com/library/e....aspArray.slice()...

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...meReference.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...hslicearray.asp

Array.split()
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.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com