This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

reading the number of directories in a given folder

Old posts that have not been replied to for several years.
Locked
q
qzec
Voice
Posts: 1
Joined: Sun Dec 05, 2004 1:32 am

reading the number of directories in a given folder

Post by qzec »

Hello World,

I have more or less just begun to work with tcl. After spending the past two weeks reading up on it and trying out all the neat features, I came up with some pretty nifty ideas for a tcl project (no the ideas aren’t relatively new).

But… (I bet you saw this one coming didn’t you?) I can’t seem to find out how to use tcl to read the number of directories in a given folder. I’m too tired to give a fancy example so let me whip up a quick doodle to illustrate (please keep the laughter to a minimal):

/area_51
-/out_there
--/moo
--/arf
--/meow

(note: the dash represents the directory depth)

Now suppose the given folder was /area_51/out_there/

What I am trying to do is figure out how I will able to use tcl to read this folder and display some kind of output similar to:

Soldier: I’m not at liberty to tell you that we have 3 sounds stored out there. $@#%. :x I just broke protocol... What am I going to do?! *lightbulb* I know!! :) I’ll pretend you didn’t hear me. :D

Now, the thing is, we don’t know the names of the directories in the given folder.

I’m assuming it’s possible to use some kind of wildmask? Actually at this point, I’m hoping it’s possible.

Q
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

Code: Select all

string trim [exec find . -type d | wc -l]
(assuming it's not a windrop)
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

the above simply returns number of directories in the current tree

if you want to traverse the tree, use [glob] and [file] commands
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

this will traverse the directory tree, executing $cmd on each file:

Code: Select all

proc traverse {dir cmd} {
  if [catch {set files [glob -directory $dir *]}] return
  foreach f $files {
    if [file isdirectory $f] {traverse $f $cmd}
    $cmd $f
  }
}
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

demond wrote:

Code: Select all

if [catch {set files [glob -directory $dir *]}]
you don't need to catch glob... use -nocomplain :)

Here's a dir counting proc that doesn't depend on external executables:

Code: Select all

proc dirs {root} {
	set i 0
	foreach dir [glob -noc -typ d -dir $root *] {
		incr i [expr {1+[dirs $dir]}]
	}
	set i
}
Have you ever read "The Manual"?
Locked