0

I found the following Google Apps Script that allows me the copy folders from MyDrive to MyDrive & SharedFolders to MyDrive:

function start() {

  var sourceFolder = "source";
  var targetFolder = "target";

  var source = DriveApp.getFoldersByName(sourceFolder);
  var target = DriveApp.createFolder(targetFolder);

  if (source.hasNext()) {
    copyFolder(source.next(), target);
  }

}

function copyFolder(source, target) {

  var folders = source.getFolders();
  var files   = source.getFiles();

  while(files.hasNext()) {
    var file = files.next();
    file.makeCopy(file.getName(), target);
  }

  while(folders.hasNext()) {
    var subFolder = folders.next();
    var folderName = subFolder.getName();
    var targetFolder = target.createFolder(folderName);
    copyFolder(subFolder, targetFolder);
  }  

}

I tried to amend it to copy folders from MyDrive to a Teamdrive but I fail like the complete noob I am. Could someone help me to update it for that purpose?

  • Ok, I'll try to edit the OP to explain what I tried. But I tried multiple different commands and all of them failing with different errors so I thought it would be better to not add all the codes I tried to use as that would be a huge messy post to read... – Juan Martin Jan 07 '19 at 14:32
  • 1
    You can probably avoid repeating the common setup code between various attempts you tried. Probably the most significant blocker is that Team Drive doesn't support moving folders from Google Drive to Team Drive even in the UI, due to the different ownership model, the different permission models associated with a folder, and the different layout restrictions (Google Drive uses folders as labels - a file belongs to one or more - while Team Drive uses them like a linkless file system - this file is in *only* **this** folder). – tehhowch Jan 07 '19 at 14:39
  • Take a look at [this](https://stackoverflow.com/a/49671170/7215091). – Cooper Jan 07 '19 at 18:03
  • it seems that despite is not possible to move folders from Google Drive to Team Drive in the UI, it's possible to do it with scripts (see my new comment below) – Juan Martin Jan 08 '19 at 19:51

2 Answers2

2

The below example will move the file to the folder specified (can be a team drive). Haven't got time to integrate it with your code at the moment but if I'm free later I'll post a better answer. Hope it helps

 //Move the new doc to the folder
  var fileId   = DriveApp.getFileById("<<FILEID>>");
  var folder = DriveApp.getFolderById("<<WHERE TO SAVE IT>>");
  var file = DriveApp.getFileById(fileId);

  folder.addFile(file);
James Sheard
  • 139
  • 9
  • thanks, hope you can help as I almost got it working as I wanted after adding a bit of your code (see my last comment), but I got stuck now and I can't make more progress – Juan Martin Jan 09 '19 at 09:11
  • @JuanMartin is there any progress on this? I have the same requirement and would be very interested to know if you finally figured it out. – fhcat Dec 03 '20 at 00:25
  • @fhcat no progress so I gave up – Juan Martin Jan 23 '21 at 17:16
0

just a little change and success.

function start() {

  var sourceFolder = "source";

  var source = DriveApp.getFoldersByName(sourceFolder);
  var target = DriveApp.getFolderById("ID");

  while (source.hasNext()) {
    copyFolder(source.next(), target);
  }

}

function copyFolder(source, target) {

  var folders = source.getFolders();
  var files   = source.getFiles();

  while(files.hasNext()) {
    var file = files.next();
    file.makeCopy(file.getName(), target);
  }

  while(folders.hasNext()) {
    var subFolder = folders.next();
    var folderName = subFolder.getName();
    var targetFolder = target.createFolder(folderName);
    copyFolder(subFolder, targetFolder);
  }  

}