Last active
March 19, 2018 16:46
-
-
Save mbierman/1967a88a5c5d0ae805627f17aacc9946 to your computer and use it in GitHub Desktop.
This looks at an arbitrary directory, looks for any folders inside that directory, and recursively looks for files older than 'Days'. I use this to clean out a directory of security camera video.
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
| /* | |
| Version 1.0.1 | |
| Updated 03/19/2018 | |
| This looks at an arbitrary directory, looks for any folders inside that directory, and recursively looks for files older than 'Days'. | |
| I use this to clean out a directory of security camera video. | |
| To configure, set the folderID and the Days. | |
| Michael Bierman | |
| with thanks to Tim Garrett. | |
| */ | |
| function findAllFolders() { | |
| Logger.clear() | |
| /* Edit the folderID for the parent folder you want to monitor */ | |
| var folderId = "0BzROkcV_RG8kQTlrOVVWUEdvajQ"; | |
| var Days = 30 * 24 * 60 * 60 * 1000; | |
| var parentFolder = DriveApp.getFolderById(folderId); | |
| var childFolders = parentFolder.getFolders(); | |
| while (childFolders.hasNext()) { | |
| var folder = childFolders.next(); | |
| Logger.log( '<<<<<<<< %s >>>>>>>>', folder); | |
| var files = folder.getFiles(); | |
| while (files.hasNext()) { | |
| var fileName = files.next(); | |
| // var fileId = fileName.getId(); | |
| var fileDate = fileName.getDateCreated(); | |
| if (new Date() - fileName.getLastUpdated() > Days) { | |
| Logger.log('DELETING! %s', fileName); | |
| /* Deleting file */ | |
| fileName.setTrashed(true); | |
| } | |
| else { | |
| Logger.log('ignoring %s...', fileName); | |
| } | |
| } | |
| Logger.log(""); | |
| } | |
| Logger.log('End of run.'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment