filelib.filesize.js 739 B

123456789101112131415161718192021222324
  1. console.log('Testing get filesize');
  2. requirelib("filelib");
  3. //Help function for converting byte to human readable format
  4. function bytesToSize(bytes) {
  5. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  6. if (bytes == 0) return '0 Byte';
  7. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  8. return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
  9. }
  10. //Get all the files filesize on desktop
  11. var fileList = filelib.glob("user:/Desktop/*.*");
  12. var results = [];
  13. for (var i =0; i < fileList.length; i++){
  14. var filename = fileList[i].split("/").pop();
  15. var fileSize = filelib.filesize(fileList[i]);
  16. results.push({
  17. filename: filename,
  18. filesize: bytesToSize(fileSize)
  19. });
  20. }
  21. sendJSONResp(JSON.stringify(results));