12.5.13    file

Description:   Manipulate files, file names and attributes

Syntax:         file option name ?arg arg ...?

         

Argument:     action, option, file names, directory name

Returns:        see below

See Also:      

 

Examples:     

#example 1

file delete -force {c:\webaccess\node\temp.bat}

 

# example 2
# Report1 is a Text File used for report data

# Test if ReportClient2 files exists,

# enter a row of time stamped data

# if not, create it with header lines

if {[file exists "../ReportClient2.txt"]} then {

  set fileid [open "../ReportClient2.txt" a+]

  puts $fileid "[GETVAL %TTMDATE]  [GETVAL %TTMTIME]  [GETVAL AMPLITUDE]  [GETVAL TIMER] "

  close $fileid

 } else {

     set fileid [open "../ReportClient2.txt" a+]

     seek $fileid 0 start

     puts $fileid "----- Water Use Report-------\nPage 1"

     #Insert data into table

     puts $fileid "[GETVAL %TTMDATE]  [GETVAL %TTMTIME]  [GETVAL AMPLITUDE]  [GETVAL TIMER] "

     close $fileid

 }

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.

Option indicates what to do with the file name. Any unique abbreviation for option is acceptable. The valid options are:

Options

file atime name

Returns a decimal string giving the time at which file name was last accessed. The time is measured in the standard POSIX fashion as seconds from a fixed starting time (often January 1, 1970).

If the file doesn’t exist or its access time cannot be queried then an error is generated.

file attributes name

file attributes name ?option?

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

This subcommand returns or sets Windows (Win95, 98, ME, 2003, 2000, XP) platform specific values associated with a file. The first form returns a list of the platform specific flags and their values. The second form returns the value for the specific option. The third form sets one or more of the values. The values are as follows:

-archive gives the value or sets or clears the archive attribute of the file.

-hidden gives the value or sets or clears the hidden attribute of the file.

-longname will expand each path element to its long version. This attribute cannot be set.

-readonly gives the value or sets or clears the read only attribute of the file.

-shortname gives a string where every path element is replaced with its short (8.3) version of the name. This attribute cannot be set.

-system gives or sets or clears the value of the system attribute of the file.

 

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

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

Copy a file or directory from source under the pathname target. If target is an existing directory, then makes a copy inside targetDir of each source file listed. If a directory is specified as a source, then the contents of the directory will be copied into targetDir. Existing files will not be overwritten unless the

-force option is specified. Trying to overwrite a non-empty directory, overwrite a directory with a file, or a file with a directory will all result in errors even if -force was specified. Arguments are processed in the order specified, halting at the first error, if any.

--  (double dashes) mark the end of switches; the argument following the -- will be treated as a source even if it starts with a -.

 

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

Deletes the file or directory specified by each pathname argument. Non-empty directories will be removed only if the -force option is specified. Trying to delete a non-existent file is not considered an error. Trying to delete a read-only file will cause the file to be deleted, even if the -force flag is not specified. Arguments are processed in the order specified, halting at the first error, if any.

-- (double dashes) mark the end of switches; the argument following the -- will be treated as a pathname even if it starts with a - (dash).

 

file dirname name

Returns the directory path to the named file. If name is a relative file name and only contains one path element, then returns ‘‘.’’ If name refers to a root directory, then the root directory is returned. For example,

file dirname c:/

returns c:/.

Note that tilde substitution will only be performed if it is necessary to complete the command. For example,

file dirname ˜/src/foo.c

returns ˜/src, whereas

file dirname ˜

returns /home (or something similar).

 

file executable name

Returns 1 if file name is executable by the current user, 0 otherwise.

 

file exists name

Returns 1 if file name exists and the current user has search privileges for the directories leading to it, 0 otherwise.

 

file extension name

Returns all of the characters after and including the last dot name. If there is no dot, returns the empty string.

 

file isdirectory name

Returns 1 if name is a directory, 0 otherwise.

 

file isfile name

Returns 1 if  name is a regular file, 0 otherwise.

 

file join name ?name ...?

Constructs a valid pathname from multiple elements, using the correct path separator. For example,

file join  /node bgr

returns /node/bgr.

 

file lstat name varName

if name refers to a symbolic link the information returned in varName is for the link rather than the file it refers to. On systems that don’t support symbolic links this option behaves exactly the same as the stat option.

 

file mkdir dir ?dir ...?

Makes the directory specified, for each pathname dir specified. This command will create all non-existing parent directories. If an existing directory is specified, then no action is taken and no error is returned. Trying to overwrite an existing file with a directory will result in an error. Arguments are processed in the order specified, halting at the first error, if any.

 

file mtime name

Returns the time at which file name was last modified (in seconds) from a fixed starting time (usually January 1, 1970).

If the file doesn’t exist or its modified time cannot be queried then an error is generated.

 

file nativename name

Returns the platform-specific name of the file. This is useful if the filename is needed to pass to a platform-specific call, such as exec under Windows 98 or Windows 2003 or Windows 2000 or Windows XP.

 

file owned name

Returns 1 if file name is owned by the current user, 0 otherwise.

 

file pathtype name

Returns absolute, relative, or volume relative. If name refers to a specific file on a specific volume, the path type will be absolute. If name refers to a file relative to the current working directory, then the path type will be relative. If name refers to a file relative to the current working directory on a specified volume, or to a specific file on the current working volume, then the file type is volumerelative.

 

file readable name

Returns 1 if file name is readable by the current user, 0 otherwise.

 

 

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

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

Renames the file or directory specified by source and renames it to target. Moves the file if the pathname target specifies a name in a different directory.

 Existing files will not be overwritten unless the -force option is specified. Trying to overwrite a non-empty directory, overwrite a directory with a file, or a file with a directory will result in errors. Arguments are processed in the order specified, halting at the first error.

-- (double dashes) mark the end of switches; the argument following the -- will be treated as a source even if it starts with a - (dash).

 

file rootname name

Returns file name stripped of its extension:  all of the characters in name up to but not including the last ‘‘.’’ character in name.

 

file size name

Returns a string giving the size of file name in bytes. If the file doesn’t exist or its size cannot be queried then an error is generated.

 

file split name

Returns a list of the path components in name. The first element of the list will have the same path type as name. All other elements will be relative. Path separators will be discarded unless they are needed ensure that an element is unambiguously relative.

file stat name varName

Invokes the stat kernel call on name, and uses the variable given by varName to hold information returned from the kernel call. VarName is treated as an array variable, and the following elements of that variable are set: atime, ctime, dev, gid, ino, mode, mtime, nlink, size, type, uid. Each element except type is a decimal string with the value of the corresponding field from the stat return structure; see the manual entry for stat for details on the meanings of the values. The type element gives the type of the file in the same form returned by the command file type. This command returns an empty string.

 

file tail name

Returns all of the characters in name after the last directory separator. If name contains no separators then returns name.

 

file type name

Returns a string giving the type of file name, which will be one of file, directory, characterSpecial, blockSpecial, fifo, link, or socket.

 

file volume

Will return a list of the available local drives (e.g. {a:/ c:/}).

 

file writable name

Returns 1 if file name is write-able by the current user, 0 otherwise.