Getting random line from text file

Results 1 to 2 of 2

Thread: Getting random line from text file

  1. #1
    Join Date
    Dec 1969

    Default Getting random line from text file

    I am trying to write a script to display a random line of text from a text file. Is there a way to count the number of lines in a file, and then read a given line by line number?<BR><BR>If I can&#039t get the number of lines what is the best way to do it?<BR><BR>Help or hints please.

  2. #2
    Jim Andrews Guest

    Default RE: Getting random line from text file

    Well, I don&#039t know that this is the BEST way, but it is certainly one way:<BR>&#060;%<BR>Set MyFileObj = Server.CreateObject("Scripting.FileSystemObject")< BR>Set MyTextFile = MyFileObj.OpenTextFile("c:files est.txt")FileContents = MyTextFile.ReadAll<BR>FileContents = Split(FileContents, vbCrLf)<BR><BR>TotalLines = UBound(FileContents)<BR>Randomize<BR>rndLine = Int((TotalLines) * Rnd)<BR>Response.Write(FileContents(rndLine))<BR>% &#062;<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts