I am trying to list all of the *.tcl files that's exactly 1 level below. Such as
./dirA/x.tcl
./dirB/y.tcl
./dirC/z.tcl
I am using foreach
command to list all files & directories under the sub directory, but the output is empty..
foreach dir [glob -type d *] {
glob -path ./$dir *
}
You only need one call to glob:
set tcl_files [glob */*.tcl]
To begin with, you probably want the -directory
option rather than the -path
option. It can be abbreviated to -dir
. Also, you wanted the files matching *.tcl
, not every file (*
).
foreach dir [glob -type d *] {
glob -dir $dir *.tcl
}
Second, the foreach
command does not collect the results of the script it runs, so you need to do that yourself:
set files [list]
foreach dir [glob -type d *] {
lappend files {*}[glob -dir $dir *.tcl]
}
Now the files
variable should hold the file names you want.
If you're using an older version of Tcl (before 8.5), you need to write it like this:
set files [list]
foreach dir [glob -type d *] {
set files [concat $files [glob -dir $dir *.tcl]]
}
Documentation: concat, foreach, glob, lappend, set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With