Last active
February 15, 2020 06:37
-
-
Save nflint/ec354561cc0e2eac07a41aeee51d6fcd to your computer and use it in GitHub Desktop.
List all files in a given folder on Google Drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* /** | |
* Google Apps Script - List all files & folders in a Google Drive folder, & write into a speadsheet. | |
* - Main function 1: List all folders | |
* - Main function 2: List all files & folders | |
* | |
* Hint: Set your folder ID first! You may copy the folder ID from the browser's address field. | |
* The folder ID is everything after the 'folders/' portion of the URL. | |
* | |
* @version 1.0 | |
* @see https://gist.github.com/mesgarpour | |
*/ | |
// TODO: Set folder ID | |
var folderId = 'YourFolderId'; | |
// Main function 1: List all folders, & write into the current sheet. | |
function listFolders(){ | |
getFolderTree(folderId, false); | |
}; | |
// Main function 2: List all files & folders, & write into the current sheet. | |
function listAll(){ | |
getFolderTree(folderId, true); | |
}; | |
// ================= | |
// Get Folder Tree | |
function getFolderTree(folderId, listAll) { | |
try { | |
// Get folder by id | |
var parentFolder = DriveApp.getFolderById(folderId); | |
// Initialise the sheet | |
var file, data, sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.clear(); | |
sheet.appendRow(["P#","Document","Category","topic","type","Manual?","Audience","Description","File Path","Date","Last Updated","MIME Type","Size","ID"]); | |
// Get files and folders | |
getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll); | |
} catch (e) { | |
Logger.log(e.toString()); | |
} | |
}; | |
// Get the list of files and folders and their metadata in recursive mode | |
function getChildFolders(parentName, parent, data, sheet, listAll) { | |
var childFolders = parent.getFolders(); | |
// List folders inside the folder | |
while (childFolders.hasNext()) { | |
var childFolder = childFolders.next(); | |
// Logger.log("Folder Name: " + childFolder.getName()); | |
data = [ | |
'', //Blank Column for #P (Priority) | |
'=HYPERLINK("' + childFolder.getUrl() + '","' + childFolder.getName().replace(/"/gi,'') + '")', //Sets Title as anchor text | |
'', //Blank Column for Category | |
'', //Blank Column for Topic | |
'', //Blank Column for Type | |
'', //Blank Column for Manual | |
'', //Blank Column for Audience | |
childFolder.getDescription(), //Get Folder Desc | |
parentName + "/" + childFolder.getName(), | |
childFolder.getDateCreated(), //Get Folder Creation Date | |
childFolder.getLastUpdated(), //Get Folder Last Updated Date | |
'', //Leave Blank for since we cannot get mime type.TODO: FIX. | |
childFolder.getSize(), //Get Size (Will only show for .zip, .pdf, .jpeg, etc.) | |
childFolder.getId() //Get ID - useful for cross reference or future upload. | |
]; | |
// Write | |
sheet.appendRow(data); | |
// List files inside the folder | |
var files = childFolder.getFiles(); | |
while (listAll & files.hasNext()) { | |
var childFile = files.next(); | |
data = [ | |
'', //Blank Column for #P (Priority) | |
'=HYPERLINK("' + childFile.getUrl() + '","' + childFile.getName().replace(/"/gi,'') + '")', //Sets Title as anchor text | |
'', //Blank Column for Category | |
'', //Blank Column for Topic | |
'', //Blank Column for Type | |
'', //Blank Column for Manual | |
'', //Blank Column for Audience | |
childFile.getDescription(), //Get File Desc | |
parentName + "/" + childFolder.getName() + "/" + childFile.getName(), //Get File Path | |
childFile.getDateCreated(), //Get File Creation Date | |
childFile.getLastUpdated(), //Get File Last Update | |
childFile.getMimeType().replace('application/vnd.',''), //Get File Type clean it up to be a little more human readable. | |
childFile.getSize(), //Get Size (Will only show for .zip, .pdf, .jpeg, etc.) | |
childFile.getId() //Get ID - useful for cross reference or future upload. | |
]; | |
// Write | |
sheet.appendRow(data); | |
} | |
// Recursive call of the subfolder | |
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment