File System - Write to file
Declaration
<AMFILESYSTEM ACTIVITY="write_file" FILE="text" DATA="text" ENCODING="text (options)" RESULTVARIABLE="text" REPLACE="text" />
Description
Writes the specified text string to a file. If the file does not exist, it will be created at runtime. If the files exists and already contains data, this action can be set to either overwrite the current data or append the new data directly below the existing one. Execution is performed in the background, therefore, the file does not have to be open in order for data to be written to it.
Practical usage
Commonly used to write specific text onto a new text file or modify an existing text file by appending text to existing data or writing new text over the original data contained in that file.
Parameters
General
Property | Type | Required | Default | Markup | Description |
---|---|---|---|---|---|
File | Text | Yes | (Empty) | FILE="c:\sourcefolder\file.txt" | The text file that the data should be written to. If the file does not exist, it will be created and written to at runtime. As with every text parameter, if the data you wish to write to is contained in a variable, simply enter the variable name surrounded by percentage signs to resolve it to it's contents at runtime (for example, %VarName%). |
Method | Yes/No | No | Yes | APPEND="NO" | Indicates
how the data should be written. The available options are:
|
Data to write | Text | Yes | (Empty) |
|
The data to write to the file. As with every text parameter, if the data you wish to write is contained in a variable, simply enter the variable name surrounded by percentage signs to resolve it to it's contents at runtime (for example, %VarName%). |
Advanced
Property | Type | Required | Default | Markup | Description |
---|---|---|---|---|---|
Encoding | Text (Options) | No | Unicode |
|
The
character encoding method to use. The available options are:
|
Populate variable with number of characters written (optional) |
Text |
No |
(Empty) |
RESULTVARIABLE="totalBytes" |
The name of the variable to populate with the number of bytes that were written to the file. |
Replace following text with Null (0x00) character (optional) |
Text |
No |
(Empty) |
REPLACE="text" |
The text to replace with Null (0x00) character. In some programming languages, notably C, null characters are used to mark the end of a character string. Use this parameter if you wish to mark the end of a particular character string. |
Example
- Copy and paste the sample AML code below directly into the Task Builder Steps Panel.
- To successfully run the sample code, update parameters containing user credentials, files, file paths, or other information specific to the task to match your environment.
Description
This task writes contents to C:\Temp\TestFile.txt using the Write to File action and reads the contents into a variable using a Read from File action. The contents are then displayed in a message box.
<AMVARIABLE NAME="TheData" VALUE="" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="C:\Temp\TestFile.txt" DATA="I love Automate!!!" APPEND="no" />
<AMFILESYSTEM ACTIVITY="read_file" FILE="C:\Temp\TestFile.txt"" RESULTVARIABLE="TheData" />
<AMSHOWDIALOG>%TheData%</AMSHOWDIALOG>