/ Published in: JavaScript
A function that will return a list of files from a directory.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
importPackage(java.io); function directoryList(startPath){ var fileObject=new File(startPath); var list = fileObject.list(); var results = []; for (var i=0; i<list.length; i++) { var child = new File(startPath + "/" + list[i]); if (child.isDirectory()){ var recurseDirectoryListing = directoryList(child.getCanonicalPath()); results = results.concat(recurseDirectoryListing); } else{ var fileArray = {}; fileArray['path'] = child.getCanonicalPath(); fileArray['name'] = child.getName(); fileArray['parent'] = child.getParent(); fileArray['hidden'] = child.isHidden() ; fileArray['dir'] = child.isDirectory() ; var pos = fileArray['name'].lastIndexOf('.'); if (pos < 0){ fileArray['ext'] = ''; }else{ fileArray['ext'] = fileArray['name'].substring(pos+1); } results.push(fileArray); } } return results; }