File size distribution: Difference between revisions

Added Wren
No edit summary
(Added Wren)
Line 1,686:
Total: 8.9 TB in 216716 files
GNU=1 ~/fsd.sh 0.81s user 1.33s system 135% cpu 1.586 total</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "io" for Directory, File, Stat
import "os" for Process
import "/math" for Math
import "/fmt" for Fmt
 
var sizes = List.filled(12, 0)
var totalSize = 0
var numFiles = 0
var numDirs = 0
 
var fileSizeDist // recursive function
fileSizeDist = Fn.new { |path|
var files = Directory.list(path)
for (file in files) {
var path2 = "%(path)/%(file)"
var stat = Stat.path(path2)
if (stat.isFile) {
numFiles = numFiles + 1
var size = stat.size
if (size == 0) {
sizes[0] = sizes[0] + 1
} else {
totalSize = totalSize + size
var logSize = Math.log10(size)
var index = logSize.floor + 1
sizes[index] = sizes[index] + 1
}
} else if (stat.isDirectory) {
numDirs = numDirs + 1
fileSizeDist.call(path2)
}
}
}
 
var args = Process.arguments
var path = (args.count == 0) ? "./" : args[0]
if (!Directory.exists(path)) Fiber.abort("Path does not exist or is not a directory.")
fileSizeDist.call(path)
 
System.print("File size distribution for '%(path)' :-\n")
for (i in 0...sizes.count) {
System.write((i == 0) ? " " : "+ ")
Fmt.print("Files less than 10 ^ $-2d bytes : $,5d", i, sizes[i])
}
System.print(" -----")
Fmt.print("= Number of files : $,5d", numFiles)
Fmt.print(" Total size in bytes : $,d", totalSize)
Fmt.print(" Number of sub-directories : $,5d", numDirs)</lang>
 
{{out}}
<pre>
File size distribution for './' :-
 
Files less than 10 ^ 0 bytes : 4
+ Files less than 10 ^ 1 bytes : 2
+ Files less than 10 ^ 2 bytes : 135
+ Files less than 10 ^ 3 bytes : 946
+ Files less than 10 ^ 4 bytes : 746
+ Files less than 10 ^ 5 bytes : 79
+ Files less than 10 ^ 6 bytes : 11
+ Files less than 10 ^ 7 bytes : 3
+ Files less than 10 ^ 8 bytes : 0
+ Files less than 10 ^ 9 bytes : 0
+ Files less than 10 ^ 10 bytes : 0
+ Files less than 10 ^ 11 bytes : 0
-----
= Number of files : 1,926
Total size in bytes : 12,683,455
Number of sub-directories : 3
</pre>
 
=={{header|zkl}}==
9,476

edits