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. If Dest is a folder ending with a backslash '\', the file is copied into the destination folder. This function can also be used to rename files by storing them under a different name.


Dest - destination folder or file path (either absolute, or relative to the Zorro folder, e.g. "Log\\MyStrategy.txt").
Src - source file path

file_delete (string name)

Deletes a file.


name - file path

file_length (string name): size_t

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 in UTC as the number of seconds since January 1, 1970. The mtu function can convert it to the DATE format. 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 name to open or save. Returns the selected file name including path. The returned string keeps its content until the next file_select call.


path - initial directory to open, f.i. "Data", or 0 for opening the Zorro directory.
filter - list of pairs of null-terminated filter strings. If the first string begins with '#', it generates a save dialog, otherwise an open dialog. 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"). Example: "CSV Files\0*.csv\0\0". Multiple filter patterns can be separated with a semicolon (f.i. "*.par;*.fac;*.c"). The pattern must not contain spaces. Example of a filter list with three pairs: "All files (*.*)\0*.*\0Par, Fac\0*.par;*.fac\0Text files\0*.txt\0\0".

file_next (string path): string

Enumerate files in a directory. Returns the first file name (without directory path) that matches the given path. Returns the next match when path is 0 or an empty string. Returns 0 after the last matching file. 


path - file path with wildcards ("Data\\MyStrategy*.par") for the first file; 0 for subsequent files.

file_content (string name): string (temporary)

Returns a null-terminated temporary string with the content of the file, or 0 when the file was not found. The string keeps its content only until the next file_content call. For multiple files to be read at the same time, allocate buffers and use file_read.


name - file path

file_read (string name, string content, size_t length): size_t

Reads the content of a file into a string or array, and appends 0 at the end. 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, or 0 for reading the whole file into a buffer of sufficient size. 

file_write (string name, string content, size_t 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, size_t length)

file_appendfront (string name, string content, size_t 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, or 0 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