MoveFolder
This is a Google Apps Script library for moving a folder including files and folders on Google Drive.
AI Summary
MoveFolder
Google Apps Script library that moves an entire Google Drive folder—including all sub-folders and files—between personal or shared drives.
Target Users
Intermediate Google Apps Script developers who migrate or reorganize large Drive folder structures across personal and shared drives in internal tools or automation scripts.
Problems Solved
Drive API cannot directly move folders into or out of shared drives, forcing developers to manually rebuild folder hierarchies and move files one-by-one; this library automates that complex process.
Tags
Main Features
Bypass shared-drive folder move restriction
Implements a multi-step copy-and-delete strategy that enables moving folders that Drive API normally forbids when shared drives are involved.
Automatic folder hierarchy recreation
Extracts the entire source folder tree and recreates it in the destination drive before relocating files, preserving the original structure.
Efficient batch processing
Integrates the BatchRequest utility to reduce API calls and speed up file transfers, even for large datasets.
Simple API surface
Execute with a single call `MoveFolder.run({srcFolderId, dstFolderId})`, with optional token and shared-drive override parameters.
Usage Examples
Move a folder between drives
This snippet moves a source folder—including all its sub-folders and files—to a destination folder that can belong to a shared drive. Replace the placeholders with your own IDs.
function moveFolderSample() {
const srcFolderId = "SOURCE_FOLDER_ID"; // Folder you want to move
const dstFolderId = "DEST_FOLDER_ID"; // Destination folder (can be in Shared Drive)
// Move the entire hierarchy in one call
MoveFolder.run({ srcFolderId, dstFolderId });
// After execution, the source folder will appear under dstFolderId.
}
Be sure to add the library with project key 1UEyIfxDTat6GYRFy5iJ3UGj2QpyVuuQI5i-BsOcHDMr8HadIWailwj4k
and enable the Drive API before running.