WriteToTextFile Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

The WriteToTextFile method assumes that the file related to the aqFileInfo object is a text file. This method opens the file using the specified character encoding and writes text to it either appending to or replacing the current contents. After the data has been written, the file is closed automatically.

Declaration

aqFileInfoObj.WriteToTextFile(String, TextCodingType, OverwriteOrCreate)

aqFileInfoObj An expression, variable or parameter that specifies a reference to an aqFileInfo object
String [in]    Required    String    
TextCodingType [in]    Required    Integer    
OverwriteOrCreate [in]    Optional    Boolean Default value: False   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

String

The text to be written to the file.

TextCodingType

An integer value that defines a file’s character encoding. You can specify one of the possible integer values directly, or you can use the appropriate constant:

Constant Value Description
aqFile.ctANSI 20 ANSI
aqFile.ctUnicode 21 Unicode (UTF-16)
aqFile.ctUTF8 22 UTF8

OverwriteOrCreate

Specifies whether or not the method will create a new file in place of existing or non-existent files. The following table summarizes possible combinations of the parameter value and file operations:

  OverwriteOrCreate
False (Default) True
The file does not exist Does not create a file and returns False. Creates a file with the specified text.
The file already exists Appends text at the end of the file. Overwrites file contents with the new text.

Result Value

True if the data has been written successfully, and False if an error has occurred (for example, if the specified file does not exist and OverwriteOrCreate is False).

Remarks

Since the aqFileInfo.WriteToTextFile method opens and closes the file automatically, it is better suited for writing text to the file at once in one turn. If you want to write text to the file in several steps, it is recommended to open the file as a text stream using the aqFileInfo.OpenTextFile method and use the resulting aqTextFile object to write the data. This approach provides a better file I/O performance than when calling WriteToTextFile several times in a row.

For example, instead of:

JavaScript, JScript

// Get an aqFileInfo object for an existing file
var oFile = aqFileSystem.GetFileInfo("C:\\MyFile.txt");

// Note: This code opens and closes the file 3 times - once per each WriteToTextFile method call
oFile.WriteToTextFile("one ", aqFile.ctANSI);
oFile.WriteToTextFile("two ", aqFile.ctANSI);
oFile.WriteToTextFile("three ", aqFile.ctANSI);

Python

# Get an aqFileInfo object for an existing file 
oFile = aqFileSystem.GetFileInfo("C:\\MyFile.txt")

# Note: This code opens and closes the file 3 times - once per each WriteToTextFile method call 
oFile.WriteToTextFile("one ", aqFile.ctANSI)
oFile.WriteToTextFile("two ", aqFile.ctANSI)
oFile.WriteToTextFile("three ", aqFile.ctANSI)

VBScript

' Get an aqFileInfo object for an existing file
Dim oFile
Set oFile = aqFileSystem.GetFileInfo("C:\MyFile.txt")

' Note: This code opens and closes the file 3 times - once per each WriteToTextFile method call
Call oFile.WriteToTextFile("one ", aqFile.ctANSI)
Call oFile.WriteToTextFile("two ", aqFile.ctANSI)
Call oFile.WriteToTextFile("three ", aqFile.ctANSI)

DelphiScript

procedure Test;
var oFile;
begin
  // Get an aqFileInfo object for an existing file
  oFile := aqFileSystem.GetFileInfo('C:\MyFile.txt');

  // Note: This code opens and closes the file 3 times - once per each WriteToTextFile method call
  oFile.WriteToTextFile('one ', aqFile.ctANSI);
  oFile.WriteToTextFile('two ', aqFile.ctANSI);
  oFile.WriteToTextFile('three ', aqFile.ctANSI);
end;

C++Script, C#Script

// Get an aqFileInfo object for an existing file
var oFile = aqFileSystem["GetFileInfo"]("C:\\MyFile.txt");

// Note: This code opens and closes the file 3 times - once per each WriteToTextFile method call
oFile["WriteToTextFile"]("one ", aqFile["ctANSI"]);
oFile["WriteToTextFile"]("two ", aqFile["ctANSI"]);
oFile["WriteToTextFile"]("three ", aqFile["ctANSI"]);

use the following code:

JavaScript, JScript

// Get an aqFileInfo object for an existing file
var oFile = aqFileSystem.GetFileInfo("C:\\MyFile.txt");

// Note: This code opens and closes the file only once
var oTextStream = oFile.OpenTextFile(aqFile.faWrite, aqFile.ctANSI);
oTextStream.Write("one ");
oTextStream.Write("two ");
oTextStream.Write("three ");
oTextStream.Close();

Python

# Get an aqFileInfo object for an existing file 
oFile = aqFileSystem.GetFileInfo("C:\\MyFile.txt")

# Note: This code opens and closes the file only once 
oTextStream = oFile.OpenTextFile(aqFile.faWrite, aqFile.ctANSI)
oTextStream.Write("one ")
oTextStream.Write("two ")
oTextStream.Write("three ")
oTextStream.Close()

VBScript

Dim oFile, oTextStream

' Get an aqFileInfo object for an existing file
Set oFile = aqFileSystem.GetFileInfo("C:\MyFile.txt")

' Note: This code opens and closes the file only once
Set oTextStream = oFile.OpenTextFile(aqFile.faWrite, aqFile.ctANSI)
Call oTextStream.Write("one ")
Call oTextStream.Write("two ")
Call oTextStream.Write("three ")
oTextStream.Close

DelphiScript

procedure Test;
var oFile, oTextStream;
begin
  // Get an aqFileInfo object for an existing file
  oFile := aqFileSystem.GetFileInfo('C:\\MyFile.txt');

  // Note: This code opens and closes the file only once
  oTextStream := oFile.OpenTextFile(aqFile.faWrite, aqFile.ctANSI);
  oTextStream.Write('one ');
  oTextStream.Write('two ');
  oTextStream.Write('three ');
  oTextStream.Close;
end;

C++Script, C#Script

// Get an aqFileInfo object for an existing file
var oFile = aqFileSystem["GetFileInfo"]("C:\\MyFile.txt");

// Note: This code opens and closes the file only once
var oTextStream = oFile["OpenTextFile"](aqFile["faWrite"], aqFile["ctANSI"]);
oTextStream["Write"]("one ");
oTextStream["Write"]("two ");
oTextStream["Write"]("three ");
oTextStream["Close"]();

Example

The code below checks file availability. If the file exists, the Hello, World! text line will be added to its contents. Otherwise, the script will throw an exception.

JavaScript

function aqFileInfoExample()
{
 let sPath = "C:\\FileName.txt";
 try 
 {
    //Checks whether the desired file exists
  if (aqFileSystem.GetFileInfo(sPath).Exists)
  {
    aqFileSystem.GetFileInfo(sPath).WriteToTextFile("Hello, World!!", aqFile.ctANSI);
  }
 }
 catch(e)
 {
    // Posts an error message to the test log
   Log.Error(e.message);
 }
}

JScript

function aqFileInfoExample()
{
 var sPath = "C:\\FileName.txt";
 try 
 {
    //Checks whether the desired file exists
  if (aqFileSystem.GetFileInfo(sPath).Exists)
  {
    aqFileSystem.GetFileInfo(sPath).WriteToTextFile("Hello, World!!", aqFile.ctANSI);
  }
 }
 catch(e)
 {
    // Posts an error message to the test log
   Log.Error(e.description);
 }
}

Python

def aqFileInfoExistsExample():
  sPath = "C:\\MyFile.txt"
  try: 
    FileInf = aqFileSystem.GetFileInfo(sPath)
    # Performs actions over the file
    # ...
    # Checks whether the specified file has not been deleted or moved 
    if FileInf.Exists:
      # Writes text to the file 
      FileInf.WriteToTextFile("Hello, world!", aqFile.ctANSI)
  except Exception as e:
    Log.Error("The file has been deleted or removed.")

VBScript

Sub aqFileInfoExample
  Dim sPath, File
  sPath = "C:\asdf\vvasdasd.txt"
  Err.Clear
  On Error Resume Next
    ' Checks whether the desired file exists
    Set File = aqFileSystem.GetFileInfo(sPath)
    If aqFileSystem.GetFileInfo(sPath).Exists Then
    ' Writes text to the file
     Call aqFileSystem.GetFileInfo(sPath).WriteToTextFile("Hello, World!", aqFile.ctANSI)
    End If 
  If Err.Number <> 0 Then
    ' Posts an error message to the test log
    Log.Error Err.Description
  End If
End Sub

DelphiScript

function aqFileInfoExample;
var sPath;

begin

 sPath := 'C:\FileName.txt';
 
 try
 // Checks whether the desired file exists
 if aqFileSystem.GetFileInfo(sPath).Exists then
    // Writes text to the file
    aqFileSystem.GetFileInfo(sPath).WriteToTextFile('Hello, World!', aqFile.ctANSI);
 
 except
    // Posts an error message to the test log
    Log.Error(ExceptionMessage);
 end;
end;

C++Script, C#Script

function aqFileInfoExample()
{
 var sPath = "C:\\FileName.txt";
 try
 {
    // Checks whether the desired file exists
    if (aqFileSystem["GetFileInfo"](sPath)["Exists"])
    {
      // Writes text to the file
      aqFileSystem["GetFileInfo"](sPath)["WriteToTextFile"]( "Hello, World!!", aqFile["ctANSI"] );
    }
 }
 
 catch(e)
 {
    // Posts an error message to the test log
    Log["Error"](e["description"]);
 }
}

See Also

Working With Files From Scripts
ReadWholeTextFile Method
OpenTextFile Method
WriteToTextFile Method

Highlight search results