使用旧答案在 tcl 中搜索文件: https ://stackoverflow.com/a/435094/984975
首先让我们讨论一下我现在在做什么:使用此功能:(感谢 Jacson)
# 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
}
并调用以下命令:
findFiles some_dir_name *.c
当前结果:
bad option "normalize": must be atime, attributes, channels, copy, delete, dirname, executable, exists, extension, isdirectory, isfile, join, lstat, mtime, mkdir, nativename, owned, pathtype, readable, readlink, rename, rootname, size, split, stat, tail, type, volumes, or writable
现在,如果我们运行:
glob *.c
我们得到很多文件,但它们都在当前目录中。
目标是获取机器上所有子文件夹中的所有文件及其路径。有谁能帮忙吗?
我真正想做的是找到 *.c 文件数量最多的目录。但是,如果我可以列出所有文件及其路径,我就可以计算每个目录中有多少文件,并获得数量最多的文件。