Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
- 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]
- package require ::fileutil::traverse
- proc check_path {path} {
- string equal [file extension $path] ".c"
- }
- set obj [::fileutil::traverse %AUTO% -filter check_path]
- array set pathes {}
- $obj foreach file {
- if {[info exists pathes([file dirname $file])]} {
- incr pathes([file dirname $file])
- } else {
- set pathes([file dirname $file]) 1
- }
- }
- # print pathes and find the biggest
- foreach {name value} [array get pathes] {
- puts "$name : $value"
- }
- glob **/*.c
- proc findFiles { basedir pattern } {
- return [glob [file join ${basedir} ** ${pattern}]]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement