Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Script test for file existance

I can't believe there isn't a simple way to do this, but I've trawled everywhere for a working solution without success.

I am running a Google script in a Google Sheet and I'm at a point in the code where I want to save the spreadsheet as a new filename. Before I do that I want to check that there isn't already one in existence (because I don't want to overwrite it).

The code below that I'm using to test returns "File Exists" regardless of whether the folder or the file exists or not.

As always, any help much appreciated:-)

Here's my test code.

function testscriptforfileexistance() {
  filecheck = checkfileexists("UnderDevelopment",'20160919 Weekly Roster v1.0.gsheet');

  if (filecheck.filefound = true)  {
    Browser.msgBox("file found") ;
  } else {
    Browser.msgBox("File not found");
  }
}


function checkfileexists(foldername,fn) {
  var destFolder = DriveApp.getFoldersByName(foldername);
  filefound=false;
  var destFileId = DriveApp.getFilesByName(fn);
//if hasnext returns false, presumably it doesn't exist
  if (!destFileId.hasNext) {
    Logger.log ("Found");
    filefound = true;
// and as a second test, if the length is zero the file doesn't exist
  if (!destFileId[0]) {
    Logger.log ("zero length");
    filefound=false;
  }
  }
  return {filefound:filefound};
}

Cheers ...Steve

like image 896
SteveParry Avatar asked Dec 02 '22 14:12

SteveParry


1 Answers

This is a little bit confusing because there is a lot of hasNext involved if you are checking the existence of Folder/File or file in specific folder. I got this working thru trial and error.

hasNext()

  • Determines whether calling next() will return an item.

Return

  • Booleantrue if next() will return an item; false if not

First here are some related SO question :

  • How do I check if a file exists (by name) in Google Drive?
  • Google Apps Script: Check upload file exist or empty

Here is a sample code:

function checkFile(filename){
  var results;
  var haBDs  = DriveApp.getFilesByName(filename)
  //Does not exist
  if(!haBDs.hasNext()){
  results =  haBDs.hasNext();
  }
  //Does exist
  else{
  results =  haBDs.hasNext();
  }
  Logger.log(results)
  return results;
}

function myFunction() {
  var nomeDB = "FILE_NAME";  
  if(checkFile(nomeDB) == true){
  Logger.log("File Found")
  }else{
  Logger.log("File Not Found")
  }

}

Checking your code, try changing !destFileId.hasNext to !destFileId.hasNext()

Additional information, if you will be checking if the file exist in a specific folder you will use the same flow but you will have to check if the folder too does exists (optional).

Here is a snippet for that:

function checkFile2(filename,foldername){
  
  var folder = DriveApp.getFoldersByName(foldername);
  
  Logger.log(folder.hasNext());

  //Folder does not exist
  if(!folder.hasNext()){
    
  Logger.log("No Folder Found");
  
  }
  //Folder does exist
  else{
    Logger.log("Folder Found")
    var file   = folder.next().getFilesByName(filename);
    if(!file.hasNext()){
       Logger.log("No File Found");
    }
    else{
       Logger.log("File Found");
    }
  }
  
}

Hope this helps!

like image 113
Mr.Rebot Avatar answered Jan 12 '23 19:01

Mr.Rebot