recursive_glob

This command is part of the TclX package.

recursive_glob dirlist globlist

Return a list of all files in/below directories in dirlist that match one of the patterns in globlist. See also for_recursive_glob.


TR Using Tcl 8.5 or newer, you can easily use the proc below instead, which is just an improved version of the TclX code:

proc rglob {dirlist globlist} {
        set result {}
        set recurse {}
        foreach dir $dirlist {
                if ![file isdirectory $dir] {
                        return -code error "'$dir' is not a directory"
                }
                foreach pattern $globlist {
                        lappend result {*}[glob -nocomplain -directory $dir -- $pattern]
                }
                foreach file [glob -nocomplain -directory $dir -- *] {
                        set file [file join $dir $file]
                        if [file isdirectory $file] {
                                set fileTail [file tail $file]
                                if {!($fileTail eq "." || $fileTail eq "..")} {
                                        lappend recurse $file
                                }
                        }
                }
        }
        if {[llength $recurse] > 0} {
                lappend result {*}[rglob $recurse $globlist]
        }
        return $result
}

gavino works great!! 6jly12


tjk The above code doesn't work (at least for me). Here is a version that works and include an exclusion list. This version doesn't take a list of directories as the first argument but I can't think of a good reason that it should.

proc rglob { dirpath patterns {exclude_pats {}} } {
    set rlist {}
    set f_exclude [glob -nocomplain -types f -directory ${dirpath} {*}${exclude_pats}]
    set d_exclude [glob -nocomplain -types d -directory ${dirpath} {*}${exclude_pats}]
    foreach fpath [glob -nocomplain -types f -directory ${dirpath} {*}${patterns}] {
        if { ${fpath} ni ${f_exclude} } {
            lappend rlist ${fpath}
        }
    }
    foreach dir [glob -nocomplain -types d -directory ${dirpath} *] {
        if { ${dir} ni ${d_exclude} } {
            lappend rlist {*}[rglob ${dir} ${patterns} ${exclude_pats}]
        }
    }
    return ${rlist}
}

RFox Even more powerful is the fileutil::find command which uses a 'visitor pattern' to allow you to filter the files found in a directory tree via a script. since the script could have side effects, this also subsumes the for_recursive_glob functionality.

gavino fileutil find I think was not recursive... 6jly12