Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL: Recursively search subdirectories to source all .tcl files

Tags:

tcl

I have a main TCL proc that sources tons of other tcl procs in other folders and subsequent subdirectories. For example, in the main proc it has:

source $basepath/folderA/1A.tcl
source $basepath/folderA/2A.tcl
source $basepath/folderA/3A.tcl
source $basepath/folderB/1B.tcl
source $basepath/folderB/2B.tcl
source $basepath/folderB/3B.tcl

and it seems kind of stupid to do it that way when I always know I will source everything in folderA and folderB. Is there a function (or simple way) that'll allow me to just source all the .tcl files in an entire folder?

like image 528
Lyndon Avatar asked Jan 09 '09 19:01

Lyndon


People also ask

How do I list files in a directory in Tcl?

There are two commands that provide information about the file system, glob and file . glob provides the access to the names of files in a directory. It uses a name matching mechanism similar to the UNIX ls command or the Windows (DOS) dir command, to return a list of names that match a pattern.

What does glob in Tcl do?

The glob command differs from csh globbing in two ways. First, it does not sort its result list (use the lsort command if you want the list sorted). Second, glob only returns the names of files that actually exist; in csh no check for existence is made unless a pattern contains a ?, *, or [] construct.

What does glob command do?

In computer programming, glob (/ɡlɑːb/) patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *. txt textfiles/ moves ( mv ) all files with names ending in . txt from the current directory to the directory textfiles .

What is .Tcl file?

Tcl, an abbreviation of the name "Tool Command Language", is a scripting language for controlling and extending software applications. A . tcl file contains a Tcl script, which is composed of Tcl functions and can also include Quartus® Prime Application Programming Interface (API) functions used as commands.


2 Answers

Perhaps a little more platform independent and using builtins commands instead of piping to a process:

foreach script [glob [file join $basepath folderA *.tcl]] {
  source $script
}

Repeat for folderB.

If you have more stringent selection criteria, and don't worry about running on any other platforms, using find may be more flexible.

like image 105
ramanman Avatar answered Oct 11 '22 09:10

ramanman


Here is one way:

set includes [open "|find $basedir -name \*.tcl -print" r]

while { [gets $includes include] >= 0 } {
  source $include
}

close $includes
like image 33
Andru Luvisi Avatar answered Oct 11 '22 09:10

Andru Luvisi