Google Drive – Get All Files’ File-ID from a Folder

google sheetsgoogle-apps-scriptgoogle-drive

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.

Best Answer

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.