Directory dir ls

From PlainOldWebserver

Jump to: navigation, search

"I should create a directory listing function, but in the meantime, here is a flexible way to recursively find all files below a directory. It uses a non-public function. For the non-recursive version, do not push directories onto the stack." - From DK

 <?sjs
 var f =  pow_file_get_handle("/pow/");
 var dir_stack = new Array(f);
 while( dir_stack.length ) {
   var entries = dir_stack.pop().directoryEntries;
   while(entries.hasMoreElements() ) {
     var entry = entries.getNext();
     entry.QueryInterface(Components.interfaces.nsIFile);
     if( entry.isDirectory() ) {
       dir_stack.push(entry);
       document.writeln("Dir: "+entry.leafName+"<br/>\n");
     } else {
       document.writeln("File: "+entry.leafName+"<br/>\n");
     }
   }
 }
?>

Or For Files below htdocs

 <?sjs
 var f =  pow_file_get_handle("/pow/htdocs/");
 var dir_stack = new Array(f);
 while( dir_stack.length ) {
   var entries = dir_stack.pop().directoryEntries;
   while(entries.hasMoreElements() ) {
     var entry = entries.getNext();
     entry.QueryInterface(Components.interfaces.nsIFile);
     if( entry.isDirectory() ) {
       dir_stack.push(entry);
       document.writeln("Dir: "+entry.leafName+"<br/>\n");
     } else {
       document.writeln("File: "+entry.leafName+"<br/>\n");
     }
   }
 }
?>


Let us keep links to original source materila http://developer.mozilla.org/en/docs/Code_snippets:File_I/O#Enumerating_files_in_given_directory

Personal tools
Navigation