gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:40:24 26 4
gpt4 key购买 nike

我有一个主要的 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 文件?

最佳答案

基于拉曼曼的回复,这里有一个例程,它使用内置的 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
}

关于TCL:递归搜索子目录以获取所有 .tcl 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/429386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com