Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Option to [glob] to ease recursive traversal

Open1880980fffffffffffffffffffffffffffffffffCreated 19 years agoUpdated 19 years ago
Descriptionplain2008-01-28 07:12:34
Currently,  to traverse a directory hierarchy: in the top directory we need to explicitly glob for all directory entries (including those that start with a period) and then filter out "." and ".." and so on recursively for children directories

The following suggested option to [glob] will simplify code as well as provide some speed increase.

[glob -walk] will return all directory entries in the current dir including those that start with a period _except_ for "." and ".."  i.e it filters out the current and parent directories.  Other filters can be stacked on top of it as per usual with [glob]

This option does _not_ walk the entire directory tree recursively.  As such the name "-walk" may be considered misleading.

[glob -walk] essentially provides a filter to a previously suggested option [glob -all]: request id 1880967 at http://sourceforge.net/tracker/index.php?func=detail&aid=1880967&group_id=10894&atid=360894
and may well be used more often than [glob -all] raising doubts about the need for the latter.

Checking for recursive links in the filesystem is still the responsibility of the user.

e.g usage. To print the names of all and only directories under $root:

proc rec-print-dirs {dir} {
  puts $dir
  foreach d [glob -walk -directory $dir -types {d}] {
    rec-print-dirs $d
  }
}
rec-print-dirs $root

Thank you
E Wilson
[email protected]plain2008-01-28 15:11:30
Logged In: NO 

I think -all is a good idea provided that it does not return . and ..

Given that, -walk would be equivalent to -all -types d, and thus
redundant.
dgp Ticket [1880980fff]: 1 change [History diff]2008-01-28 21:55:27
dkf Ticket [1880980fff]: 1 change [History diff]2008-01-28 21:56:34
evil_sonplain2008-01-29 00:37:16
Logged In: YES 
user_id=1993540
Originator: YES

In response to sender "nobody",
if -all were not to return "." and ".." then it would behave exactly like -walk.  My less specific (compared to yours) version of -all was aimed at generality while the more specific -walk (which you call -all) is the one I'd use more often .. like you and perhaps most users.

Since the existing options are strong enough and we can easily wrap Tcl procedures around them, the motivation is really performance ... which raises the question is there a better interface that can provide superior performance.  I think there is but it may become too specific to directory traversal and will require more work; so I hesitated suggesting it.  I will put up another request in a couple of days describing it.

Thanks for your comment
evil_sonplain2008-01-31 23:01:00
Logged In: YES 
user_id=1993540
Originator: YES

Following up on my previous post: what if glob -walk returned a list of pairs <filename, type (i.e. file or dir)> of all directory entries except for . and ..

So to print out files under a directory (without error checking or checking for empty return lists):

proc allfiles {dir} {
  foreach pair [glob -walk -directory $dir] {
  foreach {entry type} $pair {break}
  if {$type eq "d"} { allfiles $entry }
  elseif {$type eq "f"} { puts $entry } 
  }
}
allfiles someDir
Log in (anonymous is fine) to add a comment.