directoryList in Rhino


/ Published in: JavaScript
Save to your folder(s)

A function that will return a list of files from a directory.


Copy this code and paste it in your HTML
  1. importPackage(java.io);
  2.  
  3. function directoryList(startPath){
  4. var fileObject=new File(startPath);
  5. var list = fileObject.list();
  6. var results = [];
  7.  
  8. for (var i=0; i<list.length; i++) {
  9. var child = new File(startPath + "/" + list[i]);
  10.  
  11. if (child.isDirectory()){
  12. var recurseDirectoryListing = directoryList(child.getCanonicalPath());
  13. results = results.concat(recurseDirectoryListing);
  14. }
  15. else{
  16. var fileArray = {};
  17. fileArray['path'] = child.getCanonicalPath();
  18. fileArray['name'] = child.getName();
  19. fileArray['parent'] = child.getParent();
  20. fileArray['hidden'] = child.isHidden() ;
  21. fileArray['dir'] = child.isDirectory() ;
  22. var pos = fileArray['name'].lastIndexOf('.');
  23. if (pos < 0){
  24. fileArray['ext'] = '';
  25. }else{
  26. fileArray['ext'] = fileArray['name'].substring(pos+1);
  27. }
  28.  
  29. results.push(fileArray);
  30. }
  31.  
  32. }
  33. return results;
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.