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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List folder contents as hyperlinks 1

Status
Not open for further replies.

Coogan

Programmer
Jun 21, 2004
38
GB
Does anyone know how I would get javascript to list the contents of a system folder as hyperlinks on a page??

This needs to be done without ASP as it is for a local intranet and it does not support ASP.

Any help or pointers would be greatly appreciated.

Cheers
 
This should do the job for you:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		var fsObj = null;
		var foldedToList = 'C:\\Windows\\System32';

		function listFiles() {
			try {
				fsObj = new ActiveXObject('Scripting.FileSystemObject');
			}
			catch (err) { }

			if (!fsObj) {
				alert('You must allow the ActiveX object to run!');
			} else {
				if (!fsObj.FolderExists(foldedToList)) {
					alert('The folder:\n\n' + foldedToList + '\n\ndoes not exist!');
				} else {

					// at this stage, we can list all files
					var folderObj = fsObj.GetFolder(foldedToList);
					var filesObj = new Enumerator(folderObj.files);
					filesStr = '';
					while (!filesObj.atEnd()) {
						var tempFile = filesObj.item();
						filesStr += '<a href="' + tempFile.Path + '">' + tempFile.Name + '</a><br>';
						filesObj.moveNext();
					}
					document.getElementById('fileList').innerHTML = filesStr;
				}
			}
		}
	//-->
	</script>
</head>
<body onload="listFiles();">
	<div id="fileList"></div>
</body>
</html>

Bear in mind that because it uses an ActiveX control, it will only work in IE, and you need to have your security restrictions set to allow ActiveX to run (or at least prompt the user to give them the choice).

It's rather basic, in that it doesn't take in attributes (hidden, system, directory, etc) into account... But you can tweak this now youy have a starting point, I'm sure (just use the MSDN FileSystemObject reference available here: )

Hope this helps,
Dan
 
Hi Dan,

With some small tweaks, maily with activeX on my system, I got this working!!

Many Many thanks.

My next challange would be to get this to look at subfolders as well. Any ideas??

Martin
 

>> My next challange would be to get this to look at subfolders as well. Any ideas??

*grin* How did I know that was coming? ;o)

You need to know whether you want all subfolder content listed alongside the main content (flat structure), or if you want to click a folder link, and then overwrite the current listing with that of the new folder.

Just out of interest - what is this for? Wouldn't it be easier just to use Explorer?

Dan
 
Sorry about my lameness... I'm new to this language and whilst I can understand the concepts behind it i'm just finding it difficult to start off with.

The project that I am working on is restricting computers as much as possible and that means that Explorer will be removed / restricted on systems and in order to find files or folders I am designing an intranet that will allow people to search for the files that they need and provide links to them.

I have managed a different system which lists files in an I-frame but due to restrictions with the I-frame you cannot find a file and trawling through to find the one you wnat can take, especially when there are 50000+ files!!

Idealy I would like a flat structure, I am also assuming that this would be the easiest!!

Again many thanks for your help.

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top