File functions

The following functions can be used to read, write, or otherwise handle files:

file_copy (string dest, string src)

Copies the file with the name src to the file with the name dest. If the dest file already exists, it is overwritten.


dest - destination file path (either absolute, or relative to the Zorro folder, e.g. "Data\\Trend.fac").
src - source file path.

file_delete (string name)

Deletes a file.


name - file path

file_length (string name): long

Checks if a file with the given name exists, and returns its length in bytes. If the file was not found, 0 is returned.


name - file path

file_date (string name): long

Checks if a file with the given name exists, and returns its last modification date as the number of seconds since January 1, 1970. If the file was not found, 0 is returned.


name - file path

file_select (string path, string filter): string

Opens a file dialog box at a given directory that lets the user select a file to open. Returns the selected file name including path.


path - initial directory to open, f.i. "Data", or 0 for opening the Zorro directory.
filter - list of pairs of null-terminated filter strings. The last string must be terminated by two null characters ("\0"). The first string in each pair describes the filter (f.i. "Parameter Files"), and the second string specifies the filter pattern (f.i. "*.par"). Multiple filter patterns can be separated with a semicolon (f.i. "*.par;*.fac;*.c"). Do not include spaces in the pattern string. Example of a filter list with three pairs: "All files (*.*)\0*.*\0Par, Fac\0*.par;*.fac\0"Text files"\0*.txt\0\0".

file_next (string name): string

Returns the first file name that matches the given name; if name is 0 or empty, returns the next file name. Allows to enumerate files in a directory.


name - file path with optional wildcards, f.i. "Data\\MyStrategy*.par".

file_content (string name): string (temporary)

Returns a temporary null-terminated string containing the content of the file, or 0 when the file was not found. The string is preserved until the next file_content call.


name - file path

file_read (string name, string content, int length): long

Reads the content of a file into a string or array. Returns the number of read bytes.


name - file path
content - string (or array of any type) to be filled with the content of the file; must have a size of at least length+1.
length - maximum number of characters or bytes to read.

file_write (string name, string content, int length)

Stores the content of a string, series, or other data array in a file.


name - file path
content - string or other data to be written.
length - number of bytes to be written, or 0 for writing the complete content of a string.

file_append (string name, string content, int length)

file_appendfront (string name, string content, int length)

Opens a file and appends text or other data either at the end (file_append) or at the begin (file_appendfront). If the file does not exist, it is created.


name - file name with path.
content - text or other data to be appended at the end or begin of the file.
length - number of bytes to be written. Can be omitted for writing the content of a string.



//script for merging all WFO .par and .fac files
//from two strategies to build a combined strategy
string src1 = "Z1";    // first strategy, can be identical to dest
string src2 = "Z1add"; // second strategy, must be different to dest
string dest = "Z1";    // combined strategy

int file_merge(int n,string ext)
  char name1[40],name2[40],name3[40];
  if(n) {
  } else {
    return 0; // file does not exist
  if(0 != strcmp(name3,name1))
      return 0;
    return 0;
  return 1;

function main()
  int cycles = 1;
  for(; cycles < 100; cycles++)
  cycles += file_merge(0,"par");
  cycles += file_merge(0,"fac");
  if(cycles > 3) 
    printf("%i files combined!",cycles);
// set up strategy parameters from a .ini file
function run()
  static var Parameter1 = 0, Parameter2 = 0;
  if(is(INITRUN)) { // read the parameters only in the first run
    string setup = file_content("Strategy\\mysetup.ini");
    Parameter1 = strvar(setup,"Parameter1");
    Parameter2 = strvar(setup,"Parameter2");
// mysetup.ini is a plain text file that contains
// the parameter values in a format like this:
Parameter1 = 123
Parameter2 = 456

See also:

keys, sprintf, string functions, http functions, ftp functions, putvar

► latest version online