Skip to content

Instantly share code, notes, and snippets.

@KorewaLidesu
Last active June 8, 2021 09:07
Show Gist options
  • Save KorewaLidesu/3028d14f4a5bd58bfaa227fddf970ae7 to your computer and use it in GitHub Desktop.
Save KorewaLidesu/3028d14f4a5bd58bfaa227fddf970ae7 to your computer and use it in GitHub Desktop.
List all Folders / Files in a specified folder (id)
// TODO: Set folder ID
var folderId = 'replace_your_folder_id';
// Main function 1. List folders
function listFolders(){
getFolderTree(folderId, false);
};
// Main function 2: List all 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);
var CurrentActive1 = SpreadsheetApp.getActiveSpreadsheet();
var clickme = '=HYPERLINK("' + "https://drive.google.com/drive/folders/" + parentFolder.getId() + '"; "Pixiv") ';
if (listAll == true){
var file, data, sheet = CurrentActive1.getSheetByName("LinkAll");
sheet.clear();
sheet.appendRow([clickme,"","Name", "URL", "Date", "Last Updated", "Description", "Size"]);
// Get files and folders
getChildFolders(parentFolder, data, sheet, listAll);
}
else{
var file, data, sheet = CurrentActive1.getSheetByName("LinkFolder");
sheet.clear();
sheet.appendRow([clickme,"","ID folder", "Created Date", "Last Updated", "Description"]);
// Get files and folders
getChildFolders(parentFolder, data, sheet, listAll);
}
} catch (e) {
Logger.log(e.toString());
}
};
// List folders inside the folder
function getChildFolders(parent1, data, sheet, listAll) {
var childFolders1 = parent1.getFolders();
while (childFolders1.hasNext()) {
var childFolder1 = childFolders1.next();
//Logger.log("Folder Name: " + childFolder.getName());
data = [
childFolder1.getName()
];
// Write
sheet.appendRow(data);
getChildFolders1(childFolder1, data, sheet, listAll);
}
}
// List folders inside the folder
function getChildFolders1(parent2, data, sheet, listAll) {
var childFolders = parent2.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var clickme = '=HYPERLINK("' + "https://drive.google.com/drive/folders/" + childFolder.getId() + '"; "' + childFolder.getId() + '") ';
// Logger.log("Folder Name: " + childFolder.getName());
if (listAll == true){
data = [
null,
childFolder.getName(), null, null,
clickme,
childFolder.getLastUpdated()
];
}
else{
data = [
null, childFolder.getName(),
clickme,
childFolder.getDateCreated(),
childFolder.getLastUpdated(),
childFolder.getDescription()
];
}
// Write
sheet.appendRow(data);
if (listAll == true){
listFilesInFolder(childFolder.getId());
}
}
}
function listFilesInFolder(id) {
var MAX_FILES = 50; //use a safe value, don't be greedy
var scriptProperties = PropertiesService.getScriptProperties();
var lastExecution = scriptProperties.getProperty('LAST_EXECUTION');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LinkAll");
var data;
if( lastExecution === null )
lastExecution = '';
var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
var iterator = continuationToken == null ?
DriveApp.getFolderById(id).getFiles() : DriveApp.continueFileIterator(continuationToken);
try {
for( var i = 0; i < MAX_FILES && iterator.hasNext(); ++i ) {
var file = iterator.next();
var dateCreated = formatDate(file.getDateCreated());
if(dateCreated > lastExecution)
processFile(file);
data = [
null,
null,
file.getName(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDateCreated(),
file.getLastUpdated(),
file.getDescription(),
file.getSize(),
];
sheet.appendRow(data);
sheet.appendRow([""]);
}
} catch(err) {
Logger.log(err);
}
if( iterator.hasNext() ) {
scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', iterator.getContinuationToken());
} else { // Finished processing files so delete continuation token
scriptProperties.deleteProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
scriptProperties.setProperty('LAST_EXECUTION', formatDate(new Date()));
}
}
function formatDate(date) { return Utilities.formatDate(date, "GMT", "yyyy-MM-dd HH:mm:ss"); }
function processFile(file) {
var id = file.getId();
var name = file.getName();
//your processing...
Logger.log(name);
}
@KorewaLidesu
Copy link
Author

KorewaLidesu commented Jun 8, 2021

Source:

How to use:

Rewrite this until you satisfied

P/s: Would like someone help me recode folder to batch instead, folder progress slower than file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment