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

时间:2009-01-09 19:34:24

标签: tcl

我有一个主要的TCL过程,在其他文件夹和后续子目录中提供了大量其他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

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

7 个答案:

答案 0 :(得分:10)

在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
 }

答案 1 :(得分:10)

tcllib在船上变得微不足道了:

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

答案 2 :(得分:5)

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

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

对folderB重复一遍。

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

答案 3 :(得分: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]
}

答案 4 :(得分:2)

与schlenk相同的想法:

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

如果您只需要folderA和folderB而不是$ basepath下的其他文件夹:

package require Tclx
for_recursive_glob scriptName [list $basepath/folderA $basepath/folderB] *.tcl {
    source $scriptName
}

答案 5 :(得分:1)

这是一种方式:

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

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

close $includes

答案 6 :(得分:0)

Joseph Bui的答案很有效,只是它会跳过初始文件夹中的文件。

变化:   

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

修复