Reading File in Backwards

S

Shahid Juma

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
 
S

Shahid Juma

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
 
S

Steven Burn

<%
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!
 
T

Tim Williams

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

Dave Anderson

Shahid said:
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/en-us/script56/html/fsooriScriptingRun-TimeReference.asp
Array.join()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthjoin.asp
Array.reverse()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreverse.asp
Array.slice()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthslicearray.asp
Array.split()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthsplit.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.
 
D

Dave Anderson

I said:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreverse.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/en-us/script56/html/fsooriScriptingRun-TimeReference.asp

Array.join()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthjoin.asp

Array.reverse()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreverse.asp

Array.slice()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthslicearray.asp

Array.split()
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthsplit.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top