Kunlun
Kunlun

Reputation: 53

Google Apps Script: How to automatically copy documents to a new folder?

I am trying to create a script for my classes that will copy 2 files (templates) as soon as I create a folder. Something like this:

  1. Create new folder /new_student/ in the folder Google_drive/customer_class_b/
  2. Copy the files student_guidelines.gdoc and student_acknowledgement.gdoc from Google_drive/resources to Google_drive/customer_class_b/
  3. Rename the files student_guidelines.gdoc and student_acknowledgement.gdoc to student_guidelines_class_b.gdoc and student_acknowledgement_class_b.gdoc

Result should be having 2 files: Google_drive/customer_class_b/student_guidelines_class_b.gdoc and Google_drive/customer_class_b/student_acknowledgement_class_b.gdoc

I tried to play with the scripts but I am struggling. Any idea of how we could do so?

Upvotes: 1

Views: 4442

Answers (1)

abielita
abielita

Reputation: 13469

As @Vasim said, we can help you better if you provided some code or error code that you encountered.

From this documentation, you can copy a file using the makeCopy() method; makeCopy(destination) to copy the file in the destination directory; makeCopy(name) to create a copy of the file and names it with the name provided; and makeCopy(name, destination) to copy the file in the destination directory and names it with the name provided.

Here is an example from Google apps script examples - Copy file and move to another folder:

function copyfile() {
var file = DriveApp.getFileById("1pkwQ9te-EtpqC_NC3BoHzOTUoC7axZDcAfxrqMgslwg");
var source_folder = DriveApp.getFolderById("0B8_ub-Gf21e-fkxjSUwtczJGb3picl9LUVVPbnV6Vy1aRFRWc21IVjRkRjBPTV9xMWJLRFU")
var dest_folder = DriveApp.getFolderById("0B8_ub-Gf21e-flJ4VmxvaWxmM2NpZHFyWWxRejE5Y09CRWdIZDhDQzBmU2JnZnhyMTU2ZHM")
// Make a backup copy.
var file2 = file.makeCopy('BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + file.getName());
dest_folder.addFile(file2);
source_folder.removeFile(file2);
}

You can check on these related threads:

Hope this helps!

Upvotes: 3

Related Questions