Skip to content

Instantly share code, notes, and snippets.

@woodwardtw
Last active June 9, 2023 02:40
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save woodwardtw/22a199ecca73ff15a0eb to your computer and use it in GitHub Desktop.
Save woodwardtw/22a199ecca73ff15a0eb to your computer and use it in GitHub Desktop.
google script that lists a lot of info about the files in a particular folder/sub folder structure including viewers, editors, and sharing permissions
function listFolders(folder) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description", "Type"]); //writes the headers
var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");//that long chunk of random numbers/letters in the URL when you navigate to the folder
var files = folder.getFiles();//initial loop on loose files w/in the folder
var cnt = 0;
var file;
while (files.hasNext()) {
var file = files.next();
var listEditors = file.getEditors(); //gets the editor email(s), doesn't show your own as it's assumed
var editors = [];
for (var cnt = 0; cnt < listEditors.length; cnt++) {
editors.push(listEditors[cnt].getEmail());
Logger.log(editors);
};
var listViewers = file.getViewers(); //gets the viewer email(s)
var viewers = [];
for (var cnt = 0; cnt < listViewers.length; cnt++) {
viewers.push(listViewers[cnt].getEmail());
Logger.log(viewers);
}
cnt++; //data chunk pushes all the file info to the ss
data = [
file.getName(),
file.getSharingAccess(),
file.getSharingPermission(),
editors.toString(),
viewers.toString(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
];
sheet.appendRow(data);
};
var subfolders = folder.getFolders(); //same thing as above but for all the subfolders in the folder
while (subfolders.hasNext()) {
//Logger.log(folder);
var name = subfolders.next();
var files = name.getFiles();
var cnt = 0;
var file;
while (files.hasNext()) {
var file = files.next();
var listEditors = file.getEditors();
var editors = [];
for (var cnt = 0; cnt < listEditors.length; cnt++) {
editors.push(listEditors[cnt].getEmail());
Logger.log(editors);
};
var listViewers = file.getViewers();
var viewers = [];
for (var cnt = 0; cnt < listViewers.length; cnt++) {
viewers.push(listViewers[cnt].getEmail());
Logger.log(viewers);
}
cnt++;
data = [
file.getName(),
file.getSharingAccess(),
file.getSharingPermission(),
editors.toString(),
viewers.toString(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
];
sheet.appendRow(data);
};
}
}
@hub2git
Copy link

hub2git commented Sep 3, 2017

Hi Tom. Just wondering if this script is still current (works with the Google Drive code of today).

@hub2git
Copy link

hub2git commented Sep 3, 2017

google script that lists a lot of info about the files in a particular folder/sub folder structure

Can we use your script to scan our entire Google Drive, and not just a particular folder?

@exekutive
Copy link

How do I use this thing?

@spaceman-cb
Copy link

spaceman-cb commented Apr 4, 2019

For anyone looking at this today:

Hi Tom. Just wondering if this script is still current (works with the Google Drive code of today).

As of the posting of this comment, it appears to still work.

google script that lists a lot of info about the files in a particular folder/sub folder structure
Can we use your script to scan our entire Google Drive, and not just a particular folder?

Apparently not. You can scan the top level of your entire drive by setting the folder id to "root", but it won't scan the subdirectories if you do.

The following fork looks like it might address this issue, but the script takes so long that it's likely to time out... making it necessary to do it folder by folder :(

https://gist.github.com/rzrbld/ba50a0f51b081a5699cb1d4996e4925a

@ichaer
Copy link

ichaer commented Sep 6, 2019

Try this one if all you want is to find files with open sharing:

https://gist.github.com/ichaer/d7b91d348a250e09146057857f7b3cc2

@JavierCane
Copy link

☝️ This version doesn't list all the subfolders recursively. That is, you'll only get a list of the shared items inside the specified folder and the ones in it subfolders, but not the ones contained in the subsequent subfolders.

Here you have a version that does it recursively. It also allows you to specify a list of emails to not take into account. That is, if the documents are only shared privately with the emails contained in that list, the script will not include that shared document in the resulting sheet 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment