[thelist] Counting files in a folder using JavaScript

Ben Gustafson ben_gustafson at lionbridge.com
Tue Dec 11 10:02:45 CST 2001


> I believe the man said JavaScript. If my memory serves me 
> correctly, don't 
> security restrictions prohibit javascript from accessing files?
> 
> --Burhan Khalid
> 
>  > Hey all,
>  >
>  > Is there a quick and easy way to count files in a folder and
>  > assign the
>  > number to a var in JavaScript?
> 

That's true for unsigned client-side JavaScript (noting Raymond's reply regarding signed
JavaScript). However, in my model below it's not the client-side JS that's accessing the
server-side info; it's being passed the info accessed (securely) by the server-side
script. In general, you're going to need a server technology to work with your client-side
JavaScript to pass it such server-side info. If you have no server technology to work
with, I guess signed JavaScript is the way to go.

--Ben

> Dave,
> 
> Here's a quick and easy way to use the FileSystemObject in 
> ASP to count the 
> number of
> files in a folder and pass this server-side info to 
> client-side JavaScript:
> 
> --
> <%
> 
> var filesCount = 0;
> var strPath = "c:\\path\\to\\your\\folder";
> countFiles(strPath);
> 
> function countFiles(strPath)
> {
> 	var objFSO = Server.CreateObject("Scripting.FileSystemObject");
> 	var objFolder = objFSO.GetFolder(strPath);
> 	var objFiles = objFolder.Files;
> 	filesCount = objFiles.Count;
> 	objFolder = null;
> 	objFiles = null;
> 	objFSO = null;
> }
> 
> %>
> 
>   <SCRIPT LANGUAGE="JavaScript1.2"><!--
> 	var numFiles = "<%= filesCount %>";
> //--></SCRIPT>
> --
> 
> I imagine most any other server technology can do a similar thing.




More information about the thelist mailing list