Tomas Munkvold
Tomas Munkvold

Reputation: 61

Moving a folder (with its contents) with DriveApp

DriveApp does not have a "move"-function (as of today). How can I move this folder (with all its contents as it is)?

I wish to move the source_folder (which currently resides in parent_folder)

var source_Folder = DriveApp.getFolderById(source_id);
var parent_Folder = DriveApp.getFolderById(parent_id);

and put it inside target_folder:

var target_folder = DriveApp.getFolderById(target_id);

I already have source_id,parent_id and target_id

Upvotes: 0

Views: 366

Answers (1)

Tomas Munkvold
Tomas Munkvold

Reputation: 61

I looked around a bit and there is similar questions/answer to this problem. I made this to (hopefully) make it easy for others with the same problem.

"Folders" in Google Drive work pretty much the same way as "Labels" in Gmail.

You may add a folder multiple places in Google Drive. It will still be only one folder (not a copy) so its contents are available from all the places where you added the folder.

To simulate moving the source_folder, you can:

1) first add the source_folder to the target_folder

target_folder.addFolder(source_folder);

2) remove the source_folder from its parent.

parent_folder.removeFolder(source_folder);

full example code: (note: you need the folder ID's)

function move_folder() { // id looks like this: 1XqH79csKkPMvTsCxMUzkkpURETKHJ
   var source_id = "[insert_folder_id]"; // folder you wish to move
   var parent_id = "[insert_folder_id]"; // where the folder is currently
   var target_id = "[insert_folder_id]"; // where you want to put the folder

   var source_Folder = DriveApp.getFolderById(source_id);
   var parent_Folder = DriveApp.getFolderById(parent_id);
   var target_Folder = DriveApp.getFolderById(target_id);

   target_folder.addFolder(source_folder);    // source_folder now 2 places
   parent_folder.removeFolder(source_folder); // cleanup
 }

how to get folder_id.

a good answer is also found here: Implement a folder move function in Google Dirve

Upvotes: 2

Related Questions