//////////////////////////////////////////////////
function deleteOldSheets() {
var df = DriveApp.getFolderById("xxx_GOOGLE_DRIVE_FOLDER_ID_xxx");
var arr = new Array();
var files = df.getFiles();
while( files.hasNext() ) {
var f = files.next();
arr.push( [ [ f.getId() ] , [ f.getLastUpdated() ] ] );
}
arr.sort( sortFunction );
for( var i=100 ; i<arr.length ; i++ )
DriveApp.getFileById( 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;
};
And if you want to automatically run the above script daily, then in the script editor you can set the trigger for it.
NOTE: In the above script you'll need to replace the Google Drive folder Id.
This comment has been removed by the author.
ReplyDeletei place above script and run it but no changes?
ReplyDeleteThe Script runs just change the number of backup files you need to keep in my case i needed only one file so i changed the count and its running fine
Deletefor( var i=1 ; i<arr.length ; i++ )- My Script
for( var i=100 ; i<arr.length ; i++ ) - Author script
Try now and check
Hi. What (?)
ReplyDelete