I'm a newbie in tcl and I need to be able to list the content of a directory files and eventual subdirectories.
I have a directory which contains several files and several subdirectories.
I managed to obtain a list of all the files thanks to the glob command but I don't know how to obtain a list with the subdirectories.
How can I do this?
You should start listening to your own advice (learn what you don't know ) As I said before in the weird thread, I recommend -nocomplain to avoid having to 'catch' the glob, and -dir to avoid matching problems. Remember that the last argument(s) to glob is/are used as a glob matching pattern.
The "catch" looks to me like a set, at least it dose exactly the same thing.. Till now I didn't had problems like you've said, anyway, sugestion noted. Thanks.
Once the game is over, the king and the pawn go back in the same box.
caesar wrote:The "catch" looks to me like a set, at least it dose exactly the same thing.
The catch will result having the error message stored in the variable if there are no matches and would require an "if" to determine the outcome. Usually you do some kind of loop through the list of names and since foreach doesn't execute its body when passed an empty list, using -nocomplain will lead to results most people find convenient
it still doesn't work and i don't understand why...
i obtain the same list of files and directories without distinction with both commands.
it's really weird if you tell me that it works...
first i have both directories and files (as usual) and second i don't understand why some directories are displayed between brackets whereas others are just displayed like c:/dir/
You seem to have no idea what you're doing. Read the manual. The glob command returns a list of files, the {}'s are just the way tcl chose to represent your data in the list. To access single elements from this list, use a foreach loop/other loop combined with list commands/just list commands. You might want to take a look at the proc i posted too...if you combine that with reading the manual you might learn a thing or two.
In Tcl, backslash is the escape char and in most cases it will have to be escaped if you want it to be part of a string. The backslash char is also used for paths in dos, but when running Tcl through cygwin the paths are translated on the fly, so you can pretend you're on a UNIX machine and use forward slashes. (They don't need to be escaped.)
Last edited by user on Mon Sep 08, 2003 9:51 am, edited 1 time in total.
and for your information i read the manual and i could'nt find what interested me that's why i came in that forum. if you're not able to discuss with newbies because they are too dumb for you you should try another forum.
thank you
shenot wrote:bad switch "-files": must be -nocomplain or --
Ah..you're using some older tcl version I guess. Then you'll have to loop through the list of files and check each one using 'file isdirectory' / 'file isfile'
proc rglob {pattern {dir ./}} {
set files {}
regsub -all {[\\\[\]\{\}\?\*]} $dir {\\&} dir
foreach file [glob -nocomplain [file join $dir *]] {
if {[file isfile $file]} {
if {[string match $pattern $file]} {lappend files $file}
} elseif {[file isdir $file]} {
set files [concat $files [rglob $pattern $file]]
}
}
set files
}
Being a newbie doesn't justify doing insane stuff like adding random arguments to a command. (like you did)
What version are you using btw?
EDIT: I added a regsub to escape any glob chars in the path. (This won't work under cygwin because of the transparent rewrite to backslashes in paths, so just remove the regsub and pray if you're using it on a win* box ;P ...or upgrade to a tcl version that supports the -dir option (for glob))