Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Files in Directory -- How? 1

Status
Not open for further replies.

Rougy

Programmer
Oct 15, 2001
249
US
Hi,

I've got an entry page that shows a different image each time the page is accessed or refreshed.

I need to be able to count the number of files in the image directory.

I’d like to have the page count the number of files in the directory, so that whenever I add new files, I don’t have to update the randomize equation on the page.

The images are stored in this format:

Image01.jpg
Image02.jpg

Image100.jpg

I’ve got the randomize code working so that I can pick a random number and then use that number to choose the image, except I have to update the equation by hand every time I add new images.

Thanks,

Rougy


-----
The death of dogma is the birth of reason.
 
This will give you the count of files


Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\your folder name here")

NumFiles = Folder.Files.Count

How ever you may have to run through each file to make sure it's a jpg if there are going to be other file types, but if you only every put in jpg then this will work
 
sorry it should be
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
^^^^^^^
 
That's great.

It works when I type in the "C:\project\images..."

Is there a way to use a relative reference?

Like

Set Folder = FSO.GetFolder("..\images")

Thanks for your help.

-----
The death of dogma is the birth of reason.
 
you could try this

strImagePath = Server.MapPath("/images")
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(strImagePath)

Response.Write Folder.Files.Count
 
Server.MapPath(".") returns the path to the current script.

So if your script is in the images folder just use:
Set Folder = FSO.GetFolder(Server.MapPath("."))

If your script is one folder level up use:
Set Folder = FSO.GetFolder(Server.MapPath("images"))

etc...

BDC.
 
use the server.mappath() and put the name of the folder that has your images, I.E. \images will find the images folder in your web server root.
 
In the language of my youth,
that was
MEGA-cool.

Thanks!

-----
The death of dogma is the birth of reason.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top