Get a list of IDs for files in a Google Drive folder

In my previous post, I described how to obtain the ID of a file stored in Google Drive. The procedure is suitable for one or a handful of files. But obtaining the IDs in this kind for a large number of files can be very tiring. In this post I will offer a solution how to automatically create a list with IDs of files in a folder.

As first obtain the ID of the folder. Select the folder and click the link symbol.

Obtain the folder ID

Copy the ID like in the following screenshot.

Copy the ID

Click on the big "New" button in the top left corner.

Click New

Click on "Google Sheets".

Click on "Google Sheets"

A new window will open with a table like in Excel. Click on "Tools" and then "Script editor".

Again, a new window will open.

New window

Replace the code with the following:

function myFunction() {
  var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");
  var contents = folder.getFiles();
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  var file;
  var id;
  while(contents.hasNext()){
    file = contents.next();
    id = file.getId();      
    sheet.appendRow([id]);
  }
};

Repalce YOUR_FOLDER_ID with the ID of the folder you obtained in the first step, click on the SAVE symbol and the on the RUN symbol.

Repalce YOUR_FOLDER_ID with the ID

If you run this script the first time you will face a sequence of permission requests you have to confirm. Click on "Review Permission".

Click "Review Permission"

Select your account.

Select your account

Click on "Advanced".

Click "Advanced"

Click on "Go to Untitled project (unsafe)"

Click on "Go to Untitled project (unsafe)"

Click "Allow".

Click "Allow"

After the script finishes executing go back to the window with the Excel table. In the first column you will find all IDs. You can select the column and copy it.

Copy your IDs

If you want to save the IDs in a file instead of the Excel table use the following script:

function myFunction() {
  var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");
  var contents = folder.getFiles();
  var file;
  var text = "";
  while(contents.hasNext()){
    file = contents.next();
    text += "\"" + file.getId() + "\",\n";
  }
  folder.createFile("_IDs.txt", text, MimeType.PLAIN_TEXT);
};

This script will create a text file (_IDs.txt) in the folder. This text file will contain all IDs of the files.

Published: 16. Jul 2020
Categories: Google

Comments

  • Roman 1 year ago

    Hello!
    Thank you very much for your great job! I like your script "JavaScript gallery with files stored on Google Drive". Nice way to host images on google drive ans save on hosting cost.
    It works great for me, but... I really need it to open/play 360 images. I'm a 360 photography hobbyist.
    I found a special opensource script made by Google VR (link below) to open any 360 image in 360 player, so you see not just a flat image, but volumetric.
    But I'm not very good in coding to combine your script with that. The idea is easy (for you i guess): when user clicks on any image on (your) gallery it opens that Google VR player instead of your player. May you issue a v2 of your script to open images in Google VR player please?

    https://github.com/googlearchive/vrview/tree/gh-pages

    / Roman

    • tcnet 1 year ago

      I'm sorry, but I've not the time at the moment to look into that. But as I can see, the script you mentioned is no more updated for 4 years. Nowadays, it makes no more sense to work with third party source if they no more maintained.