tcl深度递归文件搜索,搜索扩展名为* .c的文件 [英] tcl deep recursive file search, search for files with *.c extension

查看:325
本文介绍了tcl深度递归文件搜索,搜索扩展名为* .c的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用旧答案在tcl中搜索文件: https://stackoverflow.com/a/435094/984975

Using an old answer to search for a file in tcl: https://stackoverflow.com/a/435094/984975

首先让我们讨论一下我现在正在做什么: 使用此功能:(贷给杰森)

First lets discuss what I am doing right now: Using this function:(credit to 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

我们得到了很多文件,但所有文件都在当前目录中.

We get a lot of files but all of them in the current directory.

目标是获取计算机上 ALL 子文件夹中的 ALL 文件及其路径. 有人可以帮忙吗?

The goal is to get ALL the files in ALL sub-folders on the machine with their paths. Anyone who could help?

我真正想做的是找到* .c文件数量最多的目录. 但是,如果我可以列出所有文件及其路径,则可以计算每个目录中有多少个文件,并获得计数最高的文件.

What I really want to do is find the directory with the highest count of *.c files. However, if I could list all the files and their paths, I could count, how many files are in each directory and get the one with the highest count.

推荐答案

您正在使用旧版本的Tcl. [file normalize]在2002左右的Tcl 8.4中引入.已经升级.

You are using an old version of Tcl. [file normalize] was introduced in Tcl 8.4 around 2002 or so. Upgrade already.

如果不能-则使用glob,但仅对文件调用一次,然后遍历目录.请参阅glob -types选项.

If you can't - then you use glob but call it once just for files and then walk the directories. See the glob -types option.

这是一个演示:

proc on_visit {path} {
    puts $path
}

proc visit {base glob func} {
    foreach f [glob -nocomplain -types f -directory $base $glob] {
        if {[catch {eval $func [list [file join $base $f]]} err]} {
            puts stderr "error: $err"
        }
    }
    foreach d [glob -nocomplain -types d -directory $base *] {
        visit [file join $base $d] $glob $func
    }
}

proc main {base} {
    visit $base *.c [list on_visit]
}

main [lindex $argv 0]

这篇关于tcl深度递归文件搜索,搜索扩展名为* .c的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆