Skip to content

Instantly share code, notes, and snippets.

@BuildingAtom
Created May 31, 2017 20:31
Show Gist options
  • Save BuildingAtom/3ddd4a611e13ed7a2044c99b7ad1bce2 to your computer and use it in GitHub Desktop.
Save BuildingAtom/3ddd4a611e13ed7a2044c99b7ad1bce2 to your computer and use it in GitHub Desktop.
This is a short google apps script I made to transfer ownership of an entire folder (its files and subfolders included) to another google drive account. The script is fairly straightforward to use. Just fill in the folder ID, the new owner's email, and your current email; change the flags to your liking, and run the "runThisFunction" function. T…
/** Nice and easy to understand... almost. The excessive use of var is really
* for no reason other than to be excessive and annoy a friend. - Adam Li
* This only transfers google files, you have to manually transfer other files
* It transfers everything if you have a google apps account and they allow it.
*/
//This is the last part of the folder url
var folderId = "0000000";
//email address of the future owner
var newOwner = "someotheruser@somedomain.com";
//email address of the current owner
var yourEmail = "me@somedomain.com";
//if this is false, it just shows a log, otherwise it attempts to transfer ownership. View with Ctrl+Enter.
var transferOwner = true;
//These are the mimetypes of the google specific files that allow ownership tranfer.
//If you are in an organization and don't have a @gmail.com email, then you try changing "tryAllMimeTypes" to true
var validMimeTypes = ["application/vnd.google-apps.document", "application/vnd.google-apps.drawing", "application/vnd.google-apps.form", "application/vnd.google-apps.presentation", "application/vnd.google-apps.script", "application/vnd.google-apps.spreadsheet"];
var tryAllMimeTypes = false;
//Use this to see who owns what. you can also use this to see a log of all the files.
var logOnlyUserFiles = true;
//Sorry, no comments below. This was a quick script.
function runThisFunction() {
var rootFolder = DriveApp.getFolderById(folderId);
transfer(rootFolder);
}
function transfer(rootFolder) {
var folders = rootFolder.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
transfer(folder);
}
for (var i = 0; i < validMimeTypes.length; i++){
var Mime = validMimeTypes[i];
var files = rootFolder.getFilesByType(Mime);
if (tryAllMimeTypes){
files = rootFolder.getFiles();
i = validMimeTypes.length;
}
while (files.hasNext()) {
var file = files.next();
var owner = file.getOwner().getEmail();
var isOwner = owner === yourEmail;
if (isOwner && transferOwner){
file.setOwner(newOwner);
}
if (isOwner || !logOnlyUserFiles) Logger.log("File: " + file.getName() + " by " + file.getOwner().getEmail() + (isOwner && transferOwner ? " - OWNED" : ""));
}
}
var owner = rootFolder.getOwner().getEmail();
var isOwner = false;
if (owner === yourEmail && transferOwner){
isOwner = true;
rootFolder.setOwner(newOwner);
}
Logger.log("Folder: " + rootFolder.getName() + " by " + rootFolder.getOwner().getEmail() + (isOwner ? " - OWNED" : ""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment