Advertisement
HappySunChild

FileSystemListing.lua

Oct 9th, 2022 (edited)
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | Source Code | 0 0
  1. local args = { ... }
  2.  
  3. local baseDir = args[1]
  4. local countReadOnly = args[2] == "-y"
  5.  
  6. local function getDirSize(dir, depth)
  7.     local sizes = {}
  8.     local totalSize = 0
  9.  
  10.     for i, file in pairs(fs.list(dir)) do
  11.  
  12.         if fs.exists(dir .. "/" .. file) and (countReadOnly and (fs.isReadOnly(fs.combine(dir, file)))) or
  13.             (countReadOnly == false and fs.isReadOnly(fs.combine(dir, file)) == false) then
  14.             if not fs.isDir(dir .. "/" .. file) then
  15.                 totalSize = totalSize + fs.getSize(dir .. "/" .. file)
  16.  
  17.                 table.insert(sizes, { name = dir .. "/" .. file, size = fs.getSize(dir .. "/" .. file) })
  18.             else
  19.                 local dirSize, dirSizes = getDirSize(dir .. "/" .. file, depth + 1)
  20.                 totalSize = totalSize + dirSize
  21.  
  22.                 for i, file in pairs(dirSizes) do
  23.                     if file then
  24.                         sizes[#sizes + 1] = file
  25.                     end
  26.                 end
  27.             end
  28.         end
  29.     end
  30.  
  31.     return totalSize, sizes
  32. end
  33.  
  34. local max = fs.getCapacity(baseDir)
  35. local total, sizes = getDirSize(baseDir, 0)
  36.  
  37. term.clear()
  38. term.setCursorPos(1, 1)
  39.  
  40. for i, file in pairs(sizes) do
  41.     print(file.name .. ": " .. (file.size / 1000) .. " kb")
  42. end
  43.  
  44. print("\ntotal size: " .. (total / 1000) .. " kb")
  45. print(string.format("%.2f%% space taken up", tostring((total / max) * 100)))
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement