Java Script Code for Directory File List
By Stephen Bucaro
In this article You'll learn how to create a webpage that gives a list of the files in a folder.
This example uses "client-side" Java Script. Client side Java Script that lets the public view a
directory list of files on your server is considered to be dangerous. You would need to give the
webpage special security privileges to work. This example is for use with Internet Explorer Web
browser on your local computer. For your web site, you would use a server-side ASP file. In that
case the code would be very similar, but easier.
The first step for the code is to find out what folder it's in. With a webpage, that information
is contained in the document location object. We then pass that folder path to a FileSystemObject.
Unfortunately, the format of the local path returned by the location object is not compatible with
Path returned by the location object:
Equivalent path required by FileSystemObject:
To make the path compatible, first we use some string object methods to remove the "file:///"
from the beginning of the path, and to remove the "/filename.ext" from the end of the path.
The code for this is shown below.
var locPath = location.substring(location.indexOf("C"),location.lastIndexOf("/"));
Next we use escape characters and regular expressions to replace the forward-slashes
with back-slashes and replace incompatible local path characters with web path characters. The
code for this is shown below.
var thispath = locPath.replace(/\//g,"\\\\");
var thispath = thispath.replace(/%20/g," ");
There are certain characters in Internet URLs, like that returned by the location object,
that are incompatible with local file paths. For example Windows uses back-slashes in file
paths while the Internet uses forward-slashes in URLs. A blank space is acceptable in windows
file paths, but must be represented with the %20 character code in Internet URLs.
The first line of code above uses a regular expression in the string object's replace
method. Regular expressions is too complex of a subject to go into detail here, but the expression
/\//g means "all forward slashes". The second parameter passed to the replace method is a
string meaning two back-slashes. Since, in code, a back-slash indicates an escape character,
you need to escape each back-slash. (Even to me that sounds like double-talk, but take my word
for it. Escape characters is too complex of a subject to go into detail here.
The second line of code replaces any %20 space character codes in the Internet path with a
plain old space. Now we're prepared for business. Shown below is the code to create a
Scripting.FileSystemObject and pass it's GetFolder method the path we have created,
and then save the Folder's FileCollection.
var fs = new ActiveXObject("Scripting.FileSystemObject");
var Folder = fs.GetFolder(thispath);
var FileCollection = Folder.Files;
Next, we create an array and an Enumerator object, and use the Enumerator
object in a loop to step through the FileCollection, placing all the filenames (actually all
the files complete directory file paths) into the array elements. The code for this is shown below.
var Files = new Array();
for(var objEnum = new Enumerator(FileCollection); !objEnum.atEnd(); objEnum.moveNext())
strFileName = objEnum.item();