File Functions
Text Files
Section titled “Text Files”_readFile
Section titled “_readFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_readFile($filePath[, $encoding])Arguments
| Name | Type | Description |
|---|---|---|
$filePath | string | File path of file to be read as text. File path is resolved relative to current script file. |
$encoding | stringoptional | Encodes the data in file. Defaults to "UTF-8" |
Returns
string | Content of the file |
Reads the file and returns a string.
var $contents = _readFile("users.txt");_writeFile
Section titled “_writeFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_writeFile($string, $filePath[, $overwrite[, $encoding]])Arguments
| Name | Type | Description |
|---|---|---|
$string | string | String to write to file. |
$filePath | string | File path of file to write into. File path is resolved relative to current script file. |
$overwrite | booleanoptional | If true, overwrites content. Default is false, which appends at the end of existing content. |
$encoding | stringoptional | Encoding to be used while writing. Default is "UTF-8" |
Returns
null |
_writeFile("First Line", "out.txt", true); // overwrites content in file_writeFile("Second Line", "out.txt"); // appends second line in file_writeToFile
Section titled “_writeToFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_writeToFile($string, $filePath[, $overwrite[, $encoding]])Arguments
| Name | Type | Description |
|---|---|---|
$string | string | String to write to file. |
$filePath | string | File path of file to write into. File path is resolved relative to current script file. |
$overwrite | booleanoptional | If true, overwrites content. Default is false, which appends at the end of existing content. |
$encoding | stringoptional | Encoding to be used while writing. Default is "UTF-8" |
Returns
null |
This function is the same as _writeFile. Only the name is different.
_deleteFile
Section titled “_deleteFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_deleteFile($filePath)Arguments
| Name | Type | Description |
|---|---|---|
$filePath | string | File path of file to delete. File path is resolved relative to current script file. |
Returns
boolean | true if the file was deleted on the filesystem, else false |
_deleteFile("out.txt"); // deletes out.txt_fileExists
Section titled “_fileExists”- Since
- Sahi Pro 6.1.0
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_fileExists($filePath)Arguments
| Name | Type | Description |
|---|---|---|
$filePath | string | File path of file to check. File path is resolved relative to current script file. |
Returns
boolean | true if the file exists on the filesystem, else false |
_fileExists("out.txt"); // returns true if out.txt exists._renameFile
Section titled “_renameFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_renameFile($oldFilePath, $newFilePath)Arguments
| Name | Type | Description |
|---|---|---|
$oldFilePath | string | File path of file to rename. File path is resolved relative to current script file. |
$newFilePath | string | New path of file to write into. File path is resolved relative to current script file. |
Returns
boolean | true if the file was renamed on the filesystem, else false |
Renames or moves a file from oldFilePath to newFilePath
_renameFile("original.txt", "renamed.txt"); // renames original.txt to renamed.txt_renameFile("original.txt", "newPath/renamed.txt"); // moves and renames original.txt to newPath/renamed.txt_copyFile
Section titled “_copyFile”- Since
- Sahi Pro 5.1.0
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_copyFile($srcFilePath, $destFilePath)Arguments
| Name | Type | Description |
|---|---|---|
$srcFilePath | string | File path of an existing file to copy, must not be null. File path is resolved relative to current script file. |
$destFilePath | string | File path of the new file, must not be null. File path is resolved relative to current script file. |
Returns
null |
copy a file from srcFilePath to destFilePath
_copyFile("source.txt", "dest.txt"); // copies the source.txt into dest.txtCSV Files
Section titled “CSV Files”CSV Files are a convenient way to store data. Being text based, they can be easily version controlled and diffed. Sahi has functions to read and write to CSV Files.
_readCSVFile
Section titled “_readCSVFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_readCSVFile($filePath[, $props])Arguments
| Name | Type | Description |
|---|---|---|
$filePath | string | file path to csv file. File path is resolved relative to current script file. |
$props | objectoptional | Properties to specify for: wordSeparator: string: Word separator character. Defaults to comma. ignoreEmptyRows: boolean: Ignores the empty lines. Defaults to false. includeHeader: boolean: If true, returns the header row also as the first row. Defaults to false. encoding: string: Encodes the data in csv file. Defaults to "UTF-8". |
Returns
two dimensional array of strings | a 2 dimensional array of all values in the csv file |
Given a userinfo.csv file with data
1,Ram,18,Male2,Sam,20,Malevar $userinfo = _readCSVFile("data/userinfo.csv");var $userName = $userinfo[0]; // returns 2,Sam,20,Male since includeHeader default value is falseGiven an employee.csv file with data
1,Ram,18,Male,2,Sam,20,Malevar $employee = _readCSVFile("data/userinfo.csv");var $employee1 = _readCSVFile("data/userinfo.csv", {includeHeader:true});var $employee2 = _readCSVFile("data/userinfo.csv", {wordSeparator:";",includeHeader:true,ignoreEmptyRows:true});var $employee3 = _readCSVFile("data/userinfo.csv", {encoding:"ISO-8859-1"});var $csvLength = $employee.length // returns 2 since includeHeader default value is falsevar $csvLength1 = $employee1.length // returns 3var $csvLength2 = $employee2.length // returns 1 since ignoreEmptyRows and includeHeader are true_writeCSVFile
Section titled “_writeCSVFile”- Since
- Sahi Pro 3.5
- Desktop Engine 7.0.0
- Mobile Engine 7.5.0
- SAP Engine 9.0.0
Modes BrowserWindowsJavaAndroidiOSSAP
_writeCSVFile($array2d, $filePath[, $overwrite[, $wordSeparator[, $forceQuotes[, $padEmptyCells]]]])Arguments
| Name | Type | Description |
|---|---|---|
$array2d | 2 dimensional array | Two dimensional array of data. |
$filePath | string | File path of csv file to write to. File path is resolved relative to current script file. |
$overwrite | booleanoptional | If true, overwrites data. Default is false, which appends data. |
$wordSeparator | stringoptional | Word separator character. Defaults to comma. |
$forceQuotes | booleanoptional | Force strings to be inside quotes in the csv file. Defaults to true. |
$padEmptyCells | booleanoptional | Empty cells at the end of rows are not trimmed. Ensures an equal number of commas (separators) in each line. Defaults to false. |
Returns
null |
Writes the 2 dimensional array into the csv file.
var $data = [];$data[0] = ["1", "Ram", 18, "Male"];$data[1] = ["2", "Sam", 20];
_writeCSVFile($data, "data/userinfo.csv", false, ",", true, true);//forces data to be inside quotes with specified word separator and empty cells at the end of rows are not trimmed.//appends data into userinfo.csv.
var $moreData = [["3", "Lak", 30, "Male","Married"]];_writeCSVFile($moreData, "data/userinfo.csv", false, ",", true, true);The above code will create data/userinfo.csv with data
"1","Ram","18","Male","""2","Sam","20","","""3","Lak","30","Male","Married"
