Created
August 10, 2023 16:29
-
-
Save mjherich/7ac38a4aadcdcd6c81aa63017585ab49 to your computer and use it in GitHub Desktop.
Apps Script: Daily CSV Archiver
This file contains hidden or 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
| /** | |
| * Archives CSV files that were created within the last 24 hours to a folder structure organized by year and month. | |
| * CSV files are renamed to avoid naming conflicts and then moved to the appropriate folder. | |
| * The original file is then trashed. | |
| * | |
| * | |
| Instructions to install the script and set it to run every day between midnight to 1 AM: | |
| 1. Open Google Drive and click on `New` > `More` > `Google Apps Script`. | |
| 2. Delete any code in the script editor and paste your script into the editor. | |
| 3. Click on `File` > `Save`. Name your project and click `OK`. | |
| 4. Click on `Edit` > `Current project's triggers`. | |
| 5. Click on `+ Add Trigger` in the bottom right corner. | |
| 6. Under `Choose which function to run`, select `archiveRecentCSVs`. | |
| 7. Under `Choose which deployment should run`, select `Head`. | |
| 8. Under `Select event source`, select `Time-driven`. | |
| 9. Under `Select type of time based trigger`, select `Day timer`. | |
| 10. Under `Select time of day`, select `Midnight to 1am`. | |
| 11. Click `Save`. | |
| 12. You may be prompted to authorize the script. Follow the prompts and grant the necessary permissions. | |
| 13. Your script will now run every day between midnight and 1 AM. | |
| * | |
| * | |
| */ | |
| function archiveRecentCSVs() { | |
| var files = DriveApp.getRootFolder().getFiles(); | |
| var archiveFolder = DriveApp.getFoldersByName("[99] Archive").next(); | |
| var csvsFolder = archiveFolder.getFoldersByName("[02] csvs").next(); | |
| var oneDayInMilliseconds = 24 * 60 * 60 * 1000; | |
| var totalArchived = 0; | |
| var archivedCSVs = []; | |
| while (files.hasNext()) { | |
| var file = files.next(); | |
| if ( | |
| file.getMimeType() === "text/csv" && | |
| file.getDateCreated() > | |
| new Date(new Date().getTime() - oneDayInMilliseconds) | |
| ) { | |
| var dateCreated = file.getDateCreated(); | |
| var year = dateCreated.getFullYear(); | |
| var month = dateCreated.getMonth() + 1; // JavaScript counts months from 0-11 | |
| var yearFolder = getOrCreateFolder(csvsFolder, year); | |
| var monthFolder = getOrCreateFolder(yearFolder, month); | |
| var newFileName = file.getName(); | |
| if (fileExists(monthFolder, newFileName)) { | |
| var i = 1; | |
| while (fileExists(monthFolder, "copy" + i + "_" + newFileName)) { | |
| i++; | |
| } | |
| newFileName = "copy" + i + "_" + newFileName; | |
| } | |
| var newFile = file.makeCopy(newFileName, monthFolder); | |
| file.setTrashed(true); | |
| totalArchived++; | |
| archivedCSVs.push(newFile.getName()); | |
| } | |
| } | |
| Logger.log("Total CSVs archived: " + totalArchived); | |
| if (totalArchived > 0) | |
| Logger.log("Archived CSVs: " + archivedCSVs.join(", ")); | |
| } | |
| function getOrCreateFolder(parentFolder, name) { | |
| var folders = parentFolder.getFoldersByName(name); | |
| if (folders.hasNext()) { | |
| return folders.next(); | |
| } else { | |
| return parentFolder.createFolder(name); | |
| } | |
| } | |
| function fileExists(folder, name) { | |
| var files = folder.getFilesByName(name); | |
| return files.hasNext(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment