?
Solved

How do you read folder content using JavaScript?

Posted on 2008-11-17
16
Medium Priority
?
4,147 Views
Last Modified: 2012-06-27
How do I read the contents of a folder on the server into an array using JavaScript?
0
Comment
Question by:Ray Turner
  • 6
  • 3
  • 3
  • +3
16 Comments
 
LVL 7

Expert Comment

by:black0ps
ID: 22982272
0
 
LVL 7

Expert Comment

by:black0ps
ID: 22982275
But that's on the client side. Javascript doesn't work on the server side. You need a server side scripting language to do that, php, asp, cfm, etc.
0
 
LVL 7

Expert Comment

by:ALNMOO
ID: 22982345
http://javascript.internet.com/miscellaneous/computer-drive-browser.html

but as black0ps said, this will brows the client folder not server folder. if you can tel us why u need this we may help you more.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:quincydude
ID: 22982369
Javascript is a client side ONLY scripting language with is only active on client browser. It's impossible to do what you want.
And even though it's at client side, due to security issues, javascript also cannot read files on client's local file system.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 22982872
>>Javascript is a client side ONLY scripting language

Utter nonsense. I use js on both the server and the client all the time, no problem.

You can read the folder content with the FileSystemObject and pass the (string) back to the client via Ajax.
0
 
LVL 15

Expert Comment

by:quincydude
ID: 22983038
>>I use js on both the server and the client all the time, no problem.

How? If you are talking about AJAX it's not pure javascript, just trigger event from client to server side. You should clear up your logic.

And the FileSystemObject is only supported by IE as using it is a bad practice and no other browsers support it.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 22983074
>>How?

I have been using js on the server since about 1996.


>>You should clear up your logic.

I do not know what you mean.


>>And the FileSystemObject is only supported by IE

If the FSO is an IE-only thing, then I guess none of my web apps should be working. Maybe it is an IIS thing?


>>...using it is a bad practice

I do not agree.
0
 
LVL 15

Expert Comment

by:quincydude
ID: 22983177
You are talking about server-side Javascript, we are talking about client-side Javascript. Two things are different.
0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 22983208
Try the following:

<script language="javascript">

var my_js_array = new Array();


if ( $dir = opendir ( '.' ) )
{
while ( $file = readdir ( $dir ) )
{
if ( $file != '.' && $file != '..' )
{
$my_php_array[] = $file;
}
}
}

closedir ( $dir );

sort ( $my_php_rray );

foreach ( $my_php_array as $element )
{
echo ( "my_js_array.push( '$element' );" );      
}

?>

</script>
0
 
LVL 29

Expert Comment

by:Badotz
ID: 22983241
>>You are talking about server-side Javascript, we are talking about client-side Javascript. Two things are different.

They are only different in your mind. javascript on the server is exactly the same as javascript on the client, only more so ;-)

All of my .ASP pages are written in javascript; I have never had a problem with them running on different browsers (because they are server-based, of course).

So, if you are using IIS as your server, the FSO is perfectly valid, and there is *NO* stigma attached to it.

0
 

Author Comment

by:Ray Turner
ID: 22987286
Maybe I should clarify my request.

The website has the following folder structure:

Parent_Folder
        |_ Folder_B

I have files sitting in Folder_B.  I just want to fill an array with those file names.  I have done this using ASP.NET.  Is this possible using JavaScript?

Thanks, All
         
0
 
LVL 29

Expert Comment

by:Badotz
ID: 22987406
You cannot do that on the client, but you can on the server.

You can make an Ajax request for the values, and the server can get the file names and send them up to the client.

It is easier to pass a delimited string to the client, and upon receiving it, split it into an array.

And yes, you can do it all in javascript, but in two separate places.
0
 
LVL 29

Accepted Solution

by:
Badotz earned 2000 total points
ID: 22987555
Here are some js functions to read files and folders. Put them in an .ASP file and your halfway there.


var fso = new ActiveXObject('Scripting.FileSystemObject');
// --------------------------------
 
function list_files(path) {
	var stack = [];
	
	var folder = fso.getFolder(path);
	
	for (var enu = new Enumerator(folder.Files); !enu.atEnd(); enu.moveNext()) {
		try { stack.push(enu.item()); }
		catch(e){}
	}
	if (stack.length > 0) { return stack.join('\n'); }
	
	return [];
}
// --------------------------------
 
function list_folder(path) {
	var files = [];
	var stack = [];
	
	path = ((path == 'undefined' || path == '') ? '.' : path);
	
	try {
		files.push(fso.getFolder(path));
		stack.push(fso.getFolder(path));
		
		while (stack.length > 0) {
			var folder = stack.pop();
			
			for (var enu = new Enumerator(folder.SubFolders); !enu.atEnd(); enu.moveNext()) {
				try { stack.push(enu.item()); }
				catch(e){}
			}
			
			for (enu = new Enumerator(folder.Files) ; !enu.atEnd(); enu.moveNext()) {
				try { files.push(enu.item().Path); }
				catch(e) { }
			}
		}
	}
	catch(e) {}
	finally {
		if (files.length > 0) { return files.join('\n'); }
		
		return [];
	}
}
// --------------------------------
 
function list_folders(path) {
	var found = [];
	var stack = [];
	
	path = ((path == 'undefined' || path == '') ? '.' : path);
	
	try {
		var folder = fso.getFolder(path);
		
		found.push(folder);
		stack.push(folder);
		
		while (stack.length > 0) {
			var folder = stack.pop();
			
			for (var enu = new Enumerator(folder.SubFolders); !enu.atEnd(); enu.moveNext()) {
				try {
					stack.push(enu.item());
					found.push(enu.item());
				}
				catch(e){}
			}
		}
	}
	catch(e) {}
	finally {
		if (found.length > 0) { return found.join('\n'); }
		
		return [];
	}
}

Open in new window

0
 

Author Comment

by:Ray Turner
ID: 22988522
That's great.  Is there a way to do this without forcing the user to install an ActiveX Control?  Some users may have a restriction that will not allow them to install an ActiveX Control.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 22988604
Since it is running ON THE SERVER, as long as your server is running IIS, you need not install anything. You may have to reference the FSO from within your IDE, but that should be it.

The above javascript is SERVER-SIDE CODE, not client-side code.
0
 

Author Closing Comment

by:Ray Turner
ID: 31517741
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question