1

I have code to move files from one folder to another. It does not work on two folders in the **same shared drive*. I get the following error:

Exception: Cannot use this operation on a shared drive item.

function moveFilesFromFolderToFolder(sourceFolderID, destinationFolderID)
{
  var sourceFolder = DriveApp.getFolderById(sourceFolderID);
  var destinationFolder = DriveApp.getFolderById(destinationFolderID);

  var sourceFiles = sourceFolder.getFiles();

  while(sourceFiles.hasNext())
  {
    var file = sourceFiles.next();

    destinationFolder.addFile(file);
    sourceFolder.removeFile(file);
  }
}

I know I can make a copy in the destination folder but I don't want to do that. I'm not sure why I can't move files between folders in the same shared drive. Very strange...

DriveApp.getFolders() returns folders from Shared Drives so DriveApp does work on Shared Drives.

https://developers.google.com/apps-script/reference/drive/permission has references to shared drives.

Nowhere does the DriveApp documentation say it does not work on Shared Drives. So it doesn't make sense that DriveApp wouldn't work on Shared Drives. I can do almost everything else with it on shared drives, like create folders, copy files, etc. So why would I not be able to do this one thing.

I am pretty sure this is a bug but wanted to check here if I am missing something obvious.

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
  • You may have to use the advanced [Drive Service](https://developers.google.com/apps-script/advanced/drive), not sure the DriveApp has support for shared drives. With the service and following [these steps](https://developers.google.com/drive/api/v3/enable-shareddrives) you could achieve what you are looking for. – Aerials Jun 08 '20 at 15:32
  • @Cooper Why do you say this is a similar question? They are nothing alike. Mine has to do with copying files between folders in a shared drive and the one you linked is about a web-app and files getting put in the root folder. Mine deals with a specific error. Do I need to post a brand new question with exactly the same content as this one? – IMTheNachoMan Jun 08 '20 at 19:52
  • @Aerials The DriveApp documentation does have references to Shared Drives. It doesn't say anything about not being able to copy move files within the same shared drive. I can use the Drive service if I have to but I feel like either this should work or Google would have said something about it in the documentation. – IMTheNachoMan Jun 08 '20 at 19:54
  • I'm not sure how to do it with Apps Script, but [this answer](https://stackoverflow.com/a/58171105/4243927) shows you how to use the Drive Service. Can you link to the docs where Apps Script mentions Shared Drives? – Aerials Jun 09 '20 at 08:15
  • @Aerials https://developers.google.com/apps-script/reference/drive/permission has references to shared drives. Nowhere does the DriveApp documentation say it does not work on Shared Drives. So it doesn't make sense that DriveApp wouldn't work on Shared Drives. I can do almost everything else with it like create folders, copy files, etc. So why would I not be able to do this one thing. It doesn't make sense. – IMTheNachoMan Jun 09 '20 at 12:15
  • Can you please clarify which line of code is throwing the error? – Aerials Jun 09 '20 at 15:29
  • @Aerials It errors on the `destinationFolder.addFile(file);` line. It does not like trying to add an existing file to a drive. You can make a copy of an existing file but not add an existing one. I even tried to remove the existing file from all of the parents and then add it but that doesn't work either. – IMTheNachoMan Jun 09 '20 at 18:41
  • I'm unable to reproduce your error. I was successful in adding the Files to a shared Drive folder using your code. Did you check permissions for the user running the script? – Aerials Jun 15 '20 at 12:22
  • You were able to move files from one folder in a shared drive, to another folder in a shared drive? I wonder if the account type matters? I know it isn't a permission issue. I'm on an Enterprise G-Suite account. How about you? – IMTheNachoMan Jun 17 '20 at 13:10
  • Me too, my account is G-Suite Enterprise. – Aerials Jun 17 '20 at 13:59
  • I just verified again, and I do get the issue. I supplied a folder Id that was not from the shared Drive – Aerials Jun 18 '20 at 15:02
  • So apparently it is a known issue/bug: https://issuetracker.google.com/issues/76201003. I know Shared Drives do not support files in multiple parents, like My Drive does, but that doesn't explain why it errors after removing the file from all parents. – IMTheNachoMan Jun 18 '20 at 15:47

1 Answers1

1

As @IMTheNachoMan points out this is a known issue, and I'm posting this answer with the link so that others who may face this issue, can go to the link and add a "star" next to the issue number for more visibility.

https://issuetracker.google.com/issues/76201003.

Aerials
  • 4,231
  • 1
  • 16
  • 20