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.

General Parameters

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 (e.g., %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)

a)DATA="The text to write"

b)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 (e.g., %VarName%).

Advanced Parameters

Property

Type

Required

Default

Markup

Description

Encoding

Text (Options)

No

Unicode

  1. ENCODING="ANSI"

  2. ENCODING="unicode"

  3. ENCODING="unicodebigendian"

  4. 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 the number of bytes written

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 tab - A custom description can be provided on the Description tab to convey additional information or share special notes about a task step.

Error Causes tab - Specify how this step should behave upon the occurrence of an error. (Refer to Task Builder > Error Causes Tab for details.)

On Error tab - Specify what AWE should do if this step encounters an error as defined on the Error Causes tab. (Refer to Task Builder > On Error Tab for details.)

Example

The sample AML code below can be copied and pasted directly into the Steps panel of the Task Builder.

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"></AMVARIABLE>
<AMFILEWRITE FILE="C:\Temp\TestFile.txt" APPEND="NO">I love AutoMate!!!</AMFILEWRITE>
<AMFILEREAD FILE="C:\Temp\TestFile.txt" RESULTVARIABLE="theData" />
<AMSHOWDIALOG>%TheData%</AMSHOWDIALOG>