Skip to content

Instantly share code, notes, and snippets.

@jedfonner
Created July 30, 2022 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedfonner/81df6ec57d41f1038cc60987df32ebb2 to your computer and use it in GitHub Desktop.
Save jedfonner/81df6ec57d41f1038cc60987df32ebb2 to your computer and use it in GitHub Desktop.
Transfer Google Files Between Accounts
function start() {
// Create a folder in your source and destination accounts called "To Copy" and put any document you want copied in there.
// Update the code below with the id of that folder
var copySource = DriveApp.getFolderById('id of source To Copy folder');
var copyDestination = DriveApp.getFolderById('id of destination To Copy folder');
// Create a folder in your source and destination accounts called "To Move" and put any document you want moved in there.
// Update the code below with the id of that folder
var moveSource = DriveApp.getFolderById('id of source To Move folder');
var moveDestination = DriveApp.getFolderById('id of destination To Move folder');
transferFolder(copySource, copyDestination, false);
transferFolder(moveSource, moveDestination, true);
}
function transferFolder(source, target, doMove) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
var dest = target.getFilesByName(file.getName());
if (dest.hasNext()) {
// skip because file exists
Logger.log('skipping copy for ' + file.getName() + ' because it exists.');
continue;
}
if (doMove) {
Logger.log("Moving file: " + file.getName());
file.moveTo(target);
} else {
Logger.log("Copying file: " + file.getName());
file.makeCopy(file.getName(), target);
}
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var dest = target.getFoldersByName(folderName);
var targetFolder;
if (!dest.hasNext()) {
Logger.log('creating folder ' + folderName);
targetFolder = target.createFolder(folderName);
} else {
targetFolder = dest.next();
}
transferFolder(subFolder, targetFolder, doMove);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment