04-walk-glob.agi 782 B

123456789101112131415161718192021222324252627
  1. /*---
  2. title: Walk, glob and sort
  3. group: Files
  4. summary: Find files with glob patterns and walk a whole drive.
  5. ---*/
  6. requirelib("filelib");
  7. // glob only expands wildcards in the file name part.
  8. var images = filelib.glob("user:/Photo/*.jpg").concat(filelib.glob("user:/Photo/*.png"));
  9. // A second argument picks the sort mode: smart, mostRecent, largeToSmall, ...
  10. var bySize = filelib.glob("user:/Documents/*", "largeToSmall");
  11. // walk lists everything below a folder; "file" or "folder" filters it.
  12. var allFiles = filelib.walk("user:/", "file");
  13. var total = 0;
  14. for (var i = 0; i < allFiles.length; i++) {
  15. total += filelib.filesize(allFiles[i]);
  16. }
  17. sendJSONResp({
  18. images: images,
  19. documentsLargestFirst: bySize,
  20. fileCount: allFiles.length,
  21. totalBytes: total
  22. });