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

Get indd files from folder and its subfolders

Explorer ,
Dec 28, 2021 Dec 28, 2021

I messaged brianp311 asking about my problem about gettinng indd files from folder and its subfolders  and I want to make his reply public as a reference for others who wants to do the same.

 

Here is the template code:

var main = function() {
    var mainFol = Folder.selectDialog("Choose the main folder");
    recursiveFunc(mainFol, mainFol); 
}

var recursiveFunc = function(folder, mainFol) {
    var inddRx = /\.indd$/gi;
    var allFiles = folder.getFiles();
    var doc;
    for (var i = 0; i < allFiles.length; i++) { 
        if (allFiles[i].hidden) continue;
        if (allFiles[i] instanceof Folder) { 
            recursiveFunc(allFiles[i], mainFol);
            continue;
        }  
        if (inddRx.test(allFiles[i].name) { 
            doc = app.open(allFiles[i]);
            doc.save(File(mainFol.fsName +"/" + allFiles[i].name));
            doc.close(SaveOptions.NO);
        }
    }
}

 Thank you for this, Brian!

TOPICS
Scripting
1.0K
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 ,
Dec 28, 2021 Dec 28, 2021

Thanks for sharing!
Here's a similar function — see How to find all indd-files inside subfolders — and some other useful snippets.

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 ,
Dec 28, 2021 Dec 28, 2021

Hi Martin,

I found that in very complex folder hierarchies with a lot of files and folders it could be an advantage to first do one loop with a recursive function to gather all the folders first, store them in an array and then do another loop on the stored folders of the array to get the files.

 

Regards,
Uwe Laubender

( ACP )

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 ,
Dec 28, 2021 Dec 28, 2021

I have modified your code as below.

 

 

var main = function () {
    var mainFol = Folder.selectDialog("Choose the main folder");
    recursiveFunc(mainFol, mainFol);
}

var recursiveFunc = function (folder, rootDir) {
    var inddRx = /\.indd$/gi;
    var allFiles = folder.getFiles();
    var doc;
    for (var i = 0; i < allFiles.length; i++) {
        if (allFiles[i].hidden) continue;
        if (allFiles[i] instanceof Folder) {
            recursiveFunc(allFiles[i], rootDir);
        }
        else if (inddRx.test(allFiles[i].name)) {
            try {
                doc = app.open(allFiles[i]);
                doc.close(SaveOptions.YES, new File(rootDir + "/" + doc.name));
            }
            catch (e) {
                //File may be in upper version or corrupt
            }
        }
    }
}

 

-Sumit
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 ,
Dec 28, 2021 Dec 28, 2021

Thanks. Just FYI the request was to save all files back up to the root folder. 

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 ,
Jan 01, 2022 Jan 01, 2022
LATEST

Thank you for notice, I have upated the code in my post.

 

 

-Sumit
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 ,
Dec 28, 2021 Dec 28, 2021

Here is mine:

function getFiles(folder, subfolders, extension) {
    // folder = folder object, not folder name.
    // subfolders = bool, true = include subfolders.
    // extension = string, extension to include.
    // extension undefined = any.
    // Combine multiple extensions with regex OR, i.e. indd|tif|txt
    // Ignores hidden files.
    var d = [];
    var f;
    var files;
    var i;
    var pattern = new RegExp("\." + extension + "$", "i");
    files = folder.getFiles();
    for (i = 0; i < files.length; i++) {
        f = files[i];
        if (f instanceof Folder && subfolders) {
            // Recursive (function calls itself).
            d = d.concat(getFiles(f, subfolders, extension));
        } else if (f instanceof File && !f.hidden && (extension == undefined || pattern.test(f.name))) {
            d.push(f);
        }
    }
    return d;
}

// USAGE

var files = getFiles(folder, true, "indd");

for (var i = 0; i < files.length; i++) {
    // Do something with each file.
}

Many free scripts here: https://www.marspremedia.com/software/indesign

 

William Campbell
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 ,
Dec 28, 2021 Dec 28, 2021

Lots of ways to skin a cat. Thanks for reaching out, Martin. 

-Brian

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
Explorer ,
Dec 30, 2021 Dec 30, 2021

Correct. Hehe. many thanks!

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