File size distribution: Difference between revisions

Add Tcl version
(Added Delphi example)
(Add Tcl version)
Line 1,570:
Total: 370026462 bytes in 2650 files
</pre>
 
=={{header|Tcl}}==
This is with the '''fileutil::traverse''' package from Tcllib to do the tree walking, a '''glob''' based alternative ignoring links but not hidden files is possible but would add a dozen of lines.
<lang tcl>package require fileutil::traverse
namespace path {::tcl::mathfunc ::tcl::mathop}
 
# Ternary helper
proc ? {test a b} {tailcall if $test [list subst $a] [list subst $b]}
 
set hist [dict create]
set dir [? {$argc} {[lindex $argv 0]} .]
fileutil::traverse Tobj $dir \
-prefilter {apply {path {ne [file type $path] link}}} \
-filter {apply {path {eq [file type $path] file}}}
Tobj foreach path {
if {![catch {file size $path} size]} {
dict incr hist [? {$size} {[int [log10 $size]]} -1]
}
}
Tobj destroy
 
foreach key [lsort -int [dict keys $hist]] {
puts "[? {$key == -1} 0 {1e$key}]\t[dict get $hist $key]"
}</lang>
{{out}}
<pre>0 1
1e1 339
1e2 3142
1e3 2015
1e4 150
1e5 29
1e6 13
1e7 3</pre>
 
=={{header|zkl}}==
Anonymous user