0

Really, there is no way to move a file or folder using the DriveApp class?

From what I could gather on the docs and on the StackOverflow answered questions all proposed implementations seem to copy the file to another location and then delete the original file. That would result in at least two problems:

  • File/folder having a different folderId on the destination;
  • File/folder being duplicated and stored indefinitely on Google Vault by retention policy.

I must be doing something wrong. Why is there not a method to simply move the file/folder to another destination (as provided by the Drive Web UI)?

Thanks,

Cooper
  • 59,616
  • 6
  • 23
  • 54
ddutra
  • 1,459
  • 1
  • 14
  • 17
  • Folders are labels in Google Drive. The file ID doesn't change just because you added another label to the file. In Team Drive you have restrictions on the number of labels, to simulate a real filesystem hierarchy, but folders are still just labels. Use the Drive API for Team Drive moves. – tehhowch Jan 21 '19 at 16:00
  • 1
    Possible duplicate of [Google Team Drive Move file between team drive folders using Apps Script](https://stackoverflow.com/questions/49670353/google-team-drive-move-file-between-team-drive-folders-using-apps-script) – TheMaster Jan 21 '19 at 16:02
  • This will change a files parents (i.e. move it into another folder) :`Drive.Files.update({"parents": [{"id": newFolder.getId()}]}, file.getId(), null, {"supportsTeamDrives":true});` – Cooper Jan 21 '19 at 18:53
  • @tehhowch Ahh! So for Team Drive you have to use Drive API and not the DriveApp? This is all happening inside a Team Drive. I must find a way to move folder, files and all its child folders from one folder to another. When using DriveApp I can attach a target to a destination but I get an error when trying to detach the target from the old parent. – ddutra Jan 22 '19 at 10:37
  • @TheMaster Thanks. This is helpful. – ddutra Jan 22 '19 at 10:37

2 Answers2

0

When it comes to moving files with DriveApp, you need to work from the Folder rather than the File.

Open the folder you want to move the file to and use the "addFile(file)" method to add the file to that folder, then open the folder you want to move the file from and use "removeFile(file)" to remove it.

This might seem a bit clunky but it's actually possible to have a file in more than one folder at a time on Drive. The folders are really just labels. When considering a file that is already in multiple folders, the meaning of "moving" it to a new folder is kind of ambiguous, it's really a matter of adding/removing it from folders.

Add: https://developers.google.com/apps-script/reference/drive/folder#addfilechild

Remove: https://developers.google.com/apps-script/reference/drive/folder#removeFile(File)

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • Thanks. My ultimate goal is to move whole folders with all that is within the folder (files, more folders) recursively. I tried some suggested methods but could not remove the folder from the old parent because this is all happening inside a Team Drive. – ddutra Jan 22 '19 at 10:35
0

Stumbled across this post (two years & 6 months later) and just wanted to point out that its now possible to do this:

DriveApp.getFileById(myFileId).moveTo(DriveApp.getFolderById(myTargetFolderId));
RSX
  • 376
  • 4
  • 14