Skip to content

Instantly share code, notes, and snippets.

@evjeny
Created January 29, 2021 23:17
Show Gist options
  • Save evjeny/f66b3ded7b187e6595abe23c1ff96d04 to your computer and use it in GitHub Desktop.
Save evjeny/f66b3ded7b187e6595abe23c1ff96d04 to your computer and use it in GitHub Desktop.
Google App Script for listing all image files in directory and getting links to them
function listAllFiles() {
var sheets = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sheets.getActiveSheet();
var cell = sheet.getActiveCell();
var to_visit = [DriveApp.getFolderById("<id>")];
var info = [], f;
var q = '"';
while (to_visit.length > 0) {
var cur_folder = to_visit.pop();
var files = cur_folder.getFiles();
while (files.hasNext()) {
f = files.next();
if (!(f.getName().endsWith(".jpg") || f.getName().endsWith(".png"))) {
continue;
}
var parent = f.getParents().next();
var path = f.getName();
while (true) {
path = parent.getName() + "/" + path;
if (parent.getParents().hasNext()) {
parent = parent.getParents().next();
} else {
break;
}
}
var valid_url = q + f.getUrl() + q;
var valid_path = q + path + q;
info.push([valid_url, valid_path]);
}
var subfolders = cur_folder.getFolders();
while (subfolders.hasNext()) {
f = subfolders.next();
to_visit.push(f);
}
}
sheet.getRange(cell.getRow(), cell.getColumn(), info.length, 2).setValues(info);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment