Guide to TCL scripting for Eggdrop 1.6

[ Previous ] [ Index ] [ Next ]
[ Text version ]


11. Working with files

In this chapter you will learn how to read from and write to files. Files are more work but with them you preserve your information even after the bot has restarted.

11.1 Opening files

Before you can use a file you will have to open it. This can be done with the open command.
The syntax of an open command is open <filename> [access].

The <filename> is the name of the file you want to open.

The [access] is what you want to do with the file.
Here is a list of different access types according to the man pages of TCL 8.0.4:

  r   Open the file for reading only; the file must already exist.
      This is the default value if access is not specified.

  r+  Open the file for both reading and writing; the file must already exist.

  w   Open the file for writing only. Truncate it if it exists.
      If it doesn't exist, create a new file.

  w+  Open the file for reading and writing. Truncate it if it exists.
      If it doesn't exist, create a new file.

  a   Open the file for writing only. The file must already exist,
      and the file is positioned so that new data is appended to the file.

  a+  Open the file for reading and writing. If the file doesn't exist,
      create a new empty file. Set the initial access position to the end of the file.

The open command command returns a so called channel.
You will need this channel if you want to do something with the file so you have to "catch" this channel.
This can be done by putting the channel in a string. An example would be set fs [open $file r], which would save the channel of $file to $fs.

11.2 Closing files

After you are done with a file you have to close it.
Closing is told right now before reading and writing so that later on complete and better examples can be given, what's the point of opening and closing files if you don't do anything with them, right? :) You can close files with the close command.
The syntax of a close command is close <channel>.

The <channel> is the channel of the file you cought with the open command.

After you've closed a file you will have to re-open it before you can read from and write to it again.

11.3 Reading from files

You can read from a file either per line or per piece.
With the gets command you can read from a file per line.
The syntax of a gets command is gets <channel> [string].

The <channel> is the channel of the file you cought with the open command.

The [string] is the string in which you want to place the retrieved line
If you specify a string the gets command will return how many characters were in the line and put the line in the string, if you do not specify a string the line itself is returned.

A small example:

set fs [open $file r]
gets $fs line(first)
gets $fs line(second)
close $fs

This would put the first line of $file in $line(first) and the second line of $file in $line(second).

You can read a piece of the file with the read command.
The syntax of a read command is read <channel> [bytes].

The <channel> is the channel of the file you cought with the open command.

The [bytes] are the number of bytes you want to read from the file.
If you do not specify the number of bytes the whole file is read.

A small example:

set fs [open $file r]
set info [read $fs 10]
close $fs

This would put the first 10 bytes of $file into $info.

11.4 Writing to files

You can write to a file with the puts command.
The syntax of a puts command is puts <channel> <text>.

The <channel> is the channel of the file you cought with the open command.

The <text> is what you want to write to the file.

A small example:

set fs [open $file w]
puts $fs "$nick"
close $fs

This would write $nick to $file.

11.5 The end of the file

In some cases you might not know how large a file and thus don't know up until where to read from the file.
With the eof command you can find out if the end of the file has been reached.
The syntax of an eof command is eof <channel>.

The <channel> is the channel of the file you cought with the open command.

The eof command returns a 1 if the last command that accessed the channel gave an "end of file" report and a 0 if it didn't.

A way to use the eof command is in a while loop for instance. Here's an example:

set found 0
set fs [open $file r]
while {![eof $fs]} {
  gets $fs line
  if {$line == $nick} { set found 1 }
}
close $fs
if {$found} {
  putdcc $idx "$nick was found!"
} else {
  putdcc $idx "$nick was not found."
}

This would check every line of $file and tell whether or not one of those lines was equal to $nick.

11.6 Checking if a file exists

If you are working with files it is also good to be able to check if a file exists, else you'd have to create an empty file every time you install your script since TCL will return an error if you try to open a file that doesn't exist. This can be done with the file exists command.
The syntax of a file exists command is file exists <filename>.

The <filename> is the name of the file you want to check.
Note that the path from which all external commands or file requests are filed is the directory in which your eggdrop binary resides, so if you want a file from somewhere outside the Eggdrop directory you will have to include the whole path to the file in <filename>.

The file exists command returns 1 if the file exists and a 0 if it doesn't.

11.7 Writing your own script

Lets get back to the auto-voice and greeting script again.
Make it so that everyone who joins one of the two channels gets voiced and that everybody is greeted in every channel with "Welcome $nick" or their personal greeting which can be set with a channel command called !greeting using a file as the database for the greetings.
You can find an example of the script here: example-ch11.tcl.


[ Text version ]
[ Previous ] [ Index ] [ Next ]


Design & Graphics by Shawn Borton of CalcioStar.com