Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Recursive File/Folder search?

Advocate ,
Sep 17, 2012 Sep 17, 2012

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;

}

TOPICS
Scripting
3.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Sep 23, 2012 Sep 23, 2012

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.name + "\r");

...
Translate
Advocate ,
Sep 17, 2012 Sep 17, 2012

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.name;

                    if(fileCheck > 0){

                              for(var d=0; d<fileCheck; d++){

                                        fileNames[fileNames.length] = "\t" + gatherFiles.name;

                              }

                    }

          }

}

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 17, 2012 Sep 17, 2012

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.isDirectory) {

                     if (files.name !="." && files.name !="..") {

                         //it's a directory

                         getFilesRecursive(files.nativePath);

                     }

                 } else {

                     //it's a file yupeee

                     fileList.push(files.nativePath);

                 }

             }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 20, 2012 Sep 20, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 23, 2012 Sep 23, 2012

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.name + "\r");

        if (theFiles instanceof Folder) {

            checkForFiles(theFiles, tabString + "\t");

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 24, 2012 Sep 24, 2012

Bingo! Calling the function from within itself. Doh! Thanks Paul.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 26, 2012 Sep 26, 2012
LATEST

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.

          }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines