T.Todua
T.Todua

Reputation: 56351

Google App - check if file exists already?

i use this code in upload form (google scripts):

var blob = form.myFile;    
var file = folder.createFile(blob);    
file.setDescription("Uploaded by " + form.myName);

however, i want to check if file exist with same name, then dont upload. How to do that?

Upvotes: 1

Views: 5033

Answers (1)

Vytautas
Vytautas

Reputation: 2286

Well that's simple enough. All you need to do is use the getFilesByName() and the use the hasNext() to see if any files were found:

var file = DriveApp.getFilesByName('name of file to upload')
var chk = file.hasNext()
if (chk === true) return 1

if a file with the name was found, then hasNext() will return true. That means that we need to stop the script. You can do throw instead of a return, but that is up to you. The whole thing can also be in one line

if DriveApp.getFilesByName('filename').hasNext() === true) return 1

Upvotes: 4

Related Questions