Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
google script that lists information on google drive files and folders shared by link
var getFullPath = function(pathObj) {
var out = '';
var sep = '/';
var folderIter = pathObj.getParents();
while(folderIter.hasNext()) {
out += sep + folderIter.next().getName();
}
out += sep + pathObj.getName();
return out;
};
var arrJoin = function(arr, separator, val2str) {
separator = typeof separator !== 'undefined' ? separator : ', ';
val2str = typeof val2str !== 'undefined' ? val2str : function(v){return v};
var out = '';
var sep = '';
for(i in arr) {
out += sep + val2str(arr[i]);
sep = separator;
}
return out;
}
function listFolders() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Type","Path","Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description"]);
var folders = DriveApp.getFolders();
var sharedStuff = [
['folder', DriveApp.searchFolders("visibility != 'limited' and 'me' in owners and trashed = false")],
['file', DriveApp.searchFiles("visibility != 'limited' and 'me' in owners and trashed = false")]
];
for (var i in sharedStuff) {
if (!sharedStuff.hasOwnProperty(i)) {
continue;
}
var pathType = sharedStuff[i][0];
var pathIterator = sharedStuff[i][1];
while (pathIterator.hasNext()) {
var path = pathIterator.next();
data = [
pathType,
getFullPath(path),
path.getSharingAccess(),
path.getSharingPermission(),
arrJoin(path.getEditors(), ', ', function(v){return v.getEmail()}),
arrJoin(path.getViewers(), ', ', function(v){return v.getEmail()}),
path.getDateCreated(),
path.getSize(),
path.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + path.getId(),
path.getDescription()
];
sheet.appendRow(data);
};
}
}
@thibaultmol

This comment has been minimized.

Copy link

@thibaultmol thibaultmol commented Apr 13, 2021

protip that I just discovered, might be worth adding a comment line for:
if you just replace the folder id with "root" that will scan the entire My Drive itself

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