Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I script Google Drive to unshare all subfolders and files given a folder ID?

Google Drive has a problem: if you unshare a top level folder everything in the folder is still shared...I'm not sure how such a large bug could survive but it does...details here:

http://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

I don't want to manually go through and unshare everything manually, so I'm thinking I could create a script that I could just plug in the folder ID into and have it do all the work.

How do I do this?

like image 908
Timothy Kenny Avatar asked Oct 20 '22 16:10

Timothy Kenny


1 Answers

this unshares someone from every folder and file in your drive.

// create a Google Document
// Tools / Script Editor
// paste this in
// update line 13 and save
// hit play to run. you may have to run several times as the script will die after 5 or 6 minutes.

function findSharedFolders() {
  // https://developers.google.com/apps-script/reference/drive/drive-app
  // https://developers.google.com/drive/web/search-parameters
  // http://stackoverflow.com/questions/21227771/how-do-i-script-google-drive-to-unshare-all-subfolders-and-files-given-a-folder
  // https://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

  var email = "[email protected]"; // <<<<< INSERT THE UNDESIRED EMAIL ADDRESS HERE

  var body = DocumentApp.getActiveDocument().getBody();
  body.appendParagraph(email).setHeading(DocumentApp.ParagraphHeading.HEADING1);
  var folders = DriveApp.searchFolders("'"+email+"' in writers");  while (folders.hasNext()) unshare_(folders.next(), body, email);
  var files   = DriveApp.searchFiles(  "'"+email+"' in writers");  while (  files.hasNext()) unshare_(  files.next(), body, email);
}

function unshare_(file, body, email) {
// Logger.log(file.getName());
   var para = body.appendParagraph(file.getName());
   try { file.removeEditor(email); }
   catch (e) { para.setLinkUrl(file.getUrl()); para.appendText(" (" + e + ")"); }
}
like image 192
mengwong Avatar answered Oct 31 '22 18:10

mengwong