File System - Write to file

Declaration

<AMFILESYSTEM ACTIVITY="write_file" FILE="text" DATA="text" ENCODING="text (options)" RESULTVARIABLE="text" REPLACE="text" />

Related Topics    

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:
  • Overwrite file - Existing data will be overwritten.

  • Append at the end of the file (default) - The data will be written to the end of the file as a new line.

Data to write Text Yes (Empty)
  • DATA="The text to write"
  • DATA="%varName%"
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
  • ENCODING="ANSI"
  • ENCODING="unicode"
  • ENCODING="unicodebigendian"
  • ENCODING="UTF8"
The character encoding method to use. The available options are:
  • ANSI (default) - A character encoding of the Latin alphabet, used by default in the legacy components of MS Windows English and some other Western languages.

  • Unicode - A universal character set that defines the characters included in a majority of the languages of the world.

  • Unicode big endian - Also known as UTF-16BE."Big Endian"  means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address.

  • UTF-8 - Uses variable byte to store a Unicode. UTF-8 is suitable for using on Internet, networks or some kind of applications that needs to use slow connection.

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.

Description

Error Causes

On Error

Example

NOTE:
  • 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.

Copy
<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&quot;" RESULTVARIABLE="TheData" />
<AMSHOWDIALOG>%TheData%</AMSHOWDIALOG>