View Full Version : How to get Size of a dir in TCL ?
djdeluxe76
05-27-2005, 04:40 AM
Hello, and (yet another) 'thank you' for reading this...
I have been looking this up everywhere (to my knowledge that is) but
I cannot find it in the ActiveTcl Manual. I stumbled upon a module called
TWAPI (Tcl Windows API) hoping that this would give me hooks to
explorer attributes.... but no, only for files and then only for access
rights...
It can't be that getting the size of a directory is not a common task no
matter which language you are using.
So how do I fetch this in Tcl, please?
Thanks for any and replies or pointers to additional text.
Cheers
DJ
djdeluxe76
05-27-2005, 05:05 AM
Just found this on tcl.tk
Fred Limouzin: 2005/04/11
The file size command currently (i.e. Tcl8.4.9) does not support
directories. A pure-Tcl solution involves adding recursively the size of
files in the current and in sub-directories.
Here's a Tk/GUI script that I wrote this week-end, which fits my
need. I wanted a gradient-color indication for the size of directories.
You can position the orange/yellow scale (size at which the color
indicator is orange/yellow; below that value the indicator is gradient
going toward green (size=0)) and the red scale (size from which the
indicator is red!). A scale selector also lets you decide how many
levels of sub-directories to display. read more... (http://wiki.tcl.tk/13978)
Get source code for directory size (http://wiki.tcl.tk/13978).
There is also a link on that page to source code from the UNIX du (disk usage) TCL implementation.
So, as I understand it, it's actually really not possible to get the size
all at once. You have to recursively read the size from all files??
Or maybe I don't fully understand yet what I have found above....
DJ
set srcpath [resolve pwd $pwd$release]
set nesting 3
set knownfiles 0
set knowndirs 0
set subdir "*"
set sizeneeded 0
for {set level 0} {$level < $nesting} {incr level} {
set test [glob -nocomplain -directory $srcpath "$subdir"]
foreach dir $test {
if {[file isdirectory "$dir"]} {
lappend dirs "$dir "
incr knowndirs
} else {
if {![string match -nocase "*/.*" $dir]} {
incr knownfiles
}
}
set sizeneeded [expr [expr $sizeneeded + [file size $dir]] * 1.0]
}
append subdir "/*"
}
set sizeneeded [expr [expr $sizeneeded / 1024] * 1.0]
this is basically what i use in ioPRE to check for enough free space
in a destination path before `pre`, i guess you can work out the rest
djdeluxe76
05-28-2005, 12:24 PM
Hello tuff,
Let me thank you very much for sharing your knowledege :)
I am working with your code now as this is very much all I needed
already. Just have to look at it for some time until I fully understand it :)
Cheers
DJ
vBulletin® v3.8.11 Alpha 3, Copyright ©2000-2025, vBulletin Solutions, Inc.