使用一个旧答案在tcl中搜索文件:
https://dev59.com/23RC5IYBdhLWcg3wCMnX#435094首先让我们讨论我现在正在做什么:
使用以下函数:(感谢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文件的目录。然而,如果我能列出所有文件和它们的路径,我就可以计算每个目录中有多少文件并得到其中包含最多文件的目录。