1

i want to import public google folder/files using folder/files ID. i am not sure if it's possible.

lets say my google drive folder ID is "mydriveid" public folder id is "publicfolderid"

i want to import public folder to my folder. i tried following script but i got error

Exception: Access denied: DriveApp. copyfile @ Code.gs.6

function copyfile() {
var file = DriveApp.getFileById("File ID");
var source_folder = DriveApp.getFolderById("publicfolderid")
var dest_folder = DriveApp.getFolderById("mydriveid")
// 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);
}
firekid2018
  • 143
  • 11
  • 1
    Do you have write access to `source_folder`? You can make the copy in `dest_folder` using a single [`makeCopy(name, destination)`](https://developers.google.com/apps-script/reference/drive/file#makecopyname,-destination) call. Doing so would simplify your code and resolve write-permission issues, if any. – Diego Jul 17 '21 at 19:02
  • it's a public folder. anyone can have access. here is an example folder link: https://drive.google.com/drive/folders/1090WYPGlsDIiwm3vHF85EiUAEAbA5A4M – firekid2018 Jul 17 '21 at 19:57
  • 1
    I think you have no right to remove files from the public folder. So last line of your code should throw error anyway. – Yuri Khristich Jul 17 '21 at 19:59
  • @Yuri Khristich, thank you point it out. i still getting error :( – firekid2018 Jul 17 '21 at 20:07

1 Answers1

1

To copy a file you can use this code:

function copyfile() {
  var file = DriveApp.getFileById("fileID");
  var dest_folder = DriveApp.getFolderById("folderID")
  var name = 'BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + file.getName();
  file.makeCopy(name, dest_folder);
}

It works fine. I just tested it.

But to copy a folder with files (and subfolders with their files and subfolders, I suppose) it's a whole another idea. You will have to loop through all of them as well. Let me know if you really need it.

ale13
  • 5,679
  • 3
  • 10
  • 25
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • thank you so much, yes i also want to make copy folder script as well. it will be really helpful if you can help me. however i mark this comment "accept answer" – firekid2018 Jul 17 '21 at 20:19
  • 1
    Thank you for accepting. But actually this question is quite popular (how to copy folder) is answered many times already: https://stackoverflow.com/a/66473822/14265469 or https://stackoverflow.com/questions/35448703/google-apps-script-save-a-copy-of-shared-folders etc I hardly can to add something to this. Ask me if you don't get something it these examples – Yuri Khristich Jul 17 '21 at 20:29