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.

Parameters:

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.

Parameters:

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.

Parameters:

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.

Parameters:

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.

Parameters:

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_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.

Parameters:

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.

Parameters:

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 resp. 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.

Parameters:

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)

Opens a file and appends text or other data to it; can be used to export data to Excel or other programs. If the file does not exist, it is created.

Parameters:

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

Remarks:

Examples:

//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) {
    sprintf(name1,"Data\\%s_%i.%s",src1,n,ext);
    sprintf(name2,"Data\\%s_%i.%s",src2,n,ext);
    sprintf(name3,"Data\\%s_%i.%s",dest,n,ext);
  } else {
    sprintf(name1,"Data\\%s.%s",src1,ext);
    sprintf(name2,"Data\\%s.%s",src2,ext);
    sprintf(name3,"Data\\%s.%s",dest,ext);  
  }
  if(!file_date(name1)) 
    return 0; // file does not exist
  if(0 != strcmp(name3,name1))
    if(!file_copy(name3,name1))
      return 0;
  if(!file_append(name3,file_content(name2))) 
    return 0;
  return 1;
}

function main()
{
  int cycles = 1;
  for(; cycles < 100; cycles++)
    if(!file_merge(cycles,"par")) 
      break;
  
  cycles += file_merge(0,"par");
  cycles += file_merge(0,"fac");
  
  if(cycles > 3) 
    printf("%i files combined!",cycles);
  else
    printf("Error!");
}
// 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