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.


21 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
    Replies
    1. Cool, thanks for sharing.

      Kind regards
      Phil

      Delete
    2. How do you add that adaptation into the Google Sheets already made? Sorry, I am very inexperienced with using code.

      Delete
    3. Hi

      You would need to use the Apps Script Editor. Here is the Google site to get started with learning Apps Script: https://developers.google.com/apps-script

      Kind regards
      Phil

      Delete
  4. Hi, does this only work for google files? I am trying to change ownership for a folder of JPGs and it fails at the line to to set new owner.

    ReplyDelete
    Replies
    1. Hi

      No, I've just tested it on a folder of JPGs and it worked perfectly fine.

      Kind regards
      Phil

      Delete
  5. Hi there, thanks for the script! It doesn't seem to work with jpg. How did you make it work @phil?

    ReplyDelete
    Replies
    1. Hi

      You're welcome.

      As mentioned above it works fine for me with JPGs. The only time I've not seen it work (in general) is when changing the owner to an account that belongs to another domain (eg a business).

      Kind regards
      Phil

      Delete
  6. Hello, Exactly the script I am looking for, however I've made a copy and tried to run it and after giving authorization, I am given the following:

    This app is blocked.
    This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.

    Trying to change ownership of files from one Google Account to Another (both are no GSuite and just basic Google Accounts).

    Thoughts?

    ReplyDelete
    Replies
    1. Hi Mike

      I'm afraid this is not something I've come across - there are quite a few downloads of this tool. You're not signed into more than 1 Google account at the same time are you, which may be creating some confusion?

      How far do you actually get, to the point where you select your account and see the permissions it requires?

      Are you able to try the steps with another tool on the blog to see if it's just specific to this one?

      Kind regards
      Phil

      Delete
  7. Hi Phil,

    Im looking for something where I can change the current file owner to a different owner. can you help me with the piece of code.Thanks

    ReplyDelete
  8. but I don't want to give any details in the sheet, I will provide those details in the script

    var id = SpreadsheetApp.openById("1QoBtPq4o25Aw1MfgKFFbSSm4miAzYgd-nRB7MKJGy0g");

    var files = DriveApp.getFileById(id)
    var owner = files.getOwner().getEmail();
    Logger.log('owner: ' + owner);

    if (owner != 'abc.com') {
    files.setOwner('abc.com');
    Logger.log('File that was changed: ' + files);

    }

    ReplyDelete
    Replies
    1. Ok, well it looks like you have the code there that you need.

      Kind regards
      Phil

      Delete