Tcl
|
Introduction
This tutorial draws upon a previous tutorial that dealt with
list basics.
If you are not familiar with list handling you should read that tutorial before
proceeding. set dir "C:/tcl" puts "Directory contents are:\n[glob -directory $dir *]"
The asterix is called the wild card. In effect it makes the glob command return
a full list of all the files and directories within the target directory irrespective
of their name and/or file extension. set dir "C:/tcl" set contents [glob -directory $dir *] puts "Directory contents are:" foreach item $contents { puts $item } The glob command will generate error messages if the target directory does not exist. A typical error message is shown below, no files matched glob pattern "*" while executing "glob -directory $dir *" invoked from within "puts "Directory contents are:\n[glob -directory $dir *]"" Its often better to use the nocomplain flag to turn-off such messages. [glob -nocomplain -directory $dir *] |
Using Glob RecursivelyThe following recursive procedure ensures that files within the nested sub-directories of the target directory are printed to the console. proc printDir { dir } { set contents [glob -directory $dir *] foreach item $contents { puts $item # recurse - go into the sub directory if { [file isdirectory $item] } { printDir $item } } The use of this proc is shown below, printDir "C:\tcl" For other examples refer to glob_basics.txt |
© 2002- Malcolm Kesson. All rights reserved.