I put together a script which goes through all of the files in Google Drive, and writes a spreadsheet with all of the files with their name, containing folder, URL, and date last updated.
function Start_Up() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
sheet.clear();
sheet.appendRow(['Name', 'Folder', 'URL', 'Updated'])
var start_time = new Date();
MailApp.sendEmail({
to: 'insert e-mail',
subject: "Master File Update - Started",
htmlBody: "The update of the master equipment document spreadsheet was started at: " + start_time});
var Script_Properties = PropertiesService.getScriptProperties();
Script_Properties.deleteAllProperties();
Iterate_Files();
}
function Delete_Triggers() {
var Triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < Triggers.length; i++) {
ScriptApp.deleteTrigger(Triggers[i]);
}
}
function Iterate_Files() {
var Script_Properties = PropertiesService.getScriptProperties();
var continuationToken = Script_Properties.getProperty('C_Token');
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
Delete_Triggers();
var Start_Time = new Date().getTime();
var Seconds = 0;
if (continuationToken) {
var files = DriveApp.continueFileIterator(continuationToken);
} else {
var files = DriveApp.getFiles();
}
var output = [];
while (files.hasNext() && Seconds < 240) {
var file = files.next();
var Current_Time = new Date().getTime();
var Seconds = Math.floor((Current_Time - Start_Time)/1000);
var file_name = file.getName();
if (file.getParents().hasNext()) {
var folder_name = file.getParents().next().getName();
} else {
folder_name = 'Root';
}
var file_url = file.getUrl();
var file_last_updated = file.getLastUpdated();
output.push([file_name, folder_name , file_url, file_last_updated]);
}
if (output.length) {
var last_row = sheet.getLastRow();
sheet.getRange(last_row +1, 1, output.length, 4).setValues(output);
}
if (files.hasNext()) {
var triggerID = null;
continuationToken = files.getContinuationToken();
Script_Properties.setProperty('C_Token', continuationToken);
ScriptApp.newTrigger('Iterate_Files')
.timeBased()
.after(1 * 60 * 1000)
.create();
} else {
Script_Properties.deleteAllProperties();
var end_time = new Date();
MailApp.sendEmail({
to: 'insert email',
subject: "Master File Update - Complete",
htmlBody: "The update of the master equipment document spreadsheet was completed at: " + end_time});
ScriptApp.newTrigger('Start_Up')
.timeBased()
.everyDays(1)
.atHour(0)
.create();
}
}
It currently runs through 50,000+ files in a large number of folders and I am hoping that some of you may see areas which I may be able to improve performance in order to reduce run time.
** The sheet does not necessarily need to be cleared and re-created as long as there is a viable method of tracking which files have been indexed previously.