ASP random text script

I am in need of an ASP script that will select a different line of text from a plain text file each time a web page is loaded and insert that text into the web page.
fickenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xavCommented:
Try this:

<% @LANGUAGE="JScript" %>
<html><head>
<title></title>
<%
//If you know the path, use:
//cFileName="c:\\inetpub\\wwwroot\\testing\\quotes.txt"
//or, get the path from the current one
thisScriptName=new String(Request.ServerVariables("PATH_TRANSLATED"));
aDirs=thisScriptName.split("\\");
//Chop off the file name
scriptPath="";
for (i=0;i<aDirs.length-1;i++) {
      scriptPath+=aDirs[i]+"\\";
}
cFileName=scriptPath+"quotes.txt";

//this will read a file (quotes.txt) with one quote per line:
//For example,...
//Hello
//How are you
//What's new
fs = new ActiveXObject("Scripting.FileSystemObject");
readFile = fs.OpenTextFile(cFileName);
iTotalLines=0;
aQuotes=new Array();
//Create an array of quotes
while (!readFile.AtEndOfStream)      {
      curLine = readFile.ReadLine( );
      aQuotes[iTotalLines]=curLine
      iTotalLines++;
}
readFile.Close();
rndNum=Math.floor(Math.random()*iTotalLines)
Response.Write(aQuotes[rndNum]);
%>
</body>
</html>
0
MasseyMCommented:
Is this for random quotes?  If so, I have a database driven quote machine...

If you would like, I will email it to you...
0
fickenAuthor Commented:
Your quote machine sounds interesting, how easy and what is involved in implementation?

The first comment listed sounds a little bit questionable to me, I have reservations about including ActiveX controls on our site. I would prefer to stick with ASP/Javascript.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

xavCommented:
The line:

fs = new ActiveXObject("Scripting.FileSystemObject");

Just calls the MS FileSystem object on the server.

You can replace it with:

fs = Server.CreateObject("Scripting.FileSystemObject");

It's just a reference to a COM object--the same as if you open a database:

conn=Server.CreateOject("ADODB.Recordset")
0
MasseyMCommented:
Very easy really.  YOu have a database with quotes.  The ASp randomly selects one and it is then displayed in a table...

quotedsn.asp
-------------
<%
  ' This is the entire DB path - Physical with respect to root physical path
  sQuoteDB   = "Quotes.mdb"

  sQuoteScriptDir = "/Quotes/" ' Change for your directory...

  ' Set the virtual Directory
  sQuotePath = Server.MapPath(sQuoteScriptDir) & "\"

  ' This is the resulting DSN string
  sQuoteDSN = "filedsn=" & sQuotePath & "Quotes.Dsn;DefaultDir=" & sQuotePath & ";DBQ=" & sQuotePath & sQuoteDB & ";"
%>

quotes.dsn
-------------
[ODBC]
DRIVER=Microsoft Access Driver (*.mdb)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=8
MaxBufferSize=512
ImplicitCommitSync=Yes
FIL=MS Access
DriverId=25

quote.asp
-------------
<% Server.ScriptTimeOut = 15 %>
<!-- #include File="QuoteDSN.Asp" -->
<%
      Set QConn = Server.CreateObject("ADODB.Connection")
      QConn.Open sQuoteDSN
      quotesql="SELECT * FROM Quotes"
      Set QuoteRS = Server.CreateObject("ADODB.Recordset")
      QuoteRS.Open quotesql, QConn, 3, 3
      NumQuotes = QuoteRS.RecordCount
      QuoteRS.close
%>
<%
Randomize
RNum = Int((NumQuotes * Rnd) + 1)
%>
<%
      sql2="SELECT * FROM Quotes WHERE ID=" & RNum
      QuoteRS.Open sql2, QConn, 3, 3
%>
<% =QuoteRS("Quote") %>
<%
QuoteRS.close
QConn.close
set QuoteRS = nothing
set QConn = nothing
sDSN = ""
%>

quotes.mdb
------------
1 table:

Quotes [Table]
  ID     - Autonumber
  Quote  - memo
  Author - Text(255)
 
0
fickenAuthor Commented:
On second thought, rather than having two completely different scripts on the site to do similar things, I thought I would take a script that we have for placing a random image onto a page and modify it to place random snippets of text onto a page, however it does not seem to be working, perhaps someone can troubleshoot this for me...

Here is the original random image script that does work:

<!--Begin ASP Random Image script-->

<%
ThisFile = Request.ServerVariables("PATH_TRANSLATED")
LastSlash = InstrRev(ThisFile, "\")
ThisDir = Left(ThisFile,LastSlash)

strFileName = ThisDir + "images.asp"
Set FileObject = CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile(strFileName)


Dim Images(20)

i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  i = i + 1
Loop

MaxImages = i - 1
Randomize Second(time())
intRandomNumber = Int((MaxImages) * Rnd + 1)  

'cdr 9/2/99 close & release these objects!!!!!!

set FileObject = Nothing
set ReadStream = Nothing

%>


<!--End ASP Random Image script-->




and here is my modification which does not work, all i get is a page with nothing inbetween  the two comment tags:


<!--Begin ASP Random Text script-->

<%
ThisFile = Request.ServerVariables("PATH_TRANSLATED")
LastSlash = InstrRev(ThisFile, "\")
ThisDir = Left(ThisFile,LastSlash)

strFileName = ThisDir + "quotes.asp"
Set FileObject = CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile(strFileName)


Dim Text(16)

i = 1
Do While Not ReadStream.AtEndOfStream
  Text(i) = ReadStream.ReadLine
  i = i + 1
Loop

MaxText = i - 1
Randomize Second(time())
intRandomNumber = Int((MaxText) * Rnd + 1)  

'cdr 9/2/99 close & release these objects!!!!!!

set FileObject = Nothing
set ReadStream = Nothing

%>


<!--End ASP Random Text script-->
0
xavCommented:
I think all you need to do is write it out:

response.write(Text(intRandomNumber))

before the ASP end tag.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fickenAuthor Commented:
That did it, thanks for your help, sorry if i wasted your time.
0
xavCommented:
Not at all.  Glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.