Welcome to Geek Times!
spacer
RandomFile (ASP)
Find on this site:


home
search
archive
about

.
A client likes having the date on their home page. Here's the ASP code we used. I coded a much more general version in Borscht/Java, also on this site. A string of the form "Thursday 18 June 1998" is retrieved in the body of the ASP page by saying, for example,
<% =funfact %>
<%
''------------------------------------------------------------------
'                         * * * FunFacts * * *
'------------------------------------------------------------------
randomize           ' seed the rnd function
dim fs
set fs = CreateObject("Scripting.FileSystemObject")

dim folder, files
const funPath = "/factoids/funfacts/"
const prefix = "item_ID_"   ' prefix for qualifying filenames
set folder = fs.GetFolder(Server.MapPath(funPath))
set files = folder.Files

dim i, names()
i=0               ' no qualifying files at start
funFactsCount = files.count   ' we'll examine all the files
reDim names(funFactsCount)    ' wastefully, room for all

dim funFactFiles
for each file in files      ' consider each of the files
  if ( strcomp(left(file.name, len(prefix)), prefix, vbTextCompare) = 0 ) then
    names(i) = file.name    ' if match, remember filename
    i = i + 1         ' and up funFactsCount of qualifiers
  funFactFiles = funFactFiles & " " & file.name & "
" end if next funFactsWinner = names(Rnd * (i-1)) ' pick a qualifier at random set handle = fs.OpenTextFile(Server.MapPath( funPath & funFactsWinner),1,FALSE,TristateUseDefault) funfact = handle.ReadAll ' get the funFactsWinner's contents handle.Close ' clean up %>
This page is copyrighted 1993-2006 by Michael 'Mickey' Sattler, some rights reserved via the Creative Commons License. Questions and comments? Send email to the Geek Times Webmaster. (Domain and web content hosting at 1and1.)
email