File Functions - Pritesh-Mhatre/amibroker-library GitHub Wiki

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

fileReadCsvColumnByRowId

Amibroker function to read column value from a CSV file by identifying row by the given row id (value of the first column). 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 = fileReadCsvColumnByRowId("C:\\Users\\mhatr\\Desktop\\data.txt", "Buggati", 1, 2);
_TRACE("Result [row id = Buggati, column = 2] = " + result);
result = fileReadCsvColumnByRowId("C:\\Users\\mhatr\\Desktop\\data.txt", "Ferrari", 1, 3);
_TRACE("Result [row id = Ferrari, column = 3] = " + result);
result = fileReadCsvColumnByRowId("C:\\Users\\mhatr\\Desktop\\data.txt", "McLaren", 1, 4);
_TRACE("Result [row id = McLaren, column = 4] = " + result);

// *** Output ***
// Result [row id = Buggati, column = 2] = Chiron
// Result [row id = Ferrari, column = 3] = 812 Superfast
// Result [row id = McLaren, column = 4] = Elva	

Parameters

Parameter Type Description
filePath string path of the file
rowId string row id (value of the row id index column in the row)
rowIdColumnIndex string index of the column which is to be treated as row id
columnIndex string column index