Version 39 of file

Updated 2005-02-19 16:16:12 by LES

file - Manipulate file names and attributes

http://purl.org/tcl/home/man/tcl8.4/TclCmd/file.htm

file option name ?arg arg ...?

This command provides several operations on a file's name or attributes. Name is the name of a file; if it starts with a tilde, then tilde substitution is done before executing the command (see the manual entry for filename for details). Option indicates what to do with the file name. Any unique abbreviation for option is acceptable. The valid options are: (see complete man page; here's only notes on selected options).


file atime name ?time?

file attributes name

file attributes name ?option?

file attributes name ?option value option value...?

file channels ?pattern?

file copy ?-force? ?--? source target

file copy ?-force? ?--? source ?source ...? targetDir

file delete ?-force? ?--? pathname ?pathname ... ?

file dirname name

file executable name

file exists name

file extension name

file isdirectory name

file isfile name

file join name ?name ...?

file link ?-linktype? linkName ?target? (requires Tcl8.4)

file lstat name varName

file mkdir dir ?dir ...?

file mtime name ?time?

file nativename name

file normalize name (requires Tcl8.4)

file owned name

file pathtype name

file readable name

file readlink name

file rename ?-force? ?--? source target

file rename ?-force? ?--? source ?source ...? targetDir

file rootname name

file separator ?name?

file size name

file split name

file stat name varName

file system name (requires Tcl8.4)

file tail name

file type name

file volumes

file writable name


[Hopefully someone will provide some samples to show how these are useful. With the new VFS support in Tcl 8.4, and an appropriate extension, file can operate on much more than just local files (see the One-line web browser in Tcl for example).]


Can someone comment on what happens if the current working directory is on a different drive?

Yes. The question is essentially nonsensical because the current working directory is a per-drive attribute. What happens if I try to open the door, but the other door is open?

Hmm , so the following sequence, in Windows, is nonsense?

 cd C:/tcl/lib
 set fd [open "G:list.txt" "w"]
 set lst [glob *]
 puts $fd $lst
 close $fd

Interesting- I never realized that Windows was so brain-damaged.


LES: One may want to use this proc to catch a quick grasp and/or memorize all the file commands:

 proc  fileinfo  { myFile }          {
        puts  "file name: $myFile"
        puts  "exists: [ file  exists  $myFile ]"
        puts  "----------------"
        puts  "type: [ file  type  $myFile ]"
        puts  "size: [ file  size  $myFile ]"
        puts  ""
        puts  "atime: [ file  atime  $myFile ]"
        puts  "mtime: [ file  mtime  $myFile ]"
        puts  ""
        puts  "pathtype: [ file  pathtype  $myFile ]"
        puts  "dirname: [ file  dirname  $myFile ]"
        puts  "separator: [ file  separator  $myFile ]"
        puts  "nativename: [ file  nativename  $myFile ]"
        puts  "normalize: [ file  normalize  $myFile ]"
        puts  "rootname: [ file  rootname  $myFile ]"
        puts  "tail: [ file  tail  $myFile ]"
        puts  "extension: [ file  extension  $myFile ]"
        puts  "join: [ file  join  $myFile ]"
        puts  ""
        puts  "attributes: [ file  attributes  $myFile ]"
        puts  "owned: [ file  owned  $myFile ]"
        puts  "readable: [ file  readable  $myFile ]"
        puts  "writable: [ file  writable  $myFile ]"
        puts  "executable: [ file  executable  $myFile ]"
        puts  ""
        puts  "channels: [ file  channels  $myFile ]"
        puts  "system: [ file  system  $myFile ]"
 }

run it: $ fileinfo somefile.txt


See also:


Tcl syntax help - Arts and Crafts of Tcl-Tk Programming - Category Command - Additional file commands