File and process functions#
These functions read and write files and run programs on the machine that runs the script. You can
use them in 9Login scripts and when you test in Studio. In 9Hits campaign macros,
every function on this page is blocked and returns {error: "Blocked"}.
Warning
These functions act for real on your machine and cannot be undone. Always use absolute paths,
and double-check any script that contains DelFileOrDir or LaunchProcess before you run it.
The functions on this page finish right away, so they do not need await. Most of them return an
object: the result is in result and the error is in error.
ReadFileAsString #
Reads an entire text file (UTF-8) into a string. For binary files such as images, use
ReadFileAsBytes, because reading them with this function corrupts the data.
Parameters
| Name | Description |
|---|---|
path |
Absolute path to the file, for example "C:/data/list.txt". If it is empty or not a string, the function returns {error}. |
Returns
{result: string} with the file content when the read succeeds; {error} on failure, with the
reason in error.
Example
ReadFileAsBytes #
Reads an entire file as binary data, for images and other non-text files. The whole file is loaded into memory at once, so avoid using it on very large files.
Parameters
| Name | Description |
|---|---|
path |
Absolute path to the file, for example "C:/data/image.png". If it is empty or not a string, the function returns {error}. |
Returns
{result: ArrayBuffer} when the read succeeds; {error} on failure. result is not a string: to
print it or send it somewhere as a string, convert it with Base64Encode.
Example
WriteFile #
Writes data to a file: creates the file if it does not exist, and overwrites it completely if it does. Missing parent folders are created too. The function has no append mode; to add to the end of a file, read the old content, append to it and write it back (see the example).
Parameters
| Name | Description |
|---|---|
path |
Absolute path of the file to write. If it is empty or not a string, the function returns {error}. |
data |
Content to write: a string, or an ArrayBuffer for binary data. Numbers and objects are not converted automatically; they are rejected with {error}. Convert them to a string first, for example with JSON.stringify. |
Returns
{error} on failure; no error means the write is done.
Example
TouchFile #
Sets the modification time and access time of a file to the current time without changing its
content. The file must already exist: the function does not create a new file, it returns {error}.
Parameters
| Name | Description |
|---|---|
path |
Absolute path to an existing file. |
Returns
{error} on failure, including when the file does not exist; no error means the times were
updated.
Example
ReadDir #
Lists the files and subfolders directly inside a folder. Each item is a full path, not just a name.
The function does not tell you which items are folders; to find out, call
GetFileInfo on each item.
Parameters
| Name | Description |
|---|---|
path |
Absolute path of the folder to list. |
Returns
{result: string[]}, an array of full paths, when the read succeeds; {error} on failure.
Example
MkDir #
Creates a folder, including any missing parent folders. A folder that already exists does not count as an error.
Parameters
| Name | Description |
|---|---|
path |
Absolute path of the folder to create. |
Returns
{error} on failure; no error means the folder exists (just created or already there).
Example
DelFileOrDir #
Deletes a file, or a folder and everything inside it. The function deletes right away, without asking and without undo, so double-check the path before you call it.
Parameters
| Name | Description |
|---|---|
path |
Absolute path of the file or folder to delete. |
Returns
{error} on failure; no error means it was deleted.
Example
CopyFileOrDir #
Copies a file, or a whole folder and everything inside it, from src to dst.
Parameters
| Name | Description |
|---|---|
src |
Absolute path of the source file or folder. |
dst |
Absolute path of the copy. If it is the same as src, the function returns {error}. |
Returns
{error} on failure; no error means the copy is done.
Example
MoveFileOrDir #
Moves a file or folder from src to dst. This is also how you rename: move it to a new name in the
same folder.
Parameters
| Name | Description |
|---|---|
src |
Absolute path of the file or folder to move. |
dst |
New absolute path. If it is the same as src, the function returns {error}. |
Returns
{error} on failure; no error means the move is done.
Example
PathExists #
Checks whether a file or folder exists. The function does not tell files from folders; to find out,
use GetFileInfo.
Parameters
| Name | Description |
|---|---|
path |
Absolute path to check. |
Returns
true or false. The exception: when the path is empty or not a string, the function returns an
{error} object, not false. An object always counts as true in an if, so compare with === true.
Example
GetFileInfo #
Reads information about a file or folder: size, type and timestamps. If the path does not exist, the
function returns {error}, so you can use it both to check that a path exists and to tell whether it
is a file or a folder.
Parameters
| Name | Description |
|---|---|
path |
Absolute path of the file or folder. |
Returns
{result: {size, isDirectory, isSymbolicLink, creationTime, lastModified, lastAccessed}} when the
read succeeds; {error} on failure or when the path does not exist.
size: size in bytes.isDirectory,isSymbolicLink:trueorfalse.creationTime,lastModified,lastAccessed: time in seconds since 1970.
Example
GetCurrentDir #
Returns the working directory of the session that runs the script.
Returns
A path string, not wrapped in an object like the other functions on this page.
GetHomeDir #
Returns the user's home directory on the machine, for example C:\Users\<name> on Windows.
Returns
A path string, not wrapped in an object.
Example
GetDownloadDir #
Returns the folder where the session saves downloaded files.
Returns
A path string, not wrapped in an object; null when the session has no download folder of its own.
Example
LaunchProcess #
Runs a program on the machine, for example a command-line tool or an application. You can start it and move on right away, or wait for the program to finish and get what it printed. While it waits, the script stops at that line until the program exits, so do not wait for a program that does not exit on its own (such as Notepad).
Parameters
| Name | Description |
|---|---|
cmd |
Program to run: a path to an executable, or a program name such as "notepad.exe". |
args |
Array of command-line arguments, one argument per item, for example ["/c", "dir"]. With no arguments, pass []. If it is not an array, the function returns {error}. |
wait |
true to wait for the program to finish before moving on; false to start it and move on right away. Default false. |
Returns
{result} or {error}. When wait is true, result is what the program printed.