Find Files in Folder by Name Apps Script
Learn how to find files in a specific Google Drive folder by name using Apps Script, ensuring you retrieve the correct file even when multiple files share the same name.
In a previous video I mentioned you could enter a Google Drive file name and get the URL and in app script we did this, we just looked for the file name. We used drive app dot, get files by name and look for that file name. And I mentioned that if you have a file or two files that have the same name, they're gonna cause issues. And I recommended to someone in our comments that you should just not have. The same file name, but that's not necessarily practical for all purposes. Sometimes you have the same file name but in different folders. So how do you search in a specific folder for that file name that you know is unique in that folder? So I'm gonna show you that here in these, my Google Drive, I have two sheets two
documents called Google Sheets templates . I have. Copy it so that it's exactly the same, but that it's the same name and it's gonna find those, except I want to create a new folder. I'm gonna create a folder called templates. And one of these, I'm gonna move over there. I'm gonna move it to templates. It's so now I have the exact same. File that has, sorry, the two different files that have exactly the same name, but one is in templates. Now the key here is that in this templates folder, it has a URL, and at the end of the URL is an id. So I'm gonna start another function . Get URL. In folder with a file name and it's going to be exactly this same function except this drive app dot get files by name. We're gonna add one thing before that, get files by name and
we're gonna say, get folder. By id and we're going to put the ID or the bit at the end of the URL, that is the ID of the folder right there. So now it's only going to search for that folder. So get URL in templates folder. So I can create a new column that says, enter name. Get URL in templates folder. I'm gonna change this function to have file name, get URL from templates and instead of this get URL we're going to use get URL in templates folder. Again, in this previous video I had mentioned, we created this function that we wanted to have occur on every edit, and so we need to create a trigger for that. On edit save.
I do need to do one more edit to the function , which is change the column, right? So I had D and E here, so if column is equal to four, then we're going to use that function . And again, we're looking in this templates folder. We don't have to write in templates, but we do need to get the name. And there I have a URL of the sheet, and it doesn't give me an error . It's not like looking for two files. It's going to get the file that's in the templates folder. We can see that it's a different file too than this one that we had earlier because of the id. It's like one something mw, and this is one cl. We can see that it's actually getting the URL of the file inside that template 's folder. So again, the main purpose of this video is that you can add this get
folder by ID before you get files by name, so you can actually get. Files within a very specific folder. You are watching better sheets here on YouTube. Make sure you check out this video or this video and subscribe right now to get more tips, tricks, how tos, get more out of your Google sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos. But for right now, right here, one of these videos is gonna be your next Google sheet.