Write to File
Verb: writeToFile
Writes to a file the text inserted in the Content parameter, returning the file path.
Syntax
writeToFile --value(String) [--createrandomfile(Boolean)] --file(String) --encoding(EncodingType) [--overwrite(Boolean)] [--writeasnewline(Boolean)] [--fileSystem(FileSystem)] (String)=value
Inputs
| Script | Designer | Required | AcceptedTypes | Description |
|---|---|---|---|---|
| --value | Content | Required | Text | Text that should be written to the file. |
| --createrandomfile | Create random file | Optional | Boolean | Creates a file at random, with no fixed location and name. |
| --file | File path | Only whenCreate random file is False | Text | Path to the file the text on Content should be written to. |
| --encoding | Encoding | Required | EncodingType | The character encoding used for writing in the text file:
|
| --overwrite | Overwrite | Optional | Boolean | Overwrites the old content of the file, leaving only the new content in the document. |
| --writeasnewline | New line | Optional | Boolean | Performs the writing on a new line of the file. |
| --fileSystem | File system | Optional | File System | Connection variable to a file system.
1. To obtain the connection variable with an available file system, this connection must be established using one of the following commands: Connect to Dropbox, Connect to Google Drive or Connect to Microsoft Corporation OneDrive, which must be pre-configured in the Web Client. 2. If nothing is specified in this parameter, the local file system of the operating system is used as default. |
Outputs
| Script | Designer | AcceptedTypes | Description |
|---|---|---|---|
| value | Path | Text | Path to the file where the content was written to. |
Example
Example 1: A special directory is prepared for this example, named "exampleFolderToSaveTheFile". The Write to File command is used to write the text "IBM Robotic Process Automation" in a new line of the file. The path to the file is shown on the IBM Robotic Process Automation Studio console, use this path to view the line written to the file.
defVar --name pathToFiles --type String
defVar --name exampleFolder --type String
defVar --name filePath --type String
// Gets the folder to save the file on
getSpecialFolder --folder "MyDocuments" pathToFiles=value
createDir --path "${pathToFiles}\\\\exampleFolderToSaveTheFile" exampleFolder=value
// Writes "IBM Robotic Process Automation" in a ".txt" file
writeToFile --value "IBM Robotic Process Automation" --file "${exampleFolder}\\exampleWriteToFile.txt" --encoding "Default" --overwrite --writeasnewline filePath=value
logMessage --message "File: ${filePath}" --type "Info"
// The result shown is the file path in which the file was saved.
Example 2: Writes the text "IBM-Automation" to a text file that was automatically generated, returning its path.
defVar --name filePath --type String
// Write the text "IBM-Automation" to a randomly created file.
writeToFile --value "IBM-Automation" --createrandomfile --writeasnewline filePath=value
logMessage --message "${filePath}" --type "Info"
// After the script has been executed, the location of the written file is shown.