getFilesByName - case insensitivity

847 views
Skip to first unread message

Laurie Nason

unread,
Sep 17, 2021, 7:42:13 AM9/17/21
to google-apps-sc...@googlegroups.com
Hi all,
I have a folder of photos, all of them are a number followed by .jpg or .JPG
I want to be able to find all the files, say with 123456.jpg or 123456.JPG - essentially the file name, not including the type.
I thought I might be able to search for 123456.*, but that doesn't appear to work. I really don't want to have to scan through every single file as this will be much slower than searching this way.
Any ideas/suggestions?
var myFileID=DriveApp.getFolderById(myFolderID).getFilesByName(myFileName);
while (myFileID.hasNext()){
var myFile=myFileID.next();
myURL=templateURL.replace("??",myFile.getId());
}

Brett Grear

unread,
Sep 18, 2021, 5:57:07 AM9/18/21
to Google Apps Script Community
I think you would need to use the advanced Drive API to achieve this.  You can include a query as an optional parameter whiles using Files.list.

Only way I can think of doing it using the standard DriveApp is to do the getFileByName call once for each spelling.

Of course I could be wrong and more than happy to be schooled.

Clark Lind

unread,
Sep 18, 2021, 8:32:16 AM9/18/21
to Google Apps Script Community
Try using searchFiles() instead. 

Then you should be able to add the conditions you are searching for.  Something like should work, assuming you know the numbers in the title. Otherwise,
you could probably use some RegEx to find files with only digits in the title:

var files = DriveApp.searchFiles(
    'parent: myFolderID and title contains  *123456*  ');    //or something.. you may have to play with it
    while (files.hasNext()) {
    var file = files.next();
    myURL=templateURL.replace("??", file.getId()); 
}

Also, did you know you can serve images from drive? Amit from Labnol (if I remember correctly) shared a hack a while back about viewing images directly:
https://drive.google.com/uc?id=fileIdOfImage 

I don't know if that is still valid or if you can use that in websites with CORS etc. But may work in a GAS webapp.

Laurie Nason

unread,
Sep 19, 2021, 3:09:41 AM9/19/21
to google-apps-sc...@googlegroups.com
Thanks guys for your input.
So, I cheated.....
I wrote a script to rename any image.JPG to image.jpg.... very poor solution I know! But it worked! and pretty fast too. I probably should re-write my search to use the search files function, and may well do so. The photos only change rarely, but it would be good to cover this possibility.
I am serving the images from drive Clark - it's great - but unfortunately, you do have to make the file accessible by anyone on the internet and give them view permissions, not a huge problem as the fileID can't really be guessed, but still not ideal. 
Thanks again for the assistance.
Laurie

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/e4448e58-2eda-485b-bcb9-4758a4a9fe59n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages