Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frantzmiccoli/0617a013c96e1837eb7c7951e3702144 to your computer and use it in GitHub Desktop.
Save frantzmiccoli/0617a013c96e1837eb7c7951e3702144 to your computer and use it in GitHub Desktop.
Funnily enough you can't duplicate a folder which contains Google Documents
function duplicateRootFolder() {
var templateFolder = DriveApp.getFolderById('PUT YOUR TEMPLATE FOLDER ID HERE (FROM YOUR BROWSER URL)');
var targetParent = DriveApp.getFolderById('PUT YOU PARENT FOLDER ID HERE (FROM YOUR BROWSER URL)');
duplicateFolder(templateFolder, targetParent, false);
}
function duplicateFolder(templateFolder, targetParent, useTemplateName) {
var newFolderName = templateFolder.getName();
if (!useTemplateName) {
var dt = new Date();
var dtstring = dt.getFullYear()
+ '-' + pad(dt.getMonth() + 1, 2);
var newFolderName = dtstring + '-0000____ PENDING DUPLICATE';
}
var newFolder = targetParent.createFolder(newFolderName);
// we copy all files
var files = templateFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
file.makeCopy(file.getName(), newFolder);
}
var subTemplateFolders = templateFolder.getFolders();
while (subTemplateFolders.hasNext()) {
var subTemplateFolder = subTemplateFolders.next();
Logger.log(subTemplateFolder.getName());
duplicateFolder(subTemplateFolder, newFolder, true);
}
}
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment