-
-
Save teguh-santoso/3f42489cae0d59cb3aa103783624c41c to your computer and use it in GitHub Desktop.
Limit the number of backup folders
This file contains 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
function deleteOldSheets() { | |
var df = DriveApp.getFolderById("your-backup-folder-id"); | |
var arr = new Array(); | |
var folders = df.getFolders(); | |
while( folders.hasNext() ) { | |
var f = folders.next(); | |
arr.push( [ [ f.getId() ] , [ f.getLastUpdated() ] ] ); | |
} | |
arr.sort( sortFunction ); | |
for( var i=14 ; i<arr.length ; i++ ) | |
DriveApp.getFolderById( arr[i][0] ).setTrashed( true ); | |
}; | |
function sortFunction( a , b ) { | |
var aDate = new Date(a[1]); | |
var bDate = new Date(b[1]); | |
if ( aDate === bDate ) return 0; | |
else if ( aDate < bDate ) return 1; | |
else return -1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment