NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 27 of 30 · Tools for the NASFind the largest files

Let wc and sort do the work and print the first three rows.

Programming on the NAS › BASIC in 30 lessons

27-largest-files.yab Download 27-largest-files.yab

rem The three largest files of a folder
folder$ = peek$("argument")
dim row$(1), part$(1)
n = token(system$("wc -c '" + folder$ + "'/* | sort -rn"), row$(), chr$(10))

shown = 0
for i = 1 to n
  m = token(row$(i), part$(), " ")
  if part$(2) <> "total" and shown < 3 then
    print str$(val(part$(1)), "%8.0f"), "  ", part$(2)
    shown = shown + 1
  endif
next i

Terminal

$ yabasic 27-largest-files.yab photos
   15800  photos/wedding.mov
    7300  photos/trip.mp4
    4200  photos/beach.jpg

Exercise

Take the number of rows as an argument.