Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Java Script Code for Directory File List

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 the FileSystemObject.

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();

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268