Collections in ASP: Names vs. Numbers

N

Nathan Sokalski

I am using the Folder.Files collection to display all the images in a given
directory. The following shows the code I use to create and use this
collection:

Set myfilesys=Server.CreateObject("Scripting.FileSystemObject")
Set mydirectory=myfilesys.GetFolder(Server.MapPath("./emoticons"))

When I use a specific file name to access a file, my code works fine:

<img src=" & Chr(34) & mydirectory.Name & "/" &
mydirectory.Files("blinkgirl.gif").Name & Chr(34) & ">

However, I want to iterate through all the files in the Folder USING NUMBERS
(I already know how to do it using a For Each statement). But whenever I try
to do this, I receive an error:

<img src=" & Chr(34) & mydirectory.Name & "/" & mydirectory.Files(5).Name
& Chr(34) & ">

Other collections, such as the Request.Form collection allow you to access
the collection using either text or numbers, and all books that I have read
have said that this is true for all collections. Is the Folder.Files
collection an exception to this? Is there something else I need to do? If
anybody can give me any help on this, I would appreciate it. Thank You.
 
R

Ray at

The files collection doesn't support referencing an item by its index. Why?
There's probably a reason. But you can only reference it by its key, which
is the file name. Mind if I ask why you want to do this?

Ray at home
 
N

Nathan Sokalski

Thank you for that information. The reason I would like to use indexes
instead of keys is to make the files (which are graphics) into a table which
will automatically adjust the number of rows when more files are added to
the directory. Although I do know how to do this using a For Each loop, it
would be much simpler and more efficient if I could use indexes. But I guess
I just have to look at this as another good challenge and learning
experience. Thanks.
 
C

Chris Barber

Populate an array and use the array indexes instead?

Chris.

Thank you for that information. The reason I would like to use indexes
instead of keys is to make the files (which are graphics) into a table which
will automatically adjust the number of rows when more files are added to
the directory. Although I do know how to do this using a For Each loop, it
would be much simpler and more efficient if I could use indexes. But I guess
I just have to look at this as another good challenge and learning
experience. Thanks.
 
M

Michael D. Ober

The FileSystemObject Files Collection has a .Count property that you can
use.

Mike Ober.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top