Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list all files in sub directory 1 level below?

Tags:

tcl

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 *    
}
like image 681
Bonk Avatar asked Oct 11 '25 14:10

Bonk


2 Answers

You only need one call to glob:

set tcl_files [glob */*.tcl]
like image 68
glenn jackman Avatar answered Oct 16 '25 08:10

glenn jackman


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

like image 38
Peter Lewerin Avatar answered Oct 16 '25 09:10

Peter Lewerin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!