how to articles

"Let The Computer Do The Work" series

(note: this article is a work in progress and was last updated 26-May-2005 . check back soon for the latest updates.)

Building Content using Folders and the Files In Them
using ASP FileSystem Object
(PHP coming soon)

STEP 2 Get Folders and Files

2. Add code to your Web pages to access the Folders and Files on the fly.

Our goal in this step is to create simple code in a language available on most Web servers to generate up-to-date lists of all the Files within a Folder on your Web site. In a few lines of code, all provided below, you can find all the files in any folder and, in the next step, build links to them. The well-chosen Folder and File names from Step 1 help simplify the coding needed to access the Files.

Our example will be in the language called Active Server Pages (ASP), a Microsoft technology available on Web sites hosted on Windows servers and also ported to the Linux platform. (PHP will be added at a later date.) This code can be inserted anywhere in your HTML prior to the point where you are displaying any of this information.

(NOTE: For ASP pages to work, your Web site needs to be hosted on a Windows server running IIS, or a Linux server with ASP software. Refer to your Web hosting service for this information. Also, the Web page will have to be saved with a .asp file extension, rather than .htm. In Dreamweaver MX or MX 2004, creating a new Web page by selecting the Dynamic page with ASP VBscript.)

ASP Code to Read the Folder and Files

<%
Set imgFSO = Server.CreateObject("Scripting.FileSystemObject")
thepath = Server.MapPath(Request.ServerVariables("PATH_INFO"))
thepath = LEFT(thepath, (LEN(thepath) - LEN(Request.ServerVariables("SCRIPT_NAME"))))
Set imgFolder = imgFSO.GetFolder(thepath & "\carimages")
%>

The code above serves to locate the images Folder where our Files are stashed. The only change you might need to make is the Folder name ("\carimages") at the end of the line beginning with "Set imgFolder."

Next we need to retrieve the names of all the Files in the Folder:

<%
Dim ImgArray()
ReDim ImgArray(500)
img = 0
For each thing in imgFolder.Files
ImgArray(img) = thing.name
img = img + 1
next
ReDim Preserve ImgArray(img - 1)
imgtop = img - 1
%>

The code above retrieves all the names of the Files in the Folder images and stores them in an array, so that we have them all available for our use. By default, the names are in alphabetical order.

So far, we don't have anything displaying on our Web page, But we have prepared for that by finding the images Folder and create a list of all the Files in that folder, storing them in an array.

and the next step is Step 3...