Skip to content

Instantly share code, notes, and snippets.

@leoherzog
Last active Jul 5, 2022
Embed
What would you like to do?
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