11

我有一个主要的 TCL proc,它在其他文件夹和后续子目录中获取大量其他 tcl 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

当我总是知道我将在文件夹 A 和文件夹 B 中获取所有内容时,这样做似乎有点愚蠢。是否有一个功能(或简单的方法)可以让我只获取整个文件夹中的所有 .tcl 文件?

4

7 回答 7

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
 }
于 2009-01-12T10:58:29.570 回答
10

在船上使用 tcllib 变得微不足道:

package require fileutil
foreach file [fileutil::findByPattern $basepath *.tcl] {
    source $file
}
于 2009-01-30T17:31:36.450 回答
6

也许更独立于平台并使用内置命令而不是管道到进程:

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

对文件夹 B 重复此操作。

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

于 2009-01-10T07:41:22.107 回答
2

这是一种方法:

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

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

close $includes
于 2009-01-09T20:01:19.633 回答
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]
}
于 2009-01-15T21:43:33.043 回答
2

与 schlenk 相同的想法:

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

如果您只想要文件夹 A 和文件夹 B 而不是 $basepath 下的其他文件夹:

package require Tclx
for_recursive_glob scriptName [list $basepath/folderA $basepath/folderB] *.tcl {
    source $scriptName
}
于 2009-09-14T17:48:25.277 回答
0

Joseph Bui 的回答效果很好,只是它跳过了初始文件夹中的文件。

改变:

设置目录 [列表]
到:
设置目录 [list $directory]

修理

于 2016-01-24T08:36:24.380 回答