0

I am trying to transfer ownership of all my .pdf files to another account with more space. I am testing the code with a single folder in my drive.

function transfer() {
  var user = Session.getActiveUser();
  var folder = DriveApp.getFolderById('123folder-id456789-VxdZjULVQkPAaJ');
  var files = folder.getFilesByType(MimeType.PDF);
  while (files.hasNext()) {
    var file = files.next();
    if (file.getOwner() == user) file.setOwner('example@gmail.com');
  }
}

When I run the code, none of the files change ownership.

Sara
  • 85
  • 1
  • 1
  • 6

2 Answers2

1

How about this modification?

Modification points:

In your script, it tries to compare the objects of Session.getActiveUser() and file.getOwner(). I think that this is the reason of your issue.

So how about this modification? Please think of this as just one of several answers.

Modified script:

function transfer() {
  var user = Session.getActiveUser().getEmail();  // Modified
  var folder = DriveApp.getFolderById('123folder-id456789-VxdZjULVQkPAaJ');
  var files = folder.getFilesByType(MimeType.PDF);
  while (files.hasNext()) {
    var file = files.next();
    if (file.getOwner().getEmail() == user) file.setOwner('example@gmail.com');  // Modified
  }
}
  • In this modification, the emails are compared.

References:

If this didn't resolve your issue, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
0

Currently drive can't transefere the ownership of file that are not build-in, like pdf, zip, etc. So you must download them and reupload from the other account. I wrote a colab to do that without consume my bandwith. It can recursively transfere an entire folder with both build-in file types and other file types.

simone viozzi
  • 440
  • 5
  • 18