Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active June 12, 2024 06:57
Show Gist options
  • Save tanaikech/5e79afbc50be824e02c6ba138f905e5c to your computer and use it in GitHub Desktop.
Save tanaikech/5e79afbc50be824e02c6ba138f905e5c to your computer and use it in GitHub Desktop.
Move Folder including Files and Folders in Google Drive using Google Apps Script

Move Folder including Files and Folders in Google Drive using Google Apps Script

Abstract

This script automates moving a folder including files and folders between Google Drives (personal or shared) by recreating the folder structure and transferring files, overcoming limitations for shared drives.

Introduction

This report addresses a common challenge: efficiently moving folders, including their subfolders and files, between Google Drives, encompassing 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.

IMPORTANT

This script automates moving folders and files within your Google Drive, including those in shared drives. Use caution when running the script, as it modifies your file structure.

To ensure safe testing, utilize a sample folder and files. If you possess a large number of folders and files, consider separating them into smaller groups for individual testing. Unfortunately, limitations within my environment prevent me from thoroughly testing the script with extensive data volumes.

Issue and workaround

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:

  1. Retrieving Folder Structure: It meticulously extracts the complete folder hierarchy from the source folder, capturing all subfolders at every level.
  2. Recreating Folder Structure: Leveraging the retrieved information, the script meticulously replicates the entire folder structure within the destination drive.
  3. File Movement: Individual files are efficiently transferred from the source folder to their corresponding locations within the newly created folder structure in the destination drive.
  4. Source Folder Removal (Optional): Once the files are safely relocated, the script can optionally remove the original source folder to ensure a clean and organized workspace.

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.

Usage

1. Create a Google Apps Script project

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.

2. Install a Google Apps Script library

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.

Please install the library to your Google Apps Script project with the script editor. Ref The project key for installing this library is as follows.

1UEyIfxDTat6GYRFy5iJ3UGj2QpyVuuQI5i-BsOcHDMr8HadIWailwj4k

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 copy and paste the script from the repository to your script editor. By this, you can use it. At that time, please install a library BatchRequest.

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.

3. Enable Drive API

This script uses Drive API. So, please enable Drive API at Advanced Google services. Ref

4. Sample script

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.

5. Options of this library

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.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment