Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Thanks for sharing!
Here's a similar function — see How to find all indd-files inside subfolders — and some other useful snippets.
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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
}
}
}
}
Copy link to clipboard
Copied
Thanks. Just FYI the request was to save all files back up to the root folder.
Copy link to clipboard
Copied
Thank you for notice, I have upated the code in my post.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Lots of ways to skin a cat. Thanks for reaching out, Martin.
-Brian
Copy link to clipboard
Copied
Correct. Hehe. many thanks!