Copy link to clipboard
Copied
Ok, my brain is not getting something here. Any help appreciated. I am trying to read through a folder structure (OS X based) recursively. I started to write the loop section, but then saw myself getting into a manual loop setup sort of thing. The setup where I start a series of "for" and "if" rabbit hole. What am I missing to dynamically dive through the folders recursively, simply?
var path = Folder.selectDialog("Please select starting folder.");
if(path != null){
var startingFolder = path;
var initialFolders = startingFolder.getFiles();
var initialFileCount = initialFolders.length;
var fileNames = new Array();
// LOOP STARTING HERE ————————————————————————————————
for(var c=0; c<initialFileCount; c++){
var fileCheck = checkForFilesValidator(initialFolders
if(fileCheck > 0){
for(var d=0; d<fileCheck.length; d++){
}
}
}
//————————————————————————————————
}
function checkForFilesValidator(fileFolderInput, extension){
try{
if(extension != ""){
extension = "*." + extension;
}
var retrieve = fileFolderInput.getFiles(extension);
}catch(err){
return 0;
};
return retrieve.length;
}
I'm not sure exactly what you're trying to do but maybe this will help:
var theFolder = Folder.selectDialog("Please select starting folder.");
if(theFolder != null){
var resultArray = new Array();
checkForFiles(theFolder, "");
$.writeln(resultArray.toString().replace(new RegExp(",", "g"), ""));
}
function checkForFiles(folderItem, tabString) {
var theFiles = folderItem.getFiles();
for(var c = 0; c < theFiles.length; c++){
resultArray.push(tabString + theFiles
Copy link to clipboard
Copied
Noticed I had a bad line in the code where I was trying to use a numerical value as a file object. Oops. Ok, so this setup gets me the first two levels of info from the initial selected folder. I can manually continue this way since I know how many levels I need, but I would like to make this less hard coded to this one task and more dynamic for future use. Any thoughts?
var path = Folder.selectDialog("Please select starting folder.");
if(path != null){
var startingFolder = path;
var initialFolders = startingFolder.getFiles();
var initialFileCount = initialFolders.length;
var fileNames = new Array();
for(var c=0; c<initialFileCount; c++){
var fileCheck = checkForFilesValidator(initialFolders
var gatherFiles = retrieveFiles(initialFolders
fileNames[fileNames.length] = initialFolders
if(fileCheck > 0){
for(var d=0; d<fileCheck; d++){
fileNames[fileNames.length] = "\t" + gatherFiles
}
}
}
}
var resultsList = fileNames.toString().replace(new RegExp(",", "g"), "\r");
alert("\n" + resultsList);
/// checkForFilesValidator
function checkForFilesValidator(fileFolderInput, extension){
try{
if(extension != ""){
extension = "*." + extension;
}
var retrieve = fileFolderInput.getFiles(extension);
}catch(err){
return 0;
};
return retrieve.length;
}
/// retrieveFiles
function retrieveFiles(fileFolderInput, extension){
try{
if(extension != ""){
extension = "*." + extension;
}
var retrieve = fileFolderInput.getFiles(extension);
}catch(err){
return null;
};
return retrieve;
}
Copy link to clipboard
Copied
i'm no recursion expert, but i think you have to wrap the entire thing in a function that you recall down the line. so it will keep digging until the criteria is met.
a quick search brought up this function (albeit for AIR - http://cookbooks.adobe.com/post_Recursive_folder_listings_and_contents_processing-9410.html😞
function getFilesRecursive(folder) {
//the current folder object
var currentFolder = new air.File(folder);
//the current folder's file listing
var files = currentFolder.getDirectoryListing();
//iterate and put files in the result and process the sub folders recursively
for (var f = 0; f < files.length; f++) {
if (files
if (files
//it's a directory
getFilesRecursive(files
}
} else {
//it's a file yupeee
fileList.push(files
}
}
}
Copy link to clipboard
Copied
Thanks, for posting that. I've started translating this to fit within ExtendScript's world and no solid results yet, but I'm still trying. In the meantime I'm still open to any other input.
Copy link to clipboard
Copied
I'm not sure exactly what you're trying to do but maybe this will help:
var theFolder = Folder.selectDialog("Please select starting folder.");
if(theFolder != null){
var resultArray = new Array();
checkForFiles(theFolder, "");
$.writeln(resultArray.toString().replace(new RegExp(",", "g"), ""));
}
function checkForFiles(folderItem, tabString) {
var theFiles = folderItem.getFiles();
for(var c = 0; c < theFiles.length; c++){
resultArray.push(tabString + theFiles
if (theFiles
checkForFiles(theFiles
}
}
}
Copy link to clipboard
Copied
Bingo! Calling the function from within itself. Doh! Thanks Paul.
Copy link to clipboard
Copied
Below is a recursive function I was using for something, also, might be of help.
function getSubContents(folder){
if ((folder != null) && (folder instanceof FolderItem)){
var projItem;
var i = folder.numItems;
while (i>0){//While is faster than for loops
projItem = folder.items;//eachItem
selItems[selItems.length] = projItem;//add all subItems to array
if (projItem instanceof FolderItem)
getSubContents(projItem);//recurse that folder to selItems
i--;
}//end while
}
return selItems;//return the array.
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more