Skip to main content
Known Participant
March 21, 2024
Answered

How to search folders for a similar by different filename?

  • March 21, 2024
  • 3 replies
  • 1866 views

Hello,

I'm writing a script to replace links in an InDesign file with images that have similar, but different file names.

 

So, suppose I have a set of links in a file with the following names:

  • 100A_Apple.psd
  • 200B_Orange.psd
  • 300C_Banana.psd

And I want InDesign to find matching files in a folder to replace the links, but the codes are different (there is no logic to the numbers, so math won't work):

  • 527X_Apple.psd
  • 109Y_Orange.psd
  • 845Z_Banana.psd

 

I'm using getFiles() to store file names from the folder in an array. Then I need to search that array for a matching file and return the file name so I can relink the image.

 

I've tried several methods to search the array for substrings, but none of them work. What I can glean from my research is that ExtendScript doesn't have access to higher order functions like filter(), find(), findIndex(), or includes(). So I probably have to write my own function, but that's where my coding skills start to falter. Any help?

 

This topic has been closed for replies.
Correct answer @AG

This reference can help! 

var avlbleLinks = ["100A_Apple.psd","200B_Orange.psd", "300C_Banana.psd"];
var rplcLinks = ["527X_Apple.psd", "109Y_Orange.psd","845Z_Banana.psd"];

if (rplcLinks.length > 0){
    for(var r=0; r<rplcLinks.length ; r++){
        var checkString = String(rplcLinks[r]).split("_");
        var requrdStrng = checkString[checkString.length -1].replace(".psd", "");
        for(var l=0; l<avlbleLinks.length; l++){
            if(String(avlbleLinks[l]).search(requrdStrng)> -1){
                alert(avlbleLinks[l])
                alert(rplcLinks[r])
                break
            }
        }
    }
}

3 replies

Inspiring
March 21, 2024
var oldLinks = ["200B_Orange.psd", "300C_Banana.psd", "100A_Apple.psd"];
var newLinks = ["845Z_Banana.psd", "527X_Apple.psd", "109Y_Orange.psd",];


var oldLinksSorted = sortArray(oldLinks);
var newLinksSorted = sortArray(newLinks);

alert(oldLinksSorted);
alert(newLinksSorted);


function sortArray (images_arr) {
    temp_arr = [];
    var grep = /(.+)_(.+)\.(.+)/;
    var replace = '$2_$1.$3'; 
    for (var i = 0; i < images_arr.length; i++) {
        var temp = images_arr[i].replace (grep, replace);
        temp_arr.push(temp);
    }
    temp_arr.sort();
    newTemp_arr = [];
    for (var j = 0; j < temp_arr.length; j++) {
        var newTemp = temp_arr[j].replace (grep, replace);
        newTemp_arr.push(newTemp);
    }
    return newTemp_arr
}
Robert at ID-Tasker
Legend
March 21, 2024

@nicosh 

 

But where is the part that is doing relinking?

 

Inspiring
March 22, 2024

I thought that his main problem was to match the file names

var myLinks_arr = mergeLinks(oldLinksSorted_arr, newLinksSorted_arr);
/*
myLinks_arr = [
[100A_Apple.psd,527X_Apple.psd],
[300C_Banana.psd,845Z_Banana.psd],
[200B_Orange.psd,109Y_Orange.psd]
]
*/
function mergeLinks (oldLinks_arr, newLinks_arr) {
    if (oldLinks_arr.length == newLinks_arr.length) {
        var temp_arr = [];
        var myLinksTemp_arr = [];
        for (var i = 0; i < oldLinks_arr.length; i++) {
            temp_arr[0] = oldLinks_arr[i];
            temp_arr[1] = newLinks_arr[i];
            myLinksTemp_arr.push(temp_arr);
            temp_arr = [];
        }
        return myLinksTemp_arr
    } else {
        alert('Houston we have a problem')
    }
}

 

Add to previous code. 

Robert at ID-Tasker
Legend
March 21, 2024

@chosch 

 

I'm not JS guy, but I'm pretty sure you should be able to use File() with wildcards.

 

So after you get the name of the link - you can split it by "_" - get the last part and build new filename

 

File("*_"+old_name);

 

Or something like that.

 

If not, then you should build a new name with the wildcard, get list of all files in the specified folder - then search this list.

 

Not sure how fast iterating collection / array will be, but I would just concatenate all names from the folder - with some separator - and then used RegExp to extract full name:

 

 

const ListOfFiles =  "#$#527X_Apple.psd#$#109Y_Orange.psd#$#845Z_Banana.psd#$#";
const RegExp = ".{4}_";
const OldFileName = "Orange.psd";

const Found = ListOfFiles.match(RegExp + OldFileName);

 

 

Of course someone can write you a much better RegExp query - using lookbehind/ahead.

 

Robert at ID-Tasker
Legend
March 21, 2024
@AGCorrect answer
Inspiring
March 21, 2024

This reference can help! 

var avlbleLinks = ["100A_Apple.psd","200B_Orange.psd", "300C_Banana.psd"];
var rplcLinks = ["527X_Apple.psd", "109Y_Orange.psd","845Z_Banana.psd"];

if (rplcLinks.length > 0){
    for(var r=0; r<rplcLinks.length ; r++){
        var checkString = String(rplcLinks[r]).split("_");
        var requrdStrng = checkString[checkString.length -1].replace(".psd", "");
        for(var l=0; l<avlbleLinks.length; l++){
            if(String(avlbleLinks[l]).search(requrdStrng)> -1){
                alert(avlbleLinks[l])
                alert(rplcLinks[r])
                break
            }
        }
    }
}
Robert at ID-Tasker
Legend
March 21, 2024

@@AG 

 

With two lists of files - and ordered by how they should be replaced - what's the point of all those comparisons?

 

choschAuthor
Known Participant
March 21, 2024

Because my image links aren't ordered by how they should be replaced, and I can't control how they're ordered.