List Every File In Your Google Drive
Learn how to programmatically log every file in your Google Drive using Google Sheets and Apps Script. This tutorial walks you through creating a function that retrieves file names and IDs efficiently.
In order to log every file or get the file name, get some file information of everything on your Google Drive. You do not have to copy, paste and get every single file. We can do this programmatically in a Google sheet. I've created a sheet called Files and we have a file name and a file ID here that we want to fill in for every single file on our Google Drive. So let's go to Extensions app script . We're gonna write a function called Log Every file. And now we're gonna get const files equals Drive App with Capital A and capital D get files. And now, wow. We're going to make a little while. Loop Files has next. This means that it's gonna check, we're gonna look at a file and does it have a next one? If it does, we're gonna go to that next one. And this is how we go through every single file inside of our Drive app.
But this is going to get also files that are in, that are shared with us. So we're gonna get the next file, meaning this is just the file, each file, and we're going to do something. We're gonna get a some variables variable file name equals file, docket name, variable file id equals file dot get id. And there's a whole lot of other things we can get here. We can get who the editors are. We can also get who the owner is, get their email. And what we wanna do is make sure that. We're only getting files that we are the owner of. If we do not want that, meaning we want everything, even those that are shared with us, then don't do this part that I'm about to do. So if owner is, is equal to two equal signs, I'll say variable me equals
spreadsheet app dot get active. Actually, I will do session. Get active user, get email, that's me. And if the owner of the sheet is me, then I am going to do something here, which is variable sheet equals get sheet by name and I think we called it files. Sheet append row, and I'm gonna put a square bracket here and start getting all of these items. We want the file name, file ID here, and for every single file we'll append that row. So let's check this out and see if it's working. So we're gonna create a, a wild loop that's going to. Loop through every single file. Only those who are the same owner as me will be appended to the sheet, may have
to authorize and review permissions. It's starting to log everything. Here's the id. Here's the name of the file. We got an error here. Cannot read properties of null, so there are going to be some files that we don't have access to that are still in our file that we can't access who the email is. So if the owner is me or I. If owner is equal to null, let's log those. So instead of this or owner equals null. We wanna do this, we need to set up that if there is no access, we can still just put null. So owner email equals owner . Question mark owner dot get email null. So what this is gonna do is it'll just put the null if there is no
there we go. Let's run this. And now we got through that block and we're just now running through every single file here. Even if we don't have access to get the owner 's email, we're just going to get those that are null and those that are me. We may want to also add another column here that's owner email to see which ones are null. And we can also get a URL directly to that sheet. So instead of just having the ID and then creating the URL from that, we can just get the URL there. So we can run this again. Make sure we have the parentheses after get URL or you're going to get something called this function native code. And there we go. We got all of these awesome sheets and files. There are all types of things inside of our. Google Drive and we can get them all, we can let this run for a few
more minutes and it'll get every single file on our Google Drive. Pretty cool, right? Looks something like this. It's gonna be a very long list of items of, and get all those spreadsheets and forms , everything 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.