?
Solved

Find current folder

Posted on 2007-10-11
4
Medium Priority
?
1,349 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:Millkind
  • 2
4 Comments
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 20058994
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
 

Author Comment

by:Millkind
ID: 20059029
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
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20073484
try giving full path like

var folder = fso.GetFolder("C://somewhere//somepath");
0
 
LVL 16

Accepted Solution

by:
Kiran Paul VJ earned 2000 total points
ID: 20073497
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

Featured Post

Industry Leaders: 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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

864 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