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
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.First here are some related SO question :
Here is a sample code:
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:
Hope this helps!
I found this question looking for a way to see if a file exists in a certain folder.
This is working for me. Checks if a file exists in a given folder by folder Id. I've confirmed that it returns false if there is no file with that name or if the matching file(s) aren't in the given folder. And returns true if the file does exist in the given folder.
Thanks @Mr Rebot (or @Mr-Rebot). Your checkfile2 code nearly worked.
I swapped over foldername and filename in your function call (just for compatibility with the rest of my calling code).
My test calling code now looks like this
With this, my MsgBox message always comes up “File Not Found” even if the file test.txt actually exists, although the log correctly shows [True, Folder Found, File Found]. So I'm assuming the boolean status wasn't being passed back to the calling function.
So I added a bit of explicit Boolean variable stuff and that fixed it, except I then found it only works for filenames without spaces (like ‘test.txt’).
Here’s the modified code that works with a filename search of 'test.txt' but always returns File Not Found and Log entries of [True, Folder Found, No File Found, false] with ‘20160919 Weekly Roster v1.0.gsheet’
I tried putting the parameters on the call to checklist2 in double quotes instead of single quotes but that made no difference.
Still don’t know why this is so hard in GAS – checking whether a file exists must be something done frequently I would have thought.
Appreciate any help Mr Rebot (and Tulio!):-)
Cheers ...Steve
If you have more than 1 file in your DropBox, it might always come back True with hasNext? Try testing it with getDateCreated? If it's null, you know it doesn't exist. You could then choose that as your False.