Rocketboy235 Rocketboy235 - 3 months ago 17
Javascript Question

Google Script / JavaScript: Missing ) after argument list

I'm currently working on trying to automate deletion of security video files after they are past 30 days old for my own home security project. When I try running the script,
I get a "Missing ) after argument list" for line 67 which is

var files = DriveApp.searchFiles(
'modifiedDate < "' + cutOffDateAsString + '" and (name contains 'Driveway' or name contains 'Kitchen' or name contains 'FrontDoor')');


I'm not really sure what I am doing wrong as I did follow the example that the Google Documentation stated for compound clauses:

modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')


I looked at similar questions that were asked but I'm not sure if they applied to mine so I decided to ask my own here.

Here is my script for reference:

function getFilesByDate() {
var arrayOfFileIDs = [];

var ThirtyDaysBeforeNow = new Date().getTime()-3600*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
//needed in yr-month-day format

var cutOffDate = new Date(ThirtyDaysBeforeNow);
var cutOffDateAsString = Utilities.formatDate(cutOffDate, "GMT", "yyyy-MM-dd");
Logger.log(cutOffDateAsString);

var theFileID = "";

//Create an array of file ID's by date criteria
var files = DriveApp.searchFiles(
'modifiedDate < "' + cutOffDateAsString + '" and (name contains 'Driveway' or name contains 'Kitchen' or name contains 'FrontDoor')');
//This is LINE 67

while (files.hasNext()) {
var file = files.next();
theFileID = file.getId();

arrayOfFileIDs.push(theFileID);
Logger.log('theFileID: ' + theFileID);
Logger.log('date last updated: ' + file.getLastUpdated());
}

return arrayOfFileIDs;
Logger.log('arrayOfFileIDs: ' + arrayOfFileIDs);

};


Thanks for your help guys!

Answer

You need to escape your quotes. Replace this:

'" and (name contains 'Driveway' or name contains 'Kitchen' or name contains 'FrontDoor')'

with this:

'" and (name contains \'Driveway\' or name contains \'Kitchen\' or name contains \'FrontDoor\')'

because right now, your String looks like this to JS:

modifiedTime > "2016-08-15T12:03:54" and (name contains