How to resolve the algorithm File size distribution step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm File size distribution step by step in the Tcl programming language

Table of Contents

Problem Statement

Beginning from the current directory, or optionally from a directory specified as a command-line argument, determine how many files there are of various sizes in a directory hierarchy.

My suggestion is to sort by logarithmn of file size, since a few bytes here or there, or even a factor of two or three, may not be that significant. Don't forget that empty files may exist, to serve as a marker.

Is your file system predominantly devoted to a large number of smaller files, or a smaller number of huge files?

Let's start with the solution:

Step by Step solution about How to resolve the algorithm File size distribution step by step in the Tcl programming language

Source code in the tcl programming language

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 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 {
	set size [file size $path]
	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]"
}


  

You may also check:How to resolve the algorithm Loops/Foreach step by step in the Ada programming language
You may also check:How to resolve the algorithm Nautical bell step by step in the Tcl programming language
You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the FunL programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the BCPL programming language