0

Hi I've got this Google App script that needs to overwrite a file. At the moment it creates a copy. Is there a line or two that can check if it exists,then delete? Or is there an alternative to createFile that overwrites?

DriveApp.getFolderById(fold).createFile(file.getName()+'-'+sheet+'.csv', csv);

Many thanks for looking!

1
  • if you know the id of the file open the file and set the contents to new data and you are done. Commented Nov 25, 2021 at 19:19

1 Answer 1

1

Filenames are not unique on Google Drive - the uniqueness of files is determined by their ID. Whereas on a regular file system, creating a file with a similar filename would erase the old file, in this case you are creating a new unique file every time.

As far as I know, the easiest way would the be to move the existing file to the trash. You can keep you existing script but add to it. Assuming your file will always exist, this should work:

const folder = DriveApp.getFolderById(fold);
folder.getFilesByName(file.getName()+'-'+sheet+'.csv').next().setTrashed(true);
folder.createFile(file.getName()+'-'+sheet+'.csv', csv);

If you are unsure that a file with that name will exist, or if there might be multiple files with that name, you will have to iterate through all them:

const folder = DriveApp.getFolderById(fold);
const files = folder.getFilesByName(file.getName()+'-'+sheet+'.csv');
while(files.hasNext()){
   let f = files.next();
   f.setTrashed(true);
}

folder.createFile(file.getName()+'-'+sheet+'.csv', csv);

I haven't tested this code, but it should be pretty close to what you need.

Edit Contrary to what I said, the method DriveApp.File.setContent(content) can overwrite the content of a file. The above solution still works and avoids potential data loss.

Sign up to request clarification or add additional context in comments.

4 Comments

Fantastic Sam! I copy and pasted both of those and they worked. :) The second one that iterates is perfect for me. Thank you so much!
Pretty concise about the solution and should work well. But would like to ask why you chose trashing instead of deleting the file via driveapp's method (forgot the name). Are they interchangeable or they differ on some behavior?
@ASyntuBU I'm sorry, you might have to be more specific since I I don't see a delete method in the DriveApp's documentation. I imagine that if one existed it would permanently delete the file whereas the setTrashed() method allows files to be removed from the trash.
Sorry @SamForger, I meant the advanced services of Drive. I am more familiar and frequently using Drive.Files.remove() instead. Description of the method is Permanently deletes a file by ID. Skips the trash. The currently authenticated user must own the file or be an organizer on the parent for shared drive files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.