title: Google Script to save attachment to GDrive, without duplicating
body: <p>Using Google Workspaces, I need a script to move an attachment from a daily email - with always the same sender, subject and attachment name - to a folder in Google Drive.</p>
  <p>I've tried several of the scripts found on here, which all move the attachment to drive - but when I set the periodic triggers for execution, all prior and already moved attachments are moved again and duplicates are created.
  How can i do this without having to delete either the email or the previous file?</p>
  <p>Below seems to half work - but I need this to only apply to NEW emails that haven't already had the attachments moved. Would also be great if there was a way to specify only the xls attachment but I don't know where to insert that bit</p>
  <pre class="lang-js prettyprint-override"><code>function myFunction() {
  var emailSubject = &quot;&quot;; // YOUR EMAIL SUBJECT
  var folderId = &quot;&quot;; //YOUR FOLDER ID (FOUND IN URL)
  
  var folder = DriveApp.getFolderById(folderId);
  
  var thread = GmailApp.search(emailSubject)[0];
  var message = GmailApp.getMessagesForThread(thread)[0];
  var attachments = message.getAttachments();
  
  for (var i = 0; i&lt;attachments.length; i++){
  folder.createFile(attachments[i].copyBlob());
  }
  }
  </code></pre>