[Jslib] Re: File IO

Pete Collins pete at mozdev.org
Sat Jul 26 13:53:43 EDT 2003


Here's what i came up w/.

I'm sure you can trim this code down and make it less verbose.


function test () {
   var d = new Dir('/tmp');
   var dirList = d.readDir();
 

   var dirs = new Array();
   var files = new Array();
 

   for (var i=0;i<dirList.length;i++)
     dirList[i].isDir() ?  dirs.push(dirList[i]) : files.push(dirList[i]);
 

   dirs.sort();
   files.sort();
 

   dirList = new Array();
   dirList.push((new Dir(".")));
   dirList.push((new Dir("..")));
 

   for (i=0;i<dirs.length;i++)
     dirList.push(dirs[i]);
 

   for (i=0;i<files.length;i++)
     dirList.push(files[i]);
 

   for (i=0;i<dirList.length;i++)
     jslibPrint(dirList[i].path);
}


Prints:

.
..
/tmp/.font-unix
/tmp/fubar
/tmp/lindows
/tmp/.iroha_unix
/tmp/.X11-unix
/tmp/.ICE-unix
/tmp/mcop-petejc
/tmp/orbit-petejc
/tmp/test
/tmp/xpi
/tmp/jslib
/tmp/vanguard
/tmp/ssh-XXZZRIBM
/tmp/kde-petejc
/tmp/ksocket-petejc
/tmp/ksocket-root
/tmp/kde-root
/tmp/orbit-root
/tmp/testing
/tmp/strange.logs
/tmp/.X0-lock
/tmp/newprojects
/tmp/diff
/tmp/capdata1.doc
/tmp/foo
/tmp/events



Is this the end result you want to achieve?



--pete



Jacob Emcken wrote:
> Hi
> 
> I use the the readDir() to generate an array with all the files and dirs
> in the current path.
> 
> Now I want to append a Dir object in the beginning og the array with the
> parent directory i.e.
> 
> var dir = new Dir(path);
> var dirList = dir.readDir();
> 
> var parentDir = dir.parent;
> parentDir.leaf = "..";
> dirList.unshift(parentDir);
> 
> But if I alter the leaf the path gets all fucked up :(
> 
> I'm probaly doing this the wrong way but I want to make a loop that list 
> the files and dirs in a list with the parent dir first like this.
> 
> ..
> Documents
> .mozilla
> .gaim
> example.txt
> test.swx
> etc.
> 
> 
> The resaon why I want just to alter the leaf is to avoid having if-else 
> in my loop.
> 
> Thanks
> -- Jacob
> 



More information about the Jslib mailing list