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) {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.setOwner(newOwner);
}
File Download
Download the Bulk change ownership of Google Drive items here. Please use 'File' > 'Make a copy' for your own version.
What is the format of "Source Folder ID"?
ReplyDeleteHi Jeffrey
DeleteIt'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
Thanks, I'll give it a try.
DeleteI 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
ReplyDeleteThanks 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
Hi
DeleteI'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
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
DeleteHi
DeleteYes 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
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.
ReplyDeletehttps://stackoverflow.com/a/54104948
Here's my adaptation of the bulk change script using the continuation token: https://gist.github.com/TurekBot/6da5acd27f92c86dc68d3f6588eeb499
Cool, thanks for sharing.
DeleteKind regards
Phil
How do you add that adaptation into the Google Sheets already made? Sorry, I am very inexperienced with using code.
DeleteHi
DeleteYou 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
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.
ReplyDeleteHi
DeleteNo, I've just tested it on a folder of JPGs and it worked perfectly fine.
Kind regards
Phil
Hi there, thanks for the script! It doesn't seem to work with jpg. How did you make it work @phil?
ReplyDeleteHi
DeleteYou'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
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:
ReplyDeleteThis 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?
Hi Mike
DeleteI'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
Hi Phil,
ReplyDeleteIm 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
Hi
DeleteYes this tool does that.
Kind regards
Phil
but I don't want to give any details in the sheet, I will provide those details in the script
ReplyDeletevar 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);
}
Ok, well it looks like you have the code there that you need.
DeleteKind regards
Phil
FYI this appears to not work for transferring between gmail accounts, nor for some kinds of file types. For src and dest gmail accounts, the file.setOwner call fails with "Access denied: DriveApp.". https://issuetracker.google.com/issues/228826345?pli=1 "won't fix: Following up here, this is the expected behavior as currently Drive does not support the changing of the ownership for items which are owned by gmail.com accounts."
ReplyDeleteAlso, for PDF files/etc you'll get "Exception: Action not allowed"
(this person saw the same issue: https://coderanch.com/t/749309/languages/Google-App-Script-setOwner-Fails)
Hi Ben
DeleteAh thanks for the information - we do use it internally on our domain. I see transferring ownership manually in Google Drive itself also prevents the action from occurring between gmail.com accounts.
That's weird re PDFs - as I've just been able to change 5 of them in a test folder I had (again on our domain).
Kind regards
Phil