Find current folder

The following is some jscript to get a list of .js files located in the same folder as the .thml was started from.

var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(".");  

var files = new Enumerator(folder.files);
var fileListshow = [];
var fileListcall = [];

while(!files.atEnd()) {
      // add file to array
      if ( files.item().name.lastIndexOf ( ".js" ) == ( files.item().name.length - 3 ) )
      {
      fileListshow.push(files.item().name);
      fileListcall.push(files.item().name);
      }
      files.moveNext();
}


When I was testing it on the desktop it worked fine.  Place the .html and other .js files in the same folder eslewhere and it cant find them.  Why?
MillkindAsked:
Who is Participating?
 
Kiran Paul VJConnect With a Mentor Computer EngineerCommented:
this will show the all .js files in your current folder

<script language="JavaScript" type="text/javascript">
function GetFolders()
{
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fullpath, curdir, thisfilename;

  fullpath = location.href; // fully qualified filepath
  // of the form "file:///c:/Documents%20and%20Settings/Administrator/
  // My%20Documents/htmlpreview.html"

  // regexp weirdo
  fullpath = fullpath.replace(/file:\/\/\//,""); // remove file:///
  fullpath = fullpath.replace(/\//g,"\\"); // replace slash with backslash
                                           // (for use on Windows OS)
  fullpath = fullpath.replace(/%20/g," "); // replace escaped space

  // split path and filename
  var iLastBackslash = fullpath.lastIndexOf("\\");

  // = htmlpreview.html
  var thisfilename = fullpath.substring( iLastBackslash+1, fullpath.length);
  // remove filename from full path, we are just interested in the path
  var curdir = fullpath.substring( 0, iLastBackslash );
 
      var folder = fso.GetFolder(curdir);  

      //alert(curdir);
      
      var files = new Enumerator(folder.files);
      var fileListshow = [];
      var fileListcall = [];

       while(!files.atEnd())
       {
              // add file to array
              if ( files.item().name.lastIndexOf ( ".js" ) == ( files.item().name.length - 3 ) )
              {
              fileListshow.push(files.item().name);
              fileListcall.push(files.item().name);
              }
              files.moveNext();
      }
      for(i=0;i<fileListshow.length;i++)
      {
            alert(fileListshow[i]);
      }
}
</script>


got partial code from here http://www.codeproject.com/html/htmlpreview.asp

but somehow the code doesnt work when used in a webserver.

hope this helps
kiranvj
0
 
netsmithcentralCommented:
What other folder?  Is the browser reporting an error?  Please note that if this "other folder" is on a website that you're accessing through a client browser, the posted code WILL NOT work.  It would be a security issue to let a website access a user's file system.

If you need to poll the server for this information (eg, you want to load all the files in the directory on your webserver) when a client is accessing the site you'll need to use server-side programming (eg ASP, PHP, etc)
0
 
MillkindAuthor Commented:
This piece isn't going to be a true web program.  Im just using js to create a flash card viewer.  They will select the .js file they want to load.  I want them to all be in the same folder as the .html.  keep things simple for user.  All the files used will be on the machine the js is launched from.
0
 
Kiran Paul VJComputer EngineerCommented:
try giving full path like

var folder = fso.GetFolder("C://somewhere//somepath");
0
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.

All Courses

From novice to tech pro — start learning today.