Copy link to clipboard
Copied
I want to search "indd" files from my whole directory (including subdirectories).
Arivu..
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
...
Copy link to clipboard
Copied
Look into the archive, Kasyan has posted such a script, or was it Harbs, Jongware...Don't remember but the script is somewhere around.
Copy link to clipboard
Copied
I think it might have been Peter Kahrel, but I don't remember for sure either... ![]()
Harbs
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Sorry Peter not to have mentioned you 😉 The only one I forgot and it was the one to te quoted 😄
Copy link to clipboard
Copied
Thank you Peter.
Just now i have seen your InDesign script site. It is quite good.
http://www.kahrel.plus.com/indesignscripts.html
Arivu..
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more