Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active March 19, 2018 16:46
Show Gist options
  • Select an option

  • Save mbierman/1967a88a5c5d0ae805627f17aacc9946 to your computer and use it in GitHub Desktop.

Select an option

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.
/*
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