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

Recursive function for getting files from folders

Guest
Jul 23, 2010 Jul 23, 2010

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

Arivu..

TOPICS
Scripting
3.8K
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

Community Expert , Jul 23, 2010 Jul 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
   
...
Translate
People's Champ ,
Jul 23, 2010 Jul 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.

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
LEGEND ,
Jul 23, 2010 Jul 23, 2010

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

Harbs

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 Expert ,
Jul 23, 2010 Jul 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;
    }
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
People's Champ ,
Jul 23, 2010 Jul 23, 2010

Sorry Peter not to have mentioned you 😉 The only one I forgot and it was the one to te quoted 😄

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
Guest
Jul 23, 2010 Jul 23, 2010

Thank you Peter.

Just now i have seen your InDesign script site. It is quite good.

http://www.kahrel.plus.com/indesignscripts.html

Arivu..

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 Expert ,
Jul 24, 2010 Jul 24, 2010
LATEST

I was blowing the dust off another file finder which I could not find earlier, and while trying that one out I discovered that getFiles ("*.*") also finds the recycle bin and everything in it. I don't know if this is a Windows-only thing or whether it affects Macs as well. To exclude the recycle bin, in the script I posted earlier, this line:

if (f.name.slice (-5).toLowerCase() == ".indd")

should be replaced with this one:

if (f.name.slice (-5).toLowerCase() == ".indd" && myFile.fullName.toLowerCase().indexOf ("recycle.bin") < 0)

The same goes for Kasyan's (nice and very compact) script. Change this:

if (myFile instanceof Folder){

to this:

if (myFile instanceof Folder && myFile.fullName.toLowerCase().indexOf ("recycle.bin") < 0){

The other script I meant is a file finder in which you can use the standard OS wildcards ? (any single character) and * (any range of characters). Here are some examples ("true" is a flag for subdirectory-inclusion: "true" means "include subdirs, "false" stands for "specified dir only"):

f = find_all ("/d/*.indd", true);

"Find all .indd files on drive d:\"

f = find_all ("/e/work/ch*.indd", true);

"Find all InDesign files on in folder e:\work\ whose name starts with ch"

f = find_all ("/d/*.ind?", true);

"Find all InDesign-related files (.indd, .indb, .indl, etc) on drive d:\"

f = find_all ("/e/test/*_*.jpg", true);

"Find all jpg files in the folder e:\test\ that have an underscore in their name"

Here is the script:

#target indesign;

f = find_all ("/d/*.indd", true);  // returns an array of file objects

// uncomment the next few lines to get visible output
/*
str = "Found " + f.length + " files\r" + create_string (f); if (f.length > 50)     app.documents.add().textFrames.add ({geometricBounds: ["36pt", "36pt", "806pt", "560pt"], contents: str}); else     alert (str);
*/

function find_all (mask, include_sub)     {     return find_all_sub (File (mask).path+"/", [], grep_mask (File (mask).name), include_sub)     } function find_all_sub (dir, array, grep, sub)     {     var f = Folder (dir).getFiles ("*.*");     for (var i = 0; i < f.length; i++)         {         if ((f instanceof Folder) && sub)             find_all_sub (f, array, grep, sub);         else             if (f.name.match (grep) != null && f.path.toLowerCase().indexOf ("recycle.bin") < 0)                 array.push (f);         }     return array;     } // Change the OS wildcards * and ? to GREP classes function grep_mask (m)     {     // First protect any original dots     m = m.replace (/\./g, "###");     // Replace ? with . (any single character)     m = m.replace (/\?/g, ".");     // Replace * with .+? (shortest string of any character)     m = m.replace (/\*/g, ".+?");     // Restore the original dots     m = m.replace (/###/g, "\\.");     // Create GREP string: from start to end of input, case-insensitive     return RegExp ("^" + m + "$", "i");     } function create_string (file_array)     {     var list = "";     if (file_array.length > 0)         for (var i = 0; i < file_array.length; i++)             list += file_array.fullName + "\r"     return list     }
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
Valorous Hero ,
Jul 23, 2010 Jul 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

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