Sahi Pro - File Manipulation APIs
Text Files
_readFile
Since Sahi Pro: 3.5Since Sahi OS: 2
_readFile($filePath)
Arguments
$filePath | string | File path of file ot be read as text |
Details
Reads the file and returns a string. Assumes UTF-8 encoding of file.
Reads the file and returns a string. Assumes UTF-8 encoding of file.
var $contents = _readFile("users.txt");
_writeFile
Since Sahi Pro: 3.5Since Sahi OS: 2
_writeFile($string, $filePath[, $overwrite[, $encoding]])
Arguments
$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 | boolean optional | If true, overwrites content. Default is false, which appends at the end of existing content. |
$encoding | string optional | Encoding to be used while writing. Default is "UTF-8" |
Details
_writeFile("First Line", "out.txt", true); // over writes content in file
_writeFile("Second Line", "out.txt"); // appends second line in file
_writeToFile
Since Sahi Pro: 3.5Since Sahi OS: 2
_writeToFile($string, $filePath[, $overwrite[, $encoding]])
Arguments
$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 | boolean optional | If true, overwrites content. Default is false, which appends at the end of existing content. |
$encoding | string optional | Encoding to be used while writing. Default is "UTF-8" |
Details
This API is the same as _writeFile. Only the name is different.
This API is the same as _writeFile. Only the name is different.
_deleteFile
Since Sahi Pro: 3.5Since Sahi OS: 2
_deleteFile($filePath)
Arguments
$filePath | string | File path of file to delete. File path is resolved relative to current script file. |
Details
_deleteFile("out.txt"); // deletes out.txt
_fileExists
Since Sahi Pro: 6.1.0Since Sahi OS: NA
_fileExists($filePath)
Arguments
$filePath | string | File path of file to check. File path is resolved relative to current script file. |
Details
Returns true if the file in the arguement exists on the filesystem. Otherwise it will return false.
Returns true if the file in the arguement exists on the filesystem. Otherwise it will return false.
_fileExists("out.txt"); // returns true if out.txt exists.
_renameFile
Since Sahi Pro: 3.5Since Sahi OS: 3
_renameFile($oldFilePath, $newFilePath)
Arguments
$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. |
Details
Renames or moves a file from oldFilePath to newFilePath
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 Pro: 5.1.0Since Sahi OS: 5.0
_copyFile($srcFilePath, $destFilePath)
Arguments
$srcFilePath | string | File path of an existing file to copy, must not be null |
$destFilePath | string | File path of the new file, must not be null |
Details
copy a file from srcFilePath to destFilePath
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 Pro: 3.5Since Sahi OS: 2
_readCSVFile($filePath[, $wordSeparator[, $ignoreEmptyRows]])
Arguments
$filePath | string | file path to csv file. |
$wordSeparator | char optional | Word separator character. Defaults to comma. |
$ignoreEmptyRows | boolean optional | ignores the empty lines. Defaults to false. |
Details
Given a userinfo.csv file with data
Given a employee.csv file with data
_readCSVFile api expect only one char for Word separator.
If $wordSeparator has more than one char, only first char will be taken as $wordSeparator.
Returns a 2 dimensional array of all values in the csv file.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][1]; // returns Ram
Given a employee.csv file with data
1,Ram,18,Male
,
2,Sam,20,Male
var $employee = _readCSVFile("data/employee.csv");
var $employee2 = _readCSVFile("data/employee.csv", "," , true);
var $csvLength = $employee.length // returns 3
var $csvLength2 = $employee2.length // returns 2
_writeCSVFile
Since Sahi Pro: 3.5Since Sahi OS: 3
_writeCSVFile($array2d, $filePath[, $overwrite[, $wordSeparator[, $forceQuotes]]])
Arguments
$array2d | 2 dimensional array | Two dimensional array of data. |
$filePath | string | File path of csv file to write to. |
$overwrite | boolean optional | If true, overwrites data. Default is false, which appends data. |
$wordSeparator | string optional | Word separator character. Defaults to comma. |
$forceQuotes | boolean optional | Force strings to be inside quotes in the csv file. Defaults to false. |
Details
Writes the 2 dimensional array into the csv file.
The above code will create
Writes the 2 dimensional array into the csv file.
var $data = [];
$data[0] = ["1", "Ram", 18, "Male"];
$data[1] = ["2", "Sam", 20, "Male"];
_writeCSVFile($data, "data/userinfo.csv", true); // overwrites data into userinfo.csv
var $moreData = [["3", "Lak", 30, "Male"]];
_writeCSVFile($moreData, "data/userinfo.csv"); // appends one more row into userinfo.csv
The above code will create
data/userinfo.csv
with data"1","Ram","18","Male"
"2","Sam","20","Male"
"3","Lak","30","Male"