[VB, CS3] How to place images using wildcards in filenames
Copy link to clipboard
Copied
Can anyone think of a way to place an image using a wildcard in the filename?
I can place the image fine if the complete filename is known, but what if the filename changes from day to day? - eg it could be House1234.tif for one document then House56r.tif for another. So I'd like to place House*.tif sort of thing. The files are in different folders, of which the path is known.
Any ideas?
Nigel
Copy link to clipboard
Copied
function find_file (dir, mask)
{
var f = Folder (dir).getFiles ('*.*');
for (var i = 0; i < f.length; i++)
if ( // compare f.name and mask here
return f;
}
This returns the first file in "dir" that matches "mask".
Peter
Copy link to clipboard
Copied
I found this very helpful, thanks!
I scan the crossword every day and use a script to place it in InDesign, create a matching grid and so on, to do the crossword on screen. I wanted to use the scanner's default choice of folder and filename, which is something like "15-05-2019_103515.jpg" (from today's date + time). Although I didn't use a wildcard in my mask, I used the slice method to remove all but the date.
Copy link to clipboard
Copied
Hi Jeremy,
method getFiles() supports a filter function as well.
var myFilteredFilesArray = myFolder.getFiles
(
function( file )
{
if
(
file instanceof File
&&
file.name.match( /* testForSuffixRegExp or whatever you like */ )
&&
!file.hidden
)
return true
}
);
Hope, you'll get the idea.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
That looks very useful too, thanks very much! I'm sure I'll use that approach in future. For what it's worth, here's the code I was using before I decided the slice method was easier:
var myRegex = new RegExp(myYear + "-" + myMonth + "-" + myToday + "_\\d{6}\\.jpg");
var myFile = findFile("Macintosh HD/Users/jeremy/Pictures", myRegex);
myPage.place(myFile, [36,36]);
function findFile(myFolder, myRegex) {
var myFiles = Folder(myFolder).getFiles("*.*");
for (var i = 0; i < myFiles.length; i++) {
if (myFiles.name.match(myRegex)) return (myFiles);
}
}
Jeremy
Copy link to clipboard
Copied
(Works beautifully too, by the way — thanks again!)

