Tcl
getfiles.tcl


return to misc index



This Tcl script provides a procedure that recursively checks the sizes of files in a directory. The full paths of those files that are larger than a specified size are appended to a list supplied by the user. The procedure uses recursion to examine the directory structure of the target directory.

Refer to the reference dirsize for procedure that also uses recursion.


Listing 1


proc getfiles { dir sizelimit files } {
    # enable our local variable "f" to reference
    # the global "files" variable
    upvar $files f
    
    set contents [glob -nocomplain -directory $dir *]
    foreach item $contents {
       if { [file isdirectory $item] } {
          # recurse ie. call ourself
          getfiles $item sizelimit files
       } elseif { [file isfile $item]} {
          # check the file size
          if { [file size $item] > $sizelimit } {
             append f [format "%s\n" $item]
             }
          }
       }
    }


The input parameters are:

getFiles
    dir          the full path of the target directory
    sizelimit    files larger than this size will be
                 appended to the files list
    files        the string to which file names will
                 be appened

Example:
A directory called "CV" contains 10 files of various 
sizes. The directory is located at 
    "/Users/Documents/CV" 
To get a listing of all files in this directory that exceed
32Kb the getFiles procedure might be used by another Tcl script 
as follows.
    
    source /Users/Documents/TCL/getFiles.tcl
    # print the files to the console
    set maxsize 32000
    set files ""
    getFiles "/Users/Documents/CV" $maxsize files
    puts $files

Note the use of the source command to "point" the 
Tcl interpreter at the directory containing
the getFiles.tcl script.



© 2002- Malcolm Kesson. All rights reserved.