This is a Google Apps Script library for moving a folder including files and folders on Google Drive.
This library addresses a common challenge: efficiently moving folders, including their subfolders and files, between Google Drives. This encompasses both personal and shared drives using a script. While Google Drive offers straightforward methods for moving individual files between any drives, directly moving entire folders containing subfolders presents limitations, particularly when shared drives are involved. This script bridges that gap by providing a reliable Google Apps Script solution for such scenarios.
Seeing a need, I created this script after receiving several requests. I believe it could be useful for other users, so I published it. If you find this helpful, I'm glad!
The report tackles the restriction of directly moving folders into or out of shared drives. When a direct move is attempted, the following error messages appear.
For Drive API
{
"error": {
"code": 403,
"message": "Moving folders into shared drives is not supported.",
"errors": [
{
"message": "Moving folders into shared drives is not supported.",
"domain": "global",
"reason": "teamDrivesFolderMoveInNotSupported"
}
]
}
}
For Drive service (DriveApp)
Exception: Cannot use this operation on a shared drive item.
It seems that this situation is the current specification. Ref and Ref This script in this report overcomes this limitation by employing a multi-step process:
By meticulously following these steps, the script effectively migrates entire folders, including subfolders and files, between any Google Drives, regardless of whether they are personal or shared drives. This report delves into the details of this Google Apps Script, providing a step-by-step explanation of its functionality and implementation.
In the above process, the folder IDs are changed because the folder structure is created for the destination folder while the file IDs are not changed.
In this report, Google Apps Script is used. Of course, the approach introducing this report can also be used in other languages.
Here, in order to test the following sample scripts, please create a standalone Google Apps Script project. Of course, this script can also be used with the container-bound script.
And, please open the script editor of the Google Apps Script project.
When I created a script for this, the script was a bit complicated. So, in order to easily use this script, I created it as a Google Apps Script library. Of course, you can see the actual script of this library at my repository.
1UEyIfxDTat6GYRFy5iJ3UGj2QpyVuuQI5i-BsOcHDMr8HadIWailwj4k
In order to use this library, please install this library.
1UEyIfxDTat6GYRFy5iJ3UGj2QpyVuuQI5i-BsOcHDMr8HadIWailwj4k
.This library uses the following 2 scopes.
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/script.external_request
Also, this library also uses my library BatchRequest.
But, in the recent update on the Google side, it was found that in the current stage, when the other libraries are loaded from a library, an error like
We're sorry, a server error occurred while reading from storage. Error code NOT_FOUND
occurs. So, from v1.0.1, the library of BatchRequest is included in this library.
Of course, when you want to directly use the script of this library without installing the library, you can also achieve it. In that case, please do the following steps.
MoveFolder.run({ srcFolderId, dstFolderId })
.This script uses Drive API. So, please enable Drive API at Advanced Google services. Ref
After the library is installed and Drive API is enabled, you can test this script. Please copy and paste the following script to the script editor installing the library.
Please set the source folder ID and the destination folder ID.
function myFunction() {
const srcFolderId = "###";
const dstFolderId = "###";
MoveFolder.run({ srcFolderId, dstFolderId });
}
When you run the function myFunction
, the folder of the folder srcFolderId
is moved to the folder dstFolderId
.
This library has the simple options. These can be used in the object of the argument of run
method.
srcFolderId
: Folder ID of the source folder.dstFolderId
: Folder ID of the destination folder.accessToken
: Default is ScriptApp.getOAuthToken()
. For example, when you want to use the service account, you can use the access token from the service account.forSharedDrive
: Default is false. When this is true, the process mentioned in the "Issue and workaround" section is forcibly run.v1.0.0 (June 6, 2024)
v1.0.0 (June 10, 2024)
1UEyIfxDTat6GYRFy5iJ3UGj2QpyVuuQI5i-BsOcHDMr8HadIWailwj4k
again.v1.0.1 (June 18, 2024)
We're sorry, a server error occurred while reading from storage. Error code NOT_FOUND
occurs. So, from v1.0.1, the library of BatchRequest is included in this library.v1.0.2 (June 18, 2024)
appsscript.json
. In this version, it was updated.