Proudly made on earth

Create list of files in a Google Drive folder

Drawers File Cabinet

CC-BY-NC-ND: Daniela Goulart


Update 02.09.2018

It’s funny how technology promises us more time but takes so long to install, use and maintain that in the end, savings often are minimal.

That’s what came to my mind when I created the following script, designed to list all files in a Google Drive folder as efficiently as possible. It’s a very simple little program, but installing it can be overly complicated. To make a long story short, I decided to turn this script into a web app, meaning that it no longer needs to be installed by each individual user.

From now on, users only need to launch listAllFilesInFolder, grant permissions and enter the name of a folder. All files within that folder (not including subfolders) will then be listed in a new spreadsheet.

Launch listAllFilesInFolder

Let’s say you want to sort your list of files by size, or anything else. Just open the spreadsheet and identify the column containing the properties you want to sort by (with file size, it should be column F). Click on the downwards arrow next to the column title and select Sort sheet A → Z (or vice versa). Done!

In case you prefer to install the script manually, I’ve preserved the original version of this post down below containing the source code and a detailed guide on how to use it.

Privacy policy

When using listAllFilesInFolder, none of your personal data is ever collected by, used by, or shared with any third parties. You don’t have to trust me on that, please have a look at the source code and feel free to use, share and modify my code.

To do its job, listAllFilesInFolder requires permission to view and manage the files in your Google Drive and permission to view and manage your spreadsheets in Google Drive. You can revoke these permissions at any time if you no longer want to use the app.


Original post

When it comes to managing my data, I generally choose control over convenience. Sure, it’s tiring to constantly carry several hard drives, deal with encryption, and keep my files synchronised. But in return I can be sure that my backup is always up to date, even when I travel to remote regions where people can’t even spell the word internet (I’m thinking of you, rural Germany).

There are many more advantages to this approach, one of them being the fact that I can use the command line to deal with simple operations like batch renaming or creating a list of all files in a given folder. The latter can be acomplished very easily by using the ls command on Unix-based operating systems or the dir /b command on Windows.

If you only keep your files in the cloud, it is a bit harder to create an inventory of a folder’s contents. Google Drive doesn’t even show you the number of files in a folder. There is however the possibility of using a Google Apps Script to perform a wide variety of operations, exceeding by far the possibilities of a simple command line on your local machine.

You will find a number of sample scripts on the web that will list all files in a folder to a spreadsheet. The only problem with these code snippets is that they handle each file individually, making the whole process very slow. With some help, I came up with a solution that reads the files into cache and then saves all of them at the same time. As a result, the execution time is drastically reduced and the script handles 5,000+ files without any error.

Here’s a comprehensive, step-by-step guide on how to implement the code:

  1. Open your Drive and select New > More > Connect more apps
  2. Search for Google Apps Script and install
  3. Click on New > More > Google Apps Script
  4. Copy the code from below, replace XYZ on line two with the name of your folder and save
  5. Click on the little play button that says Run
  6. From the authorisation screen, select Review Permissons and confirm your account
  7. Don’t be shocked if you see the verification warning. You can easily verify the code yourself. Click on Advanced > Go to Project name (unsafe)
  8. Grant access to Google Drive and Spreadsheets
  9. Wait until the script has run successfully, meaning the yellow status bar has disappeared
  10. Return to the root directory of your Drive where you will find a spreadsheet titled files_XYZ that contains a list of all the files in your folder

If the contents of your folder have changed and you want to recreate the list, simply run the Google Apps Script again.

Note: This script won’t work with empty folders or subfolders.

Florian Lehmuth
8 August 2016
Categories:
Tags:

24 Kommentare

What’s your take?