Last active
February 10, 2025 15:33
-
-
Save KenjiOhtsuka/d4432b6d80ad2b81ab7c965de2a8a00d to your computer and use it in GitHub Desktop.
Google Apps Script to Copy Folder Recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This is a code of Google Apps Script for copying google drive folder content to other folder. | |
## Which situation the code resolve. | |
Google doesn't allow to move folder content to other folder which is managed by | |
other organization according to the policy of the GSUITE organization. | |
And, Google doesn't allow to change the content owner to the user in other | |
organizations. | |
For example, [email protected] can not move the folder to | |
shared folder managed by a GSUITE organization. | |
And, [email protected] can not change owner to the user in | |
a GSUITE user. | |
Even in such situation, a user can read the folder content so the user can | |
copy the content. | |
This script copies the folder to other folder recursively, | |
even if the destination folder is accessible and managed by other GSUITE | |
organization. | |
## Why it is created. | |
There is a webpage to copy a folder recursively but | |
the user in a GSUITE organization is sometimes prohibited | |
to use the script managed by others. | |
*/ | |
function myFunction() { | |
// put source folder ID. | |
const fromFolder = DriveApp.getFolderById('1234567ABCDEFGHIJKLMNOPQRSTUVWXYZ') | |
// put destination folder ID. | |
const toFolder = DriveApp.getFolderById('1234567abcdefghijklmnopqrstuvwxyz') | |
// copy the folder content recursively. | |
copy(fromFolder, toFolder) | |
} | |
function copy(fromFolder, toFolder) { | |
// copy files | |
var files = fromFolder.getFiles() | |
while (files.hasNext()) { | |
var file = files.next(); | |
var newFile = file.makeCopy(toFolder) | |
newFile.setName(file.getName()) | |
} | |
// copy folders | |
var folders = fromFolder.getFolders() | |
while (folders.hasNext()) { | |
var folder = folders.next() | |
var newFolder = toFolder.createFolder(folder.getName()) | |
copy(folder, newFolder) | |
} | |
} |
Hi, are you able to amend the script so it copies all files that have been shared with me to a specific folder?
@tomasvlk84
This script is quite old, so I'm unsure if it still works as intended.
Copying all shared files seems to be a different requirement.
Microsoft Copilot says:
function copySharedFiles() {
// Get the folder where you want to copy the files
var destinationFolder = DriveApp.getFolderById('DESTINATION_FOLDER_ID');
// Get the files in the Drive
var files = DriveApp.getFiles();
// Iterate through the files
while (files.hasNext()) {
var file = files.next();
// Check if the file is owned by someone else
if (file.getOwner().getEmail() !== Session.getActiveUser().getEmail()) {
// Copy the file to the destination folder
file.makeCopy(destinationFolder);
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Life savior! Many thanks!