RosettaCodeData/Task/File-size-distribution/Euphoria/file-size-distribution.eu
2026-04-30 12:34:36 -04:00

44 lines
1.2 KiB
Text

include std/console.e
include std/filesys.e
constant UNITS = {"B","KB","MB","GB","TB"}
sequence files_by_category = {0,0,0,0,0}
function category(atom bytes) -- Returns file size category
for i = 4 to 1 by -1 do
atom a = power(1024,i)
if bytes >= a then
return i + 1
end if
end for
return 1
end function
procedure categorize(sequence rootdir)
object x = dir(rootdir & "\\*.*") -- Get DIR information of all files
if sequence(x) then -- Exclude forbidden DIRs
for i = 1 to length(x) do -- Loop through DIR content
sequence s = x[i]
if find('d',s[D_ATTRIBUTES]) then -- Skip SubDIRs
if not (equal(s[D_NAME],".") or equal(s[D_NAME],"..")) then
categorize(rootdir & "\\" & s[D_NAME])
end if
else
integer cat = category(s[D_SIZE]) -- Categorize file size
files_by_category[cat] += 1
end if
end for
end if
end procedure
sequence rootdir = prompt_string("Folder name: ")
puts(1, "Cat\tNumber\n")
if length(rootdir)>0 then
categorize(rootdir)
for i = 1 to 5 do
printf(1, "%s\t%d\n", {UNITS[i], files_by_category[i]})
end for
end if
maybe_any_key()