Tuesday, 7 April 2020

Bulk change ownership of Google Drive items

The following Google Apps Script is designed to bulk transfer ownership of Google Drive files/folders by iterating through them. It gets the current owner and compares that to the new designated owner before making the change. This particular example will retain the previous owner as an editor of the item.
Screenshot of transfer tool requirements
Screenshot of transfer tool requirements

A 'while' loop is used to cycle through and 'getFiles' in the first instance. From here we can get the email address of the current owner:
var currentFileOwner = file.getOwner().getEmail();
Using an 'if' statement we can then look for a match with the current owner specified on the Welcome sheet and if one is found the new owner can be set:
if (currentFileOwner == currentOwner) {
   file.setOwner(newOwner);
}
The process can then be repeated for sub-folders ('getFolders') which at the end we pass this folder ID back into the Function (hence creating a loop).


File Download

Download the Bulk change ownership of Google Drive items here. Please use 'File' > 'Make a copy' for your own version.


9 comments:

  1. What is the format of "Source Folder ID"?

    ReplyDelete
    Replies
    1. Hi Jeffrey

      It's the long string of characters in the Url that Google Drive uses for its ID. For example: https://drive.google.com/drive/folders/THIS PART IS THE ID

      Kind regards
      Phil

      Delete
    2. Thanks, I'll give it a try.

      Delete
  2. I checked it with the spreadsheet but it didn't work for me, then I went to the script editor, leaving the data in the spreadsheet and it worked without problems
    Thanks friend, I was looking for a solution to a code that I have for a long time but it stopped me with a time limit error

    ReplyDelete
    Replies
    1. Hi

      I've double checked the sheet/script and all seems to be fine. It won't run the very first time you authorise against your Google account (as Google makes you click to run it again as a security measure).

      Thank you for the feedback.

      Kind regards
      Phil

      Delete
    2. I ran into that same "Exceeded maximum execution time" error. If you've got so many files that it takes longer than 6 minutes, you'll need to try one of these strategies: https://stackoverflow.com/questions/14450819/google-app-script-timeout-5-minutes

      Delete
    3. Hi

      Yes you are correct about the limits (https://developers.google.com/apps-script/guides/services/quotas) at the time of developing this it was a small number of items and we had 30 minutes for execution.

      Kind regards
      Phil

      Delete
  3. Here's a script that takes into consideration the 6-minute time limit. It saves a continuationToken so you can start back up where you left off.

    https://stackoverflow.com/a/54104948

    Here's my adaptation of the bulk change script using the continuation token: https://gist.github.com/TurekBot/6da5acd27f92c86dc68d3f6588eeb499

    ReplyDelete