
Indexed Items w/ FileSystemObject in VBScript/ASP
The Files and SubFolders collections don't support numeric indices. You'll either have to load an
array of the names to index or enumerate the files with a for each..in loop and increment your own
counter until reaches the randomized value you want...
--
Michael Harris
MVP Scripting
While working on a site, I found the need to randomly pick a file name out
of a folder containing thousands of files.
I randomly generate a number from 1 to the file collection's count property,
and I'd like to access the file corresponding to the random number by index.
It looked easy enough, only the index values didn't work. I've tried tons
of different code options, IIS servers, and collections (i.e. Drives,
Folders, and Files). I've searched for hours on the Internet to explain why
the index values didn't work; to my surprise, I couldn't find this topic
mentioned anywhere but in one O'Reilly book, ASP in a Nutshell (A Desktop
Quick Reference) ?
Here is some sample code that I created to test my index problem:
<html>
<head>
<title></title>
</head>
<body bgcolor="#FFFFFF">
<%
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set fldr = objFSO.GetFolder("c:\")
Set fc = fldr.SubFolders
For Each f1 in fc
response.write f1.name & "<BR>"
Next
response.write "<br><br>->" & fc.count & "<br>"
response.write fc(1).name
response.write fc.Item(1).name
%>
</body>
</html>
and this is the output:
CFUSION
Documents and Settings
Inetpub
kraftwerk
Program Files
RECYCLER
System Volume Information
vbroker
WINNT
YODA
->10
Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument
/project/
rtext.asp, line 16
Neither fc(1).name nor fc.Item(1).name work. I've also tried setting
objFile = fc(1) or objFile = fc.Item(1) and then response.write
objFile.name, but that doesn't work either.. I've seriously tried
everything I could think of. As far as my project, I go through the entire
files collection and store the names in an array (with for each.in.next).
Then I access the name through the array. This way works, but my array
limits the number of file names I can access (I will be constantly updating
the files and I'll have TONS) and it also wastes memory.
After reading p.319, Table 18-3; p.320, Table 18-6; and p.323, Table 18-12
in the O'Reilly book, I've spent HOURS trying to get it to work and the book
only has short blips on these pages. I've tried writing to O'Reilly to ask
them how they did this, but I have not heard from them yet.
In case anyone wanted to see it, this is the code I'm forced to use:
<%
dim x(1000)
'The directory of all the pictures...
mypath="/project/pics"
Set filesystem = CreateObject("Scripting.FileSystemObject")
Set folder = filesystem.GetFolder(server.mappath(mypath))
Set filecollection = folder.Files
'the number of files....
total=filecollection.count
response.write "Total: " & total & "<br>"
'NONEFFICIENT WAY
idx=0
For Each file in filecollection
idx=idx+1
x(idx)=file.name
Next
%>
<table width="100%" border="2">
<% for count = 1 to 5%>
<tr width="100%">
<% for count2 = 1 to 5%>
<td>
<%
'Choose a random picture
randomize timer
whichNo=int(rnd()*total)+1
file_name = x(whichNO)
response.write "<a href=pics/" & file_name & ">Random Number: "
& whichNO & "<br>"
'Display the image!
response.write whichNO & ". "& file_name & "<br>"
response.write "<img src=" & mypath & "/thumbs/"
response.write file_name & " alt=" & file_name & "></a>"
%>
</td>
<% next %>
</tr>
<% next %>
<%
'Clean up...
set filesystem=nothing
set folder=nothing
set filecollection=nothing
%>
</table>
Thanks in advance to anyone that can help,
Scott Kreel
Scott Kreel
Peritus Group, Inc.
262.242.6530
www.peritusgroup.com
"Systems Design Group is now Peritus Group! We have officially changed our
name. Please see our new website for our press release. Don't forget to
update your address book!"