aqTextFile Object

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

Description

The aqTextFile object provides programming access to text files. The objects of this type are returned by the OpenTextFile method.

Members

Example

The sample routines below demonstrate how you can read data from and write it to text files using the aqTextFile object.

The following script routine creates a new text file and then writes text to it:

JavaScript, JScript

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for writing
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine("line1");
  myFile.WriteLine("line2");
  myFile.WriteLine("line3");

  // Closes the file
  myFile.Close();
}

Python

def WriteTextToFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Creates a text file at the specified location 
  aqFile.Create(sPath)
  # Opens the created file for writing 
  myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode)
  # Writes text to the file 
  myFile.WriteLine("line1")
  myFile.WriteLine("line2")
  myFile.WriteLine("line3")
  # Closes the file 
  myFile.Close()

VBScript

Sub WriteTextToFile
  Dim sPath
  sPath = "D:\Files\MyFile.txt"

  ' Creates a text file at the specified location
  Call aqFile.Create(sPath)

  ' Opens the created file for writing
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode)

  ' Writes text to the file
  Call myFile.WriteLine("line1")
  Call myFile.WriteLine("line2")
  Call myFile.WriteLine("line3")

  ' Closes the file
  Call myFile.Close()
End Sub

DelphiScript

procedure WriteTextToFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for writing
  myFile := aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine('line1');
  myFile.WriteLine('line2');
  myFile.WriteLine('line3');

  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile["Create"](sPath);

  // Opens the created file for writing
  var myFile = aqFile["OpenTextFile"](sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile["WriteLine"]("line1");
  myFile["WriteLine"]("line2");
  myFile["WriteLine"]("line3");

  // Closes the file
  myFile["Close"]();
}

The following script routine demonstrates how you can read data from a text file:

JavaScript, JScript

function ReadTextFromFile()
{
  var sPath = "D:\Files\MyFile.txt";

  // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
  Log.Message("File by lines:");

  // Reads text lines from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile.ReadLine();
    Log.Message(s);
  }

  // Closes the file
  myFile.Close();
}

Python

def ReadTextFromFile():
  sPath = "D:\Files\MyFile.txt"
  # Opens the specified file for reading 
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")
  # Reads text lines from the file and posts them to the test log  
  while not myFile.IsEndOfFile():
    s = myFile.ReadLine()
    Log.Message(s)
  # Closes the file 
  myFile.Close()

VBScript

Sub ReadTextFromFile
  Dim sPath
  sPath = "D:\Files\MyFile.txt"

  ' Opens the specified file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")

  ' Reads text lines from the file and posts them to the test log
  While Not myFile.IsEndOfFile()
    s = myFile.ReadLine()
    Log.Message(s)
  WEnd

  ' Closes the file
  Call myFile.Close()
End Sub

DelphiScript

procedure ReadTextFromFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';

  // Opens the specified file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
  Log.Message('File by lines:');

  // Reads text lines from the file and posts them to the test log
  while not myFile.IsEndOfFile() do
    begin
      s := myFile.ReadLine();
      Log.Message(s);
    end;

  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function ReadTextFromFile()
{
  var sPath = "D:\Files\MyFile.txt";

  // Opens the specified file for reading
  var myFile = aqFile["OpenTextFile"](sPath, aqFile.faRead, aqFile.ctUnicode);
  Log["Message"]("File by lines:");

  // Reads text lines from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile["ReadLine"]();
    Log["Message"](s);
  }

  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
aqFile Object
OpenTextFile Method

Highlight search results