Google Apps Script: Get File By Name – With Optional Parent Folder Crosscheck

Google Apps Script:  DriveApp

One of my recent projects in Google Apps Script required me to search for a file by name and get its ID. This can be problematic in Google Drive because you can have multiple files of the same name in multiple locations. My solution was to also check the file’s parent folder name as well. 

I created a function getFileByName() to handle this. The function takes the following parameters:

  • fileName – The name of the file you are looking for. (required)
  • fileInFolder – The parent folder of the file you are searching for. (optional)

The file path would look a little something like this:

.../fileInFolder/fileName

getFileByName() returns an object containing :

  1.  the ID of the file if the file exists or false if it does not.
  2. the ERROR if there is an error or false if there is not. 

The returned object would look like the following:

Successful

Error

getFileByName() reports the following errors:

  1. If no parent folder name is given and there are more than one file with the same file name. 
  2. There is more than one parent folder and file combination with the same name.
  3. There are multiple files with the same name but none are in the folder parent name provided.
  4. No file with the file name provided exists.

The Code

An Example

The result of this example would be: 

Logs

Create and Publish Google Workspace Add-ons with Apps Script Course 300px

[18-10-04 21:59:33:127 PDT] keJCYO4cCwZhNvyprL63jDvfluwP0gLtV

You might also find this useful: 

Google Apps Script – How to Find the Folder ID of a Non-Unique Folder Using File Path Names

I hope you find it useful.

Create and Publish a Google Workspace Add-on with Apps Script Course

Need help with Google Workspace development?

My team of experts can help you with all of your needs, from custom app development to integrations and security. We have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.


4 thoughts on “Google Apps Script: Get File By Name – With Optional Parent Folder Crosscheck”

  1. If I understand your code correctly, I will need to specify the full name of the file for the file iterator to return true. What if I only vaguely remember the file name, e.g. I want to file files whose names contain ‘yagisana’? Do I have to get all the files and loop through them?

    1. Hi Trang,

      The searchFiles() method is proabably your best bet here with search query terms found here.

      I hope that helps.

      BTW. I like your webpage. Very elegant and Google Drive Search CLI App looks cool.

      ~Yagi

  2. Hi Yagi,

    I did not understand the following piece of code;
    //Get the folder name for each file
    while(folders.hasNext()){
    folder = folders.next().getName();
    foldercount++;
    };

    Won’t we get the name of the last folder which might match with fileInFolder?

    1. Hi sheelooxl,

      Yeap. You are correct. It should get the last value in the list of any potential parent folders for the file. The example was only designed to compare one folder. If there are more, it will throw a warning in the lines 45 and 46 code. So the lazy-man option here was just to record the last value in the list of all parent folders of the file and if there were more than one folder, we would record it in the foldercount variable and that would trigger an error message.

      A little context. Back in 2018 when this was written, Google Drive was only flirting with creating multiple directory locations for the same file so it was just a cursory check.

      I think also to the code I was writing needed the file to have one folder locations; the reason for which has long since escaped me.

      Today, I would write things a little differently 🙂.

      Great question.

      Happy coding!

Leave a Reply