Skip to content

File Functions

Pritesh Mhatre edited this page Apr 14, 2020 · 3 revisions

File utility functions for AmiBroker. These functions are available in file-util.afl. To use these functions, add following line at the top of your afl:

#include <file-util.afl>

Functions

fileWriteLine

AmiBroker function to write a given line along with newline character. Returns True on successful write, otherwise False.

Example

result = fileWriteLine("C:\\Users\\demouser\\Documents\\data.txt", "Hello world!!!");
if(result) {
	_TRACE("Successfully wrote a line to file.");
} else {
	_TRACE("Failed to write a line to file.");
}

Parameters

Parameter Type Description
filePath string path of the file
line string the line to write

fileWriteLineAdvanced

AmiBroker function to write a given line along with newline character. Returns True on successful write, otherwise False. This function gives more control over the write line operation. You can customize following options:

  • Decide whether to write fresh or append to existing content
  • Decide whether to open the file in sharing mode
  • File opening may fail due to sharing, so you can specify a retry count

Example

result = fileWriteLineAdvanced("C:\\Users\\demouser\\Documents\\data.txt", "a", True, 2, "Hello world!!!");
if(result) {
	_TRACE("Successfully wrote a line to file.");
} else {
	_TRACE("Failed to write a line to file.");
}

Parameters

Parameter Type Description
filePath string path of the file
mode string access mode ("w" for writing, "a" for appending)
shared string False - open file without checking for sharing, True - open file in share-aware mode
retryCount string how many times to retry (if writing fails)
line string the line to write

fileReadCsvColumn

AmiBroker function to read column value from a CSV file by the given row number. Returns column value if found otherwise blank.

Example

Sample file (C:\Users\user\Desktop\data.csv)

Buggati,Chiron,La Voiture Noire,Divo
Ferrari,F8 Tributo,812 Superfast,Portofino
McLaren,Senna,Speedtail,Elva
result = fileReadCsvColumn("C:\\Users\\user\\Desktop\\data.csv", 1, 1);
_TRACE("Result [row = 1, column = 1] = " + result);
result = fileReadCsvColumn("C:\\Users\\user\\Desktop\\data.csv", 1, 2);
_TRACE("Result [row = 1, column = 2] = " + result);
result = fileReadCsvColumn("C:\\Users\\user\\Desktop\\data.csv", 2, 1);
_TRACE("Result [row = 2, column = 1] = " + result);

// *** Output ***
// Result [row = 1, column = 1] = Buggati
// Result [row = 1, column = 2] = Chiron
// Result [row = 2, column = 1] = Ferrari

Parameters

Parameter Type Description
filePath string path of the file
rowIndex string row index
columnIndex string column index

Clone this wiki locally