Frag Logo
  Frag Home | Frag SF Project   index | contents | previous

File Handling

Overview

Frag provides a number of commands for file handling. The File object offers a number of methods for manipulating files, as well as a number of class object methods. All file IO commands are loaded into the Frag default configuration.

File: class object methods

File is a class that is used to create file objects, which can be used as file handles. File provides a number of other class object methods:

constructFileName

Syntax

File constructFileName ? file name elements ... ?

Description

constructFileName builds a file name from file name elements that uses the file separator of the operating system.

Example

The following code:
puts [File constructFileName a b]
prints the same like:
puts "[File getSeparator]a[File getSeparator]b"
For instance under UNIX it prints: /a/b

getSeparator

Syntax

File getSeparator

Description

getSeparator returns the file separator of the operating system.

Example

The following code return "\" under Windows and "/" under Linux
puts [File getSeparator]

getUserDir

Syntax

File getUserDir

Description

Returns the current user directory.

Example

The following code prints the current user directory.
puts [File getUserDir]

File: instance methods

File is a class that is used to create file objects, which can be used as file handles. File provides a number methods for its instances.

append

Syntax

<fileInstance> append <content>

Description

append appends the (string) content of content to the file.

Example

The following code creates a file and writes ABC into it. Next, it appends "DEF" to the file. Finally it reads the file. The result should hence be ABCDEF.
set f1 [File create -name __test_a1]
$f1 write "ABC"
$f1 append DEF
puts [$f1 read]

copyDirTo

Syntax

<fileInstance> copyDirTo <targetFileName>

Description

copyDirTo tries to copy a directory to targetFileName. copyDirTo returns true upon success, or false otherwise.

Example

The following code creates a new directory and copies it to __test_a2.
set f1 [File create -name __test_a1 -mkdir]
$f1 copyTo __test_a2

copyTo

Syntax

<fileInstance> copyTo <targetFileName>

Description

copyTo tries to copy a file to targetFileName. copyTo returns true upon success, or false otherwise.

Example

The following code creates a new file and copies it to __test_a2.
set f1 [File create -name __test_a1 -createNewFile]
$f1 copyTo __test_a2

createNewFile

Syntax

<fileInstance> createNewFile

Description

createNewFile tries to create a file with the file name given to the fileInstancecreateNewFile returns true if the file creation was successful, or false if not.

Example

The following code prints 1 0 because it is tried to created the same file name twice. The first time it succeeds, if the file does not exist, the second time it does not succeed, because this time the file exists.
set f1 [File create -name __test_a1]
set f2 [File create -name __test_a1]
puts [list build [$f1 createNewFile] [$f2 createNewFile]]

delete

Syntax

<fileInstance> delete

Description

delete tries to delete a file. delete returns true if the file deletion was successful, or false if not.

Example

The following code prints true false because it is tried to delete the same file name twice. The first time it succeeds; the second time it does not succeed, because this time the file is already deleted.
set f1 [File create -name __test_a1]    
puts [list build [$f1 delete] [$f1 delete]]

deleteDir

Syntax

<fileInstance> deleteDir

Description

deleteDir tries to delete a directory. deleteDir returns true if the directory deletion was successful, or false if not.

Example

The following code prints true false because it is tried to delete the same directory name twice. The first time it succeeds; the second time it does not succeed, because this time the directory is already deleted.
set f1 [File create -name __test_a1]    
puts [list build [$f1 deleteDir] [$f1 deleteDir]]

exists

Syntax

<fileInstance> exists

Description

exists returns true if a file handle points to file that exists, or false if not.

Example

The following code creates a file handle to the current user directory, which should exist, and hence the code prints true.
puts [[File create -name [File getUserDir]] exists]

getAbsoluteFile

Syntax

<fileInstance> getAbsoluteFile

Description

getAbsoluteFile returns the absolute file name, using the separators of the operating system.

Example

The following code creates a file with the name a1 and prints the absolute file name.
File create a1 -name a1
        puts [a1 getAbsoluteFile]

getCanonicalFile

Syntax

<fileInstance> getCanonicalFile

Description

getCanonicalFile does the same as getAbsoluteFile but in addition it canonicalizes the file name, so that specifics of the file part in the file names are not shown. That is, the file names are "absolute and unique". Specifics of file names pointing to the same file like "a1" and "./a1" are not shown.

Example

The following code shows two "absolute and unique" file names, which are identical:
puts [[File create -name ./a1] getCanonicalFile] 
  puts [[File create -name a1] getCanonicalFile]

getParent

Syntax

<fileInstance> getParent

Description

getParent gets the parent directory path of a file name.

Example

The following code shows that getParent of a plain file name just created equals File getUserDir, because the file gets created in the current user directory.
File create a1 -name a1
a1 name [a1 getAbsoluteFile]
puts "[a1 getParent] equals [File getUserDir]"

isDirectory

Syntax

<fileInstance> isDirectory

Description

isDirectory returns true if a file handle points to a directory, or false if not.

Example

The following code prints true because it creates a file handle to the user directory, which is a directory.
puts [[File create -name [File getUserDir]] isDirectory]

length

Syntax

<fileInstance> length

Description

length returns the length of a file.

Example

The following code returns 0 because the length of a new file is always 0.
set f1 [File create -name __test_a1]
$f1 length

listDir

Syntax

<fileInstance> listDir

Description

listDir lists the contents in a directory. Note that there is no particular ordering guaranteed in the resulting list. If needed, consider using list sort.

Example

The following code lists the files (including directory names) in the current user directory.
puts [[File create -name [File getUserDir]] listDir]

mkdir

Syntax

<fileInstance> mkdir

Description

mkdir tries to create a directory with the file name given to the fileInstancemkdir returns true if the creation was successful, or false if not.

Example

The following code prints true false because it is tried to created the directory with the same file name twice. The first time it succeeds, if the file does not exist; the second time it does not succeed, because this time the directory exists.
set f1 [File create -name __test_a1]
set f2 [File create -name __test_a1]
puts [list build [$f1 mkdir] [$f2 mkdir]]

mkdirs

Syntax

<fileInstance> mkdirs

Description

mkdirs tries to create a directory with the file name given to the fileInstancemkdirs returns true if the creation was successful, or false if not. In contrast to mkdir, mkdir create the directory including any necessary but nonexistent parent directories.

Example

The following code prints true false because it is tried to created the directory with the same file name twice. The first time it succeeds, if the file does not exist; the second time it does not succeed, because this time the directory exists. Please note that the parent directories are created during creation of y.
set f1 [File create -name __test_a1/x/y]
set f2 [File create -name __test_a1/x/y]
puts [list build [$f1 mkdir] [$f2 mkdir]]

name

Syntax

<fileInstance> name ?fileName?

Description

name must be used upon file creation to specify the file name of a file handle. If the fileName argument is omitted, the file name is read.

Example

The following code name to specify a file name upon creation, then it reads it, changes it, and reads it again. The result is a1 a2.
File create a1 -name a1
puts [a1 name]
a1 name a2
puts [a1 name]

read

Syntax

<fileInstance> read ?encoding?

Description

read reads the content of a file and returns it (as a string). Optionally, the encoding can be given, such as "8859_1" or "UTF8".

Example

The following code creates a file and writes ABC into it. Next, it reads the file. The result should hence be ABC.
set f1 [File create -name __test_a1]
$f1 write "ABC"
puts [$f1 read]

renameTo

Syntax

<fileInstance> renameTo <targetFileName>

Description

renameTo tries to rename a file to targetFileName. renameTo returns true upon success, or false otherwise.

Example

The following code creates a new file and renames it to __test_a2.
set f1 [File create -name __test_a1 -createNewFile]
$f1 renameTo __test_a2

write

Syntax

<fileInstance> write <content> ?encoding?'

Description

write writes the (string) content of content into the file. Optionally, the encoding can be given, such as "8859_1" or "UTF8".

Example

The following code creates a file and writes ABC into it. Next, it reads the file. The result should hence be ABC.
set f1 [File create -name __test_a1]
$f1 write "ABC"
puts [$f1 read]

  index | contents | previous