File Manipulation APIs

Text Files

_readFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_readFile($filePath[, $encoding])

Arguments
$filePathstring
File path of file to be read as text. File path is resolved relative to current script file.
File path of file to be read as text. File path is resolved relative to files folder of the current project.
$encodingstring optional Encodes the data in file. Defaults to "UTF-8"

Returns
stringContent of the file

Sahi Pro Flowcharts Action :Read File

Details

Reads the file and returns a string.
var $contents = _readFile("users.txt");


_writeFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_writeFile($string, $filePath[, $overwrite[, $encoding]])

Arguments
$stringstring String to write to file.
$filePathstring
File path of file to write into. File path is resolved relative to current script file.
File path of file to write into. File path is resolved relative to files folder of the current project.
$overwriteboolean optionalIf true, overwrites content. Default is false, which appends at the end of existing content.
$encodingstring optionalEncoding to be used while writing. Default is "UTF-8"

Returns
null

Sahi Pro Flowcharts Action :Write File

Details

_writeFile("First Line", "out.txt", true); // overwrites content in file
_writeFile("Second Line", "out.txt"); // appends second line in file


_writeToFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_writeToFile($string, $filePath[, $overwrite[, $encoding]])

Arguments
$stringstring String to write to file.
$filePathstring
File path of file to write into. File path is resolved relative to current script file.
File path of file to write into. File path is resolved relative to files folder of the current project.
$overwriteboolean optionalIf true, overwrites content. Default is false, which appends at the end of existing content.
$encodingstring optionalEncoding to be used while writing. Default is "UTF-8"

Returns
null

Sahi Pro Flowcharts Action :Write To File

Details

This API is the same as _writeFile. Only the name is different.


_deleteFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_deleteFile($filePath)

Arguments
$filePathstring
File path of file to delete. File path is resolved relative to current script file.
File path of file to delete. File path is resolved relative to files folder of the current project.

Returns
booleantrue if the file was deleted on the filesystem, else false

Sahi Pro Flowcharts Action :Delete File

Details

_deleteFile("out.txt"); // deletes out.txt


_fileExists

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.1.07.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_fileExists($filePath)

Arguments
$filePathstring
File path of file to check. File path is resolved relative to current script file.
File path of file to check. File path is resolved relative to files folder of the current project.

Returns
booleantrue if the file exists on the filesystem, else false

Sahi Pro Flowcharts Action :File Exists

Details

_fileExists("out.txt"); // returns true if out.txt exists.


_renameFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_renameFile($oldFilePath, $newFilePath)

Arguments
$oldFilePathstring
File path of file to rename. File path is resolved relative to current script file.
File path of file to rename. File path is resolved relative to files folder of the current project.
$newFilePathstring
New path of file to write into. File path is resolved relative to current script file.
New path of file to write into. File path is resolved relative to files folder of the current project.

Returns
booleantrue if the file was renamed on the filesystem, else false

Sahi Pro Flowcharts Action :Rename File

Details

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

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.07.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_copyFile($srcFilePath, $destFilePath)

Arguments
$srcFilePathstring
File path of an existing file to copy, must not be null. File path is resolved relative to current script file.
File path of an existing file to copy, must not be null. File path is resolved relative to files folder of the current project.
$destFilePathstring
File path of the new file, must not be null. File path is resolved relative to current script file.
File path of the new file, must not be null. File path is resolved relative to files folder of the current project.

Returns
null

Sahi Pro Flowcharts Action :Copy File

Details

copy a file from srcFilePath to destFilePath
_copyFile("source.txt", "dest.txt"); // copies the source.txt into dest.txt


CSV Files

CSV Files are a convenient way to store data. Being text based, they can be easily version controlled and diffed. Sahi has APIs to read and write to CSV Files.

_readCSVFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_readCSVFile($filePath[, $props])

Arguments
$filePathstring
file path to csv file. File path is resolved relative to current script file.
file path to csv file. File path is resolved relative to files folder of the current project.
$propsobject optionalProperties 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 stringsa 2 dimensional array of all values in the csv file

Sahi Pro Flowcharts Action :Read CSVFile

Details

includeHeader default value is changed to false from version 8.0.0. To make previous version scripts continue as it is, change csv.include_header property to true in userdata.properties.
_readCSVFile api expect only one char for Word separator. If $wordSeparator has more than one char, only first char will be taken as $wordSeparator.
Given a userinfo.csv file with data
1,Ram,18,Male
2,Sam,20,Male


var $userinfo = _readCSVFile("data/userinfo.csv");
var $userName = $userinfo[0]; // returns 2,Sam,20,Male since includeHeader default value is false


Given a employee.csv file with data
1,Ram,18,Male
,
2,Sam,20,Male


var $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 false
var $csvLength1 = $employee1.length // returns 3
var $csvLength2 = $employee2.length // returns 1 since ignoreEmptyRows and includeHeader are true


_writeCSVFile

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
3.57.0.07.5.09.0.0NA

Available for modes: Browser | Windows | Java | Android | iOS | SAP

_writeCSVFile($array2d, $filePath[, $overwrite[, $wordSeparator[, $forceQuotes[, $padEmptyCells]]]])

Arguments
$array2d2 dimensional array Two dimensional array of data.
$filePathstring
File path of csv file to write to. File path is resolved relative to current script file.
File path of csv file to write to. File path is resolved relative to files folder of the current project.
$overwriteboolean optional If true, overwrites data. Default is false, which appends data.
$wordSeparatorstring optional Word separator character. Defaults to comma.
$forceQuotesboolean optional Force strings to be inside quotes in the csv file. Defaults to true.
$padEmptyCellsboolean optional 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

Sahi Pro Flowcharts Action :Write CSVFile

Details

    infoNOTE: $forceQuotes was added Since Sahi Pro: 6.2.0. For old document Refer here

                 $padEmptyCells was added Since Sahi Pro: 7.0.0. For old document Refer here

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"