How do you read folder content using JavaScript?

How do I read the contents of a folder on the server into an array using JavaScript?
Ray TurnerSenior ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

black0psCommented:
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
ALNMOOCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

quincydudeCommented:
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
BadotzCommented:
>>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
quincydudeCommented:
>>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
BadotzCommented:
>>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
quincydudeCommented:
You are talking about server-side Javascript, we are talking about client-side Javascript. Two things are different.
0
Vel EousResearch & Development ManagerCommented:
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
BadotzCommented:
>>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
Ray TurnerSenior ConsultantAuthor Commented:
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
BadotzCommented:
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
BadotzCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray TurnerSenior ConsultantAuthor Commented:
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
BadotzCommented:
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
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.