TCL:递归搜索子目录以源化所有.tcl文件

11

我有一个主TCL Proc,在其他文件夹和子目录中源代码了大量的其他TCL Proc。 例如,在主Proc中:

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

当我总是知道我会在folderA和folderB中获取所有内容时,这种做法似乎有点愚蠢。是否有一种函数(或简单方式)可以允许我只在整个文件夹中源化所有.tcl文件?

7个回答

11

有了tcllib,它变得非常简单:

package require fileutil
foreach file [fileutil::findByPattern $basepath *.tcl] {
    source $file
}

11

在ramanman的回复基础上,这里有一个例程,它使用内置的TCL文件命令来解决问题,并递归地遍历目录树。

# findFiles
# basedir - the directory to start looking in
# pattern - A pattern, as defined by the glob command, that the files must match
proc findFiles { basedir pattern } {

    # Fix the directory name, this ensures the directory name is in the
    # native format for the platform and contains a final directory seperator
    set basedir [string trimright [file join [file normalize $basedir] { }]]
    set fileList {}

    # Look in the current directory for matching files, -type {f r}
    # means ony readable normal files are looked at, -nocomplain stops
    # an error being thrown if the returned list is empty
    foreach fileName [glob -nocomplain -type {f r} -path $basedir $pattern] {
        lappend fileList $fileName
    }

    # Now look for any sub direcories in the current directory
    foreach dirName [glob -nocomplain -type {d  r} -path $basedir *] {
        # Recusively call the routine on the sub directory and append any
        # new files to the results
        set subDirList [findFiles $dirName $pattern]
        if { [llength $subDirList] > 0 } {
            foreach subDirFile $subDirList {
                lappend fileList $subDirFile
            }
        }
    }
    return $fileList
 }

谢谢Jackson。我觉得现在我们可以把这个问题解决了! - Lyndon
3
如果您有一个创建循环的符号链接,您将会收到“太多嵌套评估(无限循环?)”错误。 - Joseph Bui
sudo apt-get install tcllib tcl tk - Bimo

7
也许更独立于平台,使用内置命令而不是通过管道传递到进程:
foreach script [glob [file join $basepath folderA *.tcl]] {
  source $script
}

重复进行文件夹B的操作。

如果您有更严格的选择标准,并且不担心在其他平台上运行,使用find命令可能更加灵活。


我唯一注意到的是,如果没有匹配的文件,这会返回一个错误,但是我承认我没有检查其他答案做了什么。 - Lyndon
在glob命令上使用“-nocomplain”选项,以防止生成空列表时出现错误。 - Jackson

2

和 Schlenk 相同的思路:

package require Tclx
for_recursive_glob scriptName $basepath *.tcl {
    source $scriptName
}

如果你只想要 $basepath 下的 folderA 和 folderB,而不是其他文件夹:
package require Tclx
for_recursive_glob scriptName [list $basepath/folderA $basepath/folderB] *.tcl {
    source $scriptName
}

2

以下是一种方法:

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

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

close $includes

2

根据之前的答复,此版本处理由符号链接创建的循环,并在此过程中消除由符号链接引起的重复文件。

# findFiles
# basedir - the directory to start looking in
# pattern - A pattern, as defined by the glob command, that the files must match
proc findFiles {directory pattern} {

    # Fix the directory name, this ensures the directory name is in the
    # native format for the platform and contains a final directory seperator
    set directory [string trimright [file join [file normalize $directory] { }]]

    # Starting with the passed in directory, do a breadth first search for
    # subdirectories. Avoid cycles by normalizing all file paths and checking
    # for duplicates at each level.

    set directories [list]
    set parents $directory
    while {[llength $parents] > 0} {

        # Find all the children at the current level
        set children [list]
        foreach parent $parents {
            set children [concat $children [glob -nocomplain -type {d r} -path $parent *]]
        }

        # Normalize the children
        set length [llength $children]
        for {set i 0} {$i < $length} {incr i} {
            lset children $i [string trimright [file join [file normalize [lindex $children $i]] { }]]
        }

        # Make the list of children unique
        set children [lsort -unique $children]

        # Find the children that are not duplicates, use them for the next level
        set parents [list]
        foreach child $children {
            if {[lsearch -sorted $directories $child] == -1} {
                lappend parents $child
            }
        }

        # Append the next level directories to the complete list
        set directories [lsort -unique [concat $directories $parents]]
    }

    # Get all the files in the passed in directory and all its subdirectories
    set result [list]
    foreach directory $directories {
        set result [concat $result [glob -nocomplain -type {f r} -path $directory -- $pattern]]
    }

    # Normalize the filenames
    set length [llength $result]
    for {set i 0} {$i < $length} {incr i} {
        lset result $i [file normalize [lindex $result $i]]
    }

    # Return only unique filenames
    return [lsort -unique $result]
}

0

Joseph Bui的答案很好,但它会跳过初始文件夹中的文件。

更改:

set directories [list]
为:
set directories [list $directory]

即可修复问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接