This is a really quick post for the greater good… I have been looking into a solution to satisfy a customer requirements (really strange ones) about their need to automatically create daily versions of all files on a certain G Suite account they have…
The good thing about this is they are all using the Business license (the unlimited Drive storage) so it does not matter really how large the files goes on the Drive. I just need to make sure the destination folder does not get downloaded for offline access (hence the need for archiving).
I checked for some samples of code around and I found a lot of scripts with only one common problem: They cannot work well with sub-folders… So I tried to create my own and I came up with this script…
This will work with files and folders and sub-folders well. It will copy first any files that are existing in the root of the source folder, then work its way through the sub-files and folders… All you need is to follow the instructions at the end of this post to get it running:
function run() { Logger.clear(); var rootFolder=DriveApp.getRootFolder(); // getting the My Drive folder var todayStr= new Date(); // getting the date/time string var backupRootFolder=DriveApp.getFolderById("FOLDERID"); // the root folder where the backup folders will be created var backupFolder=backupRootFolder.createFolder(todayStr); // the folder which we will put the content in (destination) var dataFolder=DriveApp.getFolderById("FOLDERID"); // the data source folder which we want to copy to other location var destinationFolder=DriveApp.getFolderById(backupFolder.getId()); Logger.log("Root folder is:" + rootFolder); // My Drive folder Logger.log("Data root folder is:" + dataFolder); // Data root folder (source folder) Logger.log("Backup root folder is:" + backupRootFolder); // Backup root folder Logger.log("Backup folder is:" + backupFolder); // Backup folder Logger.log("Destination folder is:" + destinationFolder); // Destination folder Logger.log("============================================"); try { Logger.log("top folder:" + dataFolder); Logger.log("============================================"); // first we will copy the root files copyRootFiles(dataFolder, destinationFolder); // working on subfolders and files getChildFolders(dataFolder, destinationFolder); } catch (e) { Logger.log(e.toString()); } }; function getChildFolders(parent, backupSpace) { var childFolders = parent.getFolders(); Logger.log("parent: " + parent); //copyRootFiles(parent, backupSpace); while (childFolders.hasNext()) { var childFolder = childFolders.next(); Logger.log("============================"); Logger.log("Backup space: " + backupSpace); Logger.log("============================"); //first we will copy the root files //copyRootFiles(parent, backupSpace); var newBackupSpace = backupSpace.createFolder(childFolder.getName()); var backupSpaceChild = DriveApp.getFolderById(newBackupSpace.getId()); Logger.log("Creating subfolder for backup: " + backupSpaceChild.getId() + "(" + backupSpaceChild.getName() + ")"); //Logger.log("Folder Name: " + childFolder.getName()); //Logger.log("Folder URL: " + childFolder.getUrl()); var files = childFolder.getFiles(); var file; var fileName; while (files.hasNext()) { // Print list of files inside the folder file = files.next(); fileName = file.getName(); file.makeCopy(fileName, backupSpaceChild); } // Recursive call for any sub-folders Logger.log("New data child folder: " + childFolder); Logger.log("New backup space subfolder: " + backupSpaceChild); getChildFolders(childFolder,backupSpaceChild); } }; function copyRootFiles(childFolder, backupSpaceChild) { var files = childFolder.getFiles(); var file; var fileName; while (files.hasNext()) { // Print list of files inside the folder file = files.next(); fileName = file.getName(); file.makeCopy(fileName, backupSpaceChild); } };
To run the script:
- Create a new script, copy and paste the code in it
- Save and enter a name for the project
- Click Resources -> Advanced Google services…
- Enable the Drive API, then save
- Select the function ‘run’, and run it
No responses yet