Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. # findFiles
  2. # basedir - the directory to start looking in
  3. # pattern - A pattern, as defined by the glob command, that the files must match
  4. proc findFiles { basedir pattern } {
  5.  
  6. # Fix the directory name, this ensures the directory name is in the
  7. # native format for the platform and contains a final directory seperator
  8. set basedir [string trimright [file join [file normalize $basedir] { }]]
  9. set fileList {}
  10.  
  11. # Look in the current directory for matching files, -type {f r}
  12. # means ony readable normal files are looked at, -nocomplain stops
  13. # an error being thrown if the returned list is empty
  14. foreach fileName [glob -nocomplain -type {f r} -path $basedir $pattern] {
  15. lappend fileList $fileName
  16. }
  17.  
  18. # Now look for any sub direcories in the current directory
  19. foreach dirName [glob -nocomplain -type {d r} -path $basedir *] {
  20. # Recusively call the routine on the sub directory and append any
  21. # new files to the results
  22. set subDirList [findFiles $dirName $pattern]
  23. if { [llength $subDirList] > 0 } {
  24. foreach subDirFile $subDirList {
  25. lappend fileList $subDirFile
  26. }
  27. }
  28. }
  29. return $fileList
  30. }
  31.  
  32. findFiles some_dir_name *.c
  33.  
  34. 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
  35.  
  36. glob *.c
  37.  
  38. proc on_visit {path} {
  39. puts $path
  40. }
  41.  
  42. proc visit {base glob func} {
  43. foreach f [glob -nocomplain -types f -directory $base $glob] {
  44. if {[catch {eval $func [list [file join $base $f]]} err]} {
  45. puts stderr "error: $err"
  46. }
  47. }
  48. foreach d [glob -nocomplain -types d -directory $base *] {
  49. visit [file join $base $d] $glob $func
  50. }
  51. }
  52.  
  53. proc main {base} {
  54. visit $base *.c [list on_visit]
  55. }
  56.  
  57. main [lindex $argv 0]
  58.  
  59. package require ::fileutil::traverse
  60.  
  61. proc check_path {path} {
  62. string equal [file extension $path] ".c"
  63. }
  64.  
  65. set obj [::fileutil::traverse %AUTO% -filter check_path]
  66. array set pathes {}
  67. $obj foreach file {
  68. if {[info exists pathes([file dirname $file])]} {
  69. incr pathes([file dirname $file])
  70. } else {
  71. set pathes([file dirname $file]) 1
  72. }
  73. }
  74.  
  75. # print pathes and find the biggest
  76. foreach {name value} [array get pathes] {
  77. puts "$name : $value"
  78. }
  79.  
  80. glob **/*.c
  81.  
  82. proc findFiles { basedir pattern } {
  83. return [glob [file join ${basedir} ** ${pattern}]]
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement