Forked from abubelinha/appsScript_ListFilesFolders_Mesgarpour.js
Last active
May 13, 2022 09:59
-
-
Save rah003/cd273369762ecc462b902d2f3f7504e9 to your computer and use it in GitHub Desktop.
[Google Apps Script] List all files & folders in a Google Drive folder, & write into a speadsheet
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 | |
* - Main function 3: List all files in current folder | |
* | |
* 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://github.com/mesgarpour | |
*/ | |
// TODO: Set folder ID | |
var folderId = 'yadda yadda yadda'; | |
// Main function 1: List all folders, & write into the current sheet. | |
function listFolders(){ | |
getFolderTree(folderId, false, true); | |
}; | |
// Main function 2: List all files & folders, & write into the current sheet. | |
function listAll(){ | |
getFolderTree(folderId, true, true); | |
}; | |
// Main function 3: List all files in current folder, but not subfolders | |
function listSingleFolder(){ | |
console.info("single folder") | |
getFolderTree(folderId, true, false); | |
}; | |
// ================= | |
// Get Folder Tree | |
function getFolderTree(folderId, listAll, listSubFolders) { | |
try { | |
// Get folder by id | |
var parentFolder = DriveApp.getFolderById(folderId); | |
// Initialise the sheet | |
var file, data, sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.clear(); | |
sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]); | |
// Get files and folders | |
getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll, listSubFolders); | |
} 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, listSubFolders) { | |
var childFolders = parent.getFolders(); | |
// List folders inside the folder | |
if (listSubFolders == true) { | |
while ( childFolders.hasNext()) { | |
var childFolder = childFolders.next(); | |
// Logger.log("Folder Name: " + childFolder.getName()); | |
data = [ | |
parentName + "/" + childFolder.getName(), | |
childFolder.getName(), | |
childFolder.getDateCreated(), | |
childFolder.getUrl(), | |
childFolder.getLastUpdated(), | |
childFolder.getDescription(), | |
childFolder.getSize() | |
]; | |
// Write | |
sheet.appendRow(data); | |
// Recursive call of the subfolder | |
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll); | |
} | |
} | |
// List files inside the folder | |
var files = parent.getFiles(); | |
while (listAll & files.hasNext()) { | |
var childFile = files.next(); | |
// Logger.log("File Name: " + childFile.getName()); | |
data = [ | |
parentName + "/" + parent.getName() + "/" + childFile.getName(), | |
childFile.getName(), | |
childFile.getDateCreated(), | |
childFile.getUrl(), | |
childFile.getLastUpdated(), | |
childFile.getDescription(), | |
childFile.getSize() | |
]; | |
// Write | |
sheet.appendRow(data); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment