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.

up vote 3 down vote accepted

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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