Skip to main content
July 23, 2010
Answered

Recursive function for getting files from folders

  • July 23, 2010
  • 2 replies
  • 3730 views

I want to search "indd" files from my whole directory (including subdirectories).

Arivu..

This topic has been closed for replies.
Correct answer Peter Kahrel

So it was -- the function is below. You call it like this:

var myFiles = ("/d/test" );

It starts searching at the specified path and includes all subdirectories. It returns an array of file objects.

Peter

function find_files (dir)
    {
    return find_files_sub (dir, []);
    }

function find_files_sub (dir, array)
    {
    var f = Folder (dir).getFiles ("*.*");
    for (var i = 0; i < f.length; i++)
        {
        if (f instanceof Folder)
            find_files_sub (f, array);
        else
            if (f.name.slice (-5).toLowerCase() == ".indd")
                array.push (f);
        }
    return array;
    }

2 replies

Kasyan Servetsky
Legend
July 23, 2010

Here is a script that illustrates how to process all subfolders in the selected folder.

See getSubFolders function.

// Get all subfolders in the selected folder
function getSubFolders(theFolder) {
     var myFileList = theFolder.getFiles();
     for (var i = 0; i < myFileList.length; i++) {
          var myFile = myFileList;
          if (myFile instanceof Folder){
               mySubFolders.push(myFile.absoluteURI);
               getSubFolders(myFile);
          }
     }
     return mySubFolders;
}

Hope this helps.

Kasyan

Loic.Aigon
Legend
July 23, 2010

Look into the archive, Kasyan has posted such a script, or was it Harbs, Jongware...Don't remember but the script is somewhere around.

Harbs.
Legend
July 23, 2010

I think it might have been Peter Kahrel, but I don't remember for sure either...

Harbs

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
July 23, 2010

So it was -- the function is below. You call it like this:

var myFiles = ("/d/test" );

It starts searching at the specified path and includes all subdirectories. It returns an array of file objects.

Peter

function find_files (dir)
    {
    return find_files_sub (dir, []);
    }

function find_files_sub (dir, array)
    {
    var f = Folder (dir).getFiles ("*.*");
    for (var i = 0; i < f.length; i++)
        {
        if (f instanceof Folder)
            find_files_sub (f, array);
        else
            if (f.name.slice (-5).toLowerCase() == ".indd")
                array.push (f);
        }
    return array;
    }