FileHandler Examples

The FileHandler is a utility object to write or read strings to and from text files.

 

Example 1: Copy a text file to another text file

//Reader

local.fin = FileHandler.OpenReader(efFileIn);

IF(local.fin == NULL)

{

    Form.MessageBox("Error", "Cannot open input file", MB_ICONERROR);

    EXIT;

}

//Writer

local.fout = FileHandler.OpenWriter(efFileOut);

IF(local.fout == NULL)

{

    FileHandler.Close(local.fin);

    Form.MessageBox("Error", "Cannot open output file", MB_ICONERROR);

    EXIT;

}

//Read from source and write destination

local.bContinue = true;

local.result = 0;

WHILE(( local.bContinue ))

{

    IF(bReadLineByLine)

    {

        local.str = FileHandler.ReadLine(local.fin);

    }

    ELSE

    {

        local.str = FileHandler.Read(local.fin, 5);

    }

    IF(local.str <> NULL)

    {

        IF(bReadLineByLine)

        {

            local.result = FileHandler.WriteLine(local.fout, local.str);

        }

        ELSE

        {

            local.result = FileHandler.Write(local.fout, local.str);

        }

        IF(local.result <> 0)

        {

            Form.MessageBox("Error", "Error writing to output file", MB_ICONERROR);

            local.bContinue = false;

        }

    }

    ELSE

    {

        IF(FileHandler.GetReadResult(local.fin) <> 0)

        {

            Form.MessageBox("Error", "Error reading to input file", MB_ICONERROR);

            local.result = -1;

        }

        local.bContinue = false;

    }

}

//Close files

FileHandler.Close(local.fin);

FileHandler.Close(local.fout);

//Show success message

IF(local.result == 0)

{

    local.str = "File copied successfully (IN=" & File.GetFileSize(efFileIn);

    local.str = local.str & ", OUT=" & File.GetFileSize(efFileOut) & ")";

    Form.MessageBox("Success", local.str, MB_ICONEXCLAMATION);

}

Note: