gmail-processor icon indicating copy to clipboard operation
gmail-processor copied to clipboard

Overwrite Existing File

Open marcusjb opened this issue 6 years ago • 6 comments

If the file already exists in the GDrive Path how can we have it overwrite instead of creating a duplicate filename?

marcusjb avatar Apr 12 '18 07:04 marcusjb

It should work using file.setContent().

See the following links for more details:

  • https://developers.google.com/apps-script/reference/drive/file#setContent(String)
  • https://stackoverflow.com/questions/14965442/how-to-delete-overwrite-csv-file-using-google-apps-script

ahochsteger avatar Apr 13 '18 18:04 ahochsteger

Is it possible to update your script? I'm not good with programming at all.

UPDATE:

Thanks to StackOverFlow, the duplicate issue is now resolved! Just replace line 94 (as of the current version on GitHub):

var file = folder.createFile(attachment);

With the following:

      var fileName = attachment.getName();
      var f = folder.getFilesByName(fileName);
      var file = f.hasNext() ? f.next() : folder.createFile(attachment);

chaoscreater avatar Nov 08 '18 22:11 chaoscreater

At line 94 I replaced: var file = folder.createFile(attachment); with: var fileName = attachment.getName(); var f = folder.getFilesByName(fileName); var file = f.hasNext() ? f.next() : folder.createFile(attachment); However my Gmail2Gdrive script still is duplicating filenames on Gdrive instead of overwriting the existing files. Please advise.

soetheman avatar Dec 11 '18 01:12 soetheman

Sorry mate, I'm not a programmer, not sure why it's not working for you but it works for me. Have you tried saving your script?

Here's the question I posted on Stackoverflow, you might find it useful:

https://stackoverflow.com/questions/53221266/modify-google-app-script-gmail-to-google-drive/53221832?noredirect=1#comment93331540_53221832

chaoscreater avatar Dec 11 '18 03:12 chaoscreater

@chaoscreater Thank you for your feedback.

Did some more testing (*) with the Gmail2Gdrive script and noticed that several file system related issues arise:

  • Multiple files with the same name are present on Gdrive after time based TRIGGERING (**) of the Gmail2Gdrive script.
  • Overwriting an existing file seems the take place properly after EXECUTING the function Gmail2Gdrive from the browser script menu, in case a new file is processed from the Gmail inbox. The newest file is written correctly on Gdrive:
  • Deleting of an existing file takes place after running the function Gmail2Gdrive from the browser script menu (NO time based triggering) in case NO new file is present in email inbox. In this scenario however the oldest file is kept while the newest file(s) is/are being deleted ... (***):

Did you use time based triggering as well to run the script? For me this method is the only option to deploy it in an useful way.

*) I tested with both Gmail2Gdrive script modifications mentioned at https://stackoverflow.com/questions/53221266/modify-google-app-script-gmail-to-google-drive/53221832?noredirect=1#comment93331540_53221832

**) When you use simple triggers to run an Apps Script, it runs in a sandbox with reduced permissions. (Ref: Answer 1 @ https://stackoverflow.com/questions/26661545/createfile-in-google-apps-script-is-not-functioning-properly

***) I'm not a programmer either. But I would expect some more clever behavior from the code (or the file system?) just overwriting a duplicated file name :-)

Thanks,

soetheman avatar Dec 11 '18 11:12 soetheman

Hi mate,

From what I understand, the 3 lines of code translated to plain English is this:

  • Check whether the filename exists in the folder. If exist, skip, otherwise create the file.

So based on that, I don't think it creates the file and then deletes the file. I think it simply creates only when the filename is unique.

And yes, I do have a time based trigger for my Google Script. Kind of pointless if you were to run it manually?

chaoscreater avatar Dec 12 '18 00:12 chaoscreater

I'm closing this issue since GMail2GDrive is discontinued and has been superseded by the much improved Gmail Processor. It allows to specify conflict resolution strategies among which 'overwrite' is the one you were looking for. See the Getting Started Guide as well as the Reference Docs for more information

ahochsteger avatar Sep 20 '23 21:09 ahochsteger