4

I've a folder in my Google Drive and would like to get the file id of all the files against their file name in a spreadsheet.

Currently I have to open all the files in the folder individually and copy past the "File-Id" from the address bar to this spreadsheet (MasterSheet). In the MasterSheet I've set some import rules which imports automatically using the file ID.

I am unable to find a solution for this though it seems to be a very common requirement one can come across.

  • 1
    This will get you most of the way there. – Tom Woodward Nov 20 '15 at 2:44
5

I modified Tom Woodward's script to suite my need. Here it is:

   function listFilesInFolder(folderName) {

   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(["Name", "File-Id"]);


//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
    var folder = DriveApp.getFolderById("THIS_SHOULD_BE_YOUR_FOLDER_ID");
    var contents = folder.getFiles();

    var cnt = 0;
    var file;

    while (contents.hasNext()) {
        var file = contents.next();
        cnt++;

           data = [
                file.getName(),
                file.getId(),
            ];

            sheet.appendRow(data);
    };
};

I needed to get only the file name and its file-id, and the simplified version does that.

| improve this answer | |
0

If you need Folder-id not Files-id

function listFilesInFolder(folderName) {

   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(['Name','File-Id']);


//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
    var folder = DriveApp.getFolderById('your ID mother folder');
    var contents = folder.getFolders();

    var cnt = 0;
    var folder;

    while (contents.hasNext()) {
        var folder = contents.next();
        cnt++;

           data = [
                folder.getName(),
                folder.getId(),
            ];

            sheet.appendRow(data);
    };
}
| improve this answer | |
-1

Editor's note: This answer is explaining how to use the script shared in another answer

For other non-expert people like me, in Google Sheets:

  1. Go to tools>script editor
  2. Clear everything, then paste the script
  3. At the "THIS_SHOULD_BE_YOUR_FOLDER_ID" you have to insert your Folder ID (Just the ID, but leave the "". You can find the ID if you open your folder and check out the address.)

It should look like something like this:

https://drive.google.com/drive/u/2/folders/11O9VN_JHjfdadjkllÖKLKJDFLKJS

11O9VN_JHjfdadjkllÖKLKJDFLKJS - This is the ID (That is just an example)

  1. After that, save the script and just run it (next to the BUG icon), it will automatically paste the resolts in your Google Sheet
  2. It will probably ask for rights, so you have to type in your password

You can create different scripts, for different Folders, if you have many folders, like me.

| improve this answer | |
-1

use python with gam, and this script.

import os import subprocess

from csv import writer from csv import reader

path to googleidlist.csv

csvfile = 'c:\GAM\googleidlist.csv' destination = 'c:\GAM\OUTPUT\' #Destination for downloaded documents

Open the input_file in read mode and output_file in write mode

with open(csvfile, 'r') as read_obj, \ open('output_1.txt', 'w', newline='') as write_obj: # Create a csv.reader object from the input file object csv_reader = reader(read_obj) # Create a csv.writer object from the output file object csv_writer = writer(write_obj) # Read each row of the input csv file as list for row in csv_reader: file_id = row[0] outcome = subprocess.Popen(['gam', 'user', 'googleserviceaccountname', 'get', 'drivefile', 'id', file_id, 'targetfolder',destination], stdout=subprocess.PIPE) # os.system("gam user david.bruinsma@colonialmed.com show fileinfo "+ file_id + "name") filename = subprocess.Popen(['gam', 'user', 'googleserviceaccountname', 'show', 'fileinfo', file_id, 'name' ], stdout=subprocess.PIPE) output = outcome.stdout.readline() file_name = filename.stdout.readline() print(output) # Append the default text in the row / list # row.append(filename) row.append(output) row.append(file_name) row.append(file_id)

    # Add the updated row / list to the output file
    csv_writer.writerow(row)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.