-
Notifications
You must be signed in to change notification settings - Fork 0
/
Housekeeping-cp.js
157 lines (123 loc) · 6.27 KB
/
Housekeeping-cp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Script Title: Google Housekeeping
Version: 1.0
Creation Date: 5/12/2020
Author: Robert C. Tucker
Description: The Google Drive Housekeeping script is a google script designed to cull through a folder on Google Drive and delete any folders older than a specified date.
Notes: This Script must be run at script.google.com, unless you have a standard hosted google project with drive api access.
*/
//GetFolderByDate Function searches for all folders in a single parent folder and returns the Folder IDs in an array called arrFolderIDs.
function GetFolderByDate() {
//Declare all variables.
var arrFolderIDs = [];
var arrFolderNames = [];
var arrLastUpdated = [];
var arrListNames = [];
var arrSearchFolders = [];
var arrIgnoredFolders = [];
//Set Threshold date for removal.
var Threshold = new Date().getTime()-600*1000*24*30;
/*
30 is the number of days
(3600 seconds = 1 hour, 1000 milliseconds = 1 second, 24 hours = 1 day and 30 days is the duration you wanted
Must be in yr-month-day format.
This will give you your Cull Date or Date at which items need to be removed.
*/
var CullDate = new Date(Threshold);
var strCullDate = Utilities.formatDate(CullDate, "America/New_York", "MMMM-dd-yyyy");
var FolderID = "";
var FolderName = "";
var LastUpdated = "";
var strFolderNames = "";
var strRemovedFolderNames = "";
var strSearchFolders = "";
var strIgnoredFolders = "";
//Replace FolderIDs iin arrSearchFolderIDs with IDs of the folders you wish to search within. This can be obtained by going to your desired folder and copying everything in the URL after https://drive.google.com/drive/folders/
var arrSearchFolderIDs = ['FolderID'];
Logger.log('\n' + 'CullDate: ' + strCullDate + '\n');
for (var i = 0; i < arrSearchFolderIDs.length; i++) {
var SearchFolder = DriveApp.getFolderById(arrSearchFolderIDs[I]);
var SearchFolderName = SearchFolder.getName();
arrSearchFolders.push(SearchFolderName);
//Set Folder as a variable containing folders within search folder.
var Folders = SearchFolder.getFolders();
//While statment to search for all folders within the search folder.
while (Folders.hasNext()) {
var Folder = Folders.next();
FolderID = Folder.getId();
FolderName = Folder.getName();
LastUpdated = Folder.getLastUpdated();
//Replace Templates with folders you would like the script to ignore and not remove.
//Put the ID of each Folder into the arrFolderIDs array and the Name for each folder into the FolderNames Array.
if (FolderName != "Templates"){
if (LastUpdated < CullDate){
arrFolderIDs.push(FolderID);
arrFolderNames.push(SearchFolderName + '/' + FolderName);
arrLastUpdated.push(LastUpdated);
}
//Check if Last Updated date is later than the cull date. If so, push string to array(arrIgnoredFolders).
if (LastUpdated > CullDate){
arrIgnoredFolders.push(FolderName + 'Last Updated: ' + Utilities.formatDate(LastUpdated, "America/New_York", "MM-dd-yyyy"));
}
}
}
}
//Sort Arrays Alphabetically
arrIgnoredFolders.sort();
arrListNames.sort();
arrSearchFolders.sort();
//Place string of all Ignored folders into an string variable, strIgnoredFolders.
for ( i = 0; i < arrIgnoredFolders.length; i++){
strIgnoredFolders += '\n' + arrIgnoredFolders[i];
}
//Place string of all Search folders into an string variable, strSearchFolders.
for (var sf = 0; sf < arrSearchFolders.length; sf++){
strSearchFolders += '\n' + arrSearchFolders[sf];
}
//Place change date of all selected folders into an string variable, strChangeDate.
//Arrange Folder Name and Last Updated into array for later use.
for (i = 0; i < arrFolderNames.length; i++){
var strChangedDate = Utilities.formatDate(arrLastUpdated[i], "America/New_York", "MM-dd-yyyy");
arrListNames.push(arrFolderNames[f] + ' Last Modified: ' + strChangedDate);
}
//Convert array of Removed Folders (arrListNames) to string (strRemovedFolderNames).
for (i = 0; i < arrFolderIDs.length; i++) {
strRemovedFolderNames += '\n' + arrListNames[i];
}
Logger.log('\n' + '\n' + 'Ignored Folders: ' + strIgnoredFolders + '\n' + '\n');//List of Ignored Folders
Logger.log('\n' + '\n' + 'Search Folders: ' + strSearchFolders + '\n' + '\n'); //List of folders searched.
Logger.log('\n' + '\n' + 'Folders: ' + strRemovedFolderNames + '\n' + '\n'); //List of all Folders to be removed within Search Folder and Date last updated for each folder.
//For each value in array arrFolderIDs output Folder Name to be removed to log.
for (i = 0; i < arrFolderIDs.length; i++) {
strFolderNames += '\n' + arrFolderNames[i];
}
Logger.log('\n' + '\n' + 'Folders To Be Removed: ' + strFolderNames + '\n'); //List all Folders to be removed.
return arrFolderIDs;
};
//Funtion to Empty Trashcan.
function emptydrivetrash() {
Drive.Files.emptyTrash();
}
//Main Function to Delete old folders to trashcan per schedule.
function DeleteFolderByDate() {
//Retrieves arrFolderID array into an array variable arrayIDs
var arrayIDs = GetFolderByDate();
//Sets new Date for removal - Settings below show for 15 days after initial run. - triggered by seperate trigger in scripts dashboard.
var RunDate = new Date().getTime()+3600*1000*24*15;
var RemovalDate = new Date(RunDate);
var strRemovalDate = Utilities.formatDate(RemovalDate, "America/New_York", "MMMM dd, yyyy");
/*
30 is the number of days
(3600 seconds = 1 hour, 1000 milliseconds = 1 second, 24 hours = 1 day and 30 days is the duration you wanted
needed in yr-month-day format
*/
for (var i = 0; i < arrayIDs.length; i++) {
// Logger.log('arrayIDs[i]: ' + arrayIDs[i]);
var DelResponse = DriveApp.getFolderById(arrayIDs[i]).setTrashed(true);
}
//Send email to Administrator - Replace email with that of the desired contact. Email will come from the google account associated with the script.
var recipient = "[email protected]";
var subject = 'Folders to be Removed from Google Drive on ' + strRemovalDate;
var body = Logger.getLog();
MailApp.sendEmail(recipient, subject, body);
};