Skip to content

Instantly share code, notes, and snippets.

@leoherzog
Last active May 9, 2024 15:31
Show Gist options
  • Save leoherzog/3ff83641104da8d755aa0bab9cca4048 to your computer and use it in GitHub Desktop.
Save leoherzog/3ff83641104da8d755aa0bab9cca4048 to your computer and use it in GitHub Desktop.
Gather All Google Drive Files of a Certain Type and Change Owner
const newOwner = 'blah@example.com';
const fileTypesToTransfer = ['application/vnd.google-apps.form']; // https://developers.google.com/drive/api/guides/mime-types
function changeOwnership() {
console.log('Gathering all files in Drive of type ' + fileTypesToTransfer.join(',') + '...');
let fileIterator = DriveApp.searchFiles(fileTypesToTransfer.map(x => 'mimeType = "' + x + '"').join(' or '));
let files = [];
while (fileIterator.hasNext()) {
files.push(fileIterator.next());
}
console.log('Transferring ownership of ' + files.length + ' files...');
for (let file of files) {
try {
file.setOwner(newOwner);
}
catch(e) {
console.error('Problem updating ownership for the file ' + file.getName() + '. Continuing...');
}
Utilities.sleep(100); // .1 secs, avoiding rate limiting
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment