mIRC Scripting

mIRC: File Handling

In this section you will learn how to manipulate files using mIRC.

All files that are open should be closed at the end of the script to allow other applications to access them.

Opening a File

The following command is used to open a file to be accessed. The name is a reference name used to reference it later in the script. If the file does not exist, this command will fail. This can be avoided with the –n switch, which will create the file prior the opening it. The –o switch is used to override an existing file.

/fopen -no <name> <filename>

Closing a File

This command is used to close the file that was opened. A wildcard name can be specified to close multiple references at once.

/fclose <name | wildcard>

In this example, we will open and close a test file:

Alias Example1 {
  ;Open the file (create if needed)
  fOpen -n Example Test.txt
  ;Close the file (using the reference name)
  fClose Example
}

You will notice, you got two messages similar to these:

* fopen created 'Example' (C:\Users\ZigWap\AppData\Roaming\mIRC\Test.txt)
* fclose closed 'Example' (C:\Users\ZigWap\AppData\Roaming\mIRC\Test.txt)

Writing to a File

With the following command you can write data to a file.

/fwrite -bn <name> <text | &binvar>

The -b Should be used if the data is in binary form. And the –n switch is used to append a $CRLF at the end of the line.

Alias Example2 {
  ;Open the file
  fOpen Example Test.txt
  ;Write some text
  fWrite -n Example This is an example!
  fWrite Example This is another line.
  ;Close the file (using the reference name)
  fClose Example
}

Reading from File

The fRead identifier can be used to read the next line of a file.

$fread(name | N)

The fGetc identifier can be used to read the next character.

$fgetc(name | N)

For 1 or 2 lines you can simple use that identifier by itself, but if you are reading a file with lots of lines, you will need to loop through every line. The next too identifiers can be used to check when it’s the End Of File or if there was an error reading the file.

$feof Can be used to check if we have reached the End Of File.

$ferr Should be checked to see if there was any error.

Alias Example3 {
  fOpen Example Test.txt

  echo -a $fread(Example)
  echo -a $fread(Example)

  
fClose Example
}

Alias Example3 {
  fOpen Example Test.txt
  while (!$feof) && (!$ferr) {
    echo -a $fread(Example)
  }
  fClose Example
}

Other Identifiers/Commands

This identifier is used to get more information on a file suing its reference name.

$fopen(name | N).[fname, pos, eof, err]

The following command can be used to see all the open files.

$fopen(name | N).[fname, pos, eof, err