Minimizing file names

WJP When you use tk_getSaveFile or tk_getOpenFile to choose a file, the file name returned is a full path name even if the file is in the current directory. This can clutter up messages and entries, so it is often desirable to use only the tail of the file name if it is in the current directory. Here is a little procedure that does this. It takes the full pathname as argument and returns whatever is appropriate.

  proc MinimizeFileName {s} {
     set cwd [pwd];
     set sdir [file dirname $s]
     if {[string equal $cwd $sdir]} {
        return [file tail $s]
     } else {
        return $s;

  # Test:
  catch {console show}
  set fn "D:/devel/tcl-tk/test/MinFilename.tcl"
  puts "$fn = [ MinimizeFileName "$fn" ]"

Category File