FileLength Method

Applies to TestComplete 14.70, last modified on April 22, 2021

Description

Use the FileLength method to learn the length of the open file. The file length is the total number of characters in a file, including spaces and non-printed symbols. The byte length of a single character depends on the file’s encoding system, which is defined by the first 3 bytes of the file.

Declaration

aqTextFileObj.FileLength()

aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object
Result Integer

Applies To

The method is applied to the following object:

Result Value

An integer that denotes the number of symbols in the file.

Example

The code below obtains the length of the specified file and then posts this file's content to the test log.

JavaScript, JScript

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

   // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Obtains the file length
  var fLen = myFile.FileLength();
  
  // Posts the file content by letters to the test log
  for (var i = 0; i < fLen; i++)
    Log.Message(myFile.ReadString(1));
  
  // Closes the file
  myFile.Close();
}

Python

def ReadFileByLetters():
  sPath = "D:\\Files\\MyFile.txt"
  # Opens the specified file for reading
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  # Obtains the file length
  fLen = myFile.FileLength()
  # Posts the file content by letters to the test log
  for i in range(0, fLen):
    Log.Message(myFile.ReadString(i))
  # Closes the file 
  myFile.Close()

VBScript

Sub ReadFileByLetters
  sPath = "D:\Files\MyFile.txt"

   ' Opens the specified file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  
  ' Obtains the file length
  fLen = myFile.FileLength
  
  ' Posts the file content by letters to the test log
  For i = 0 to fLen-1
    Log.Message(myFile.ReadString(1))
  Next 
  
  ' Closes the file
  myFile.Close()
End Sub

DelphiScript

function ReadFileByLetters;
var sPath, myFile, fLen, i;
begin
  sPath := 'D:\Files\MyFile.txt';

   // Opens the specified file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Obtains the file length
  fLen := myFile.FileLength();
  
  // Posts the file content by letters to the test log
  for i := 0 to fLen-1 do
    Log.Message(myFile.ReadString(1));
  
  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

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

   // Opens the specified file for reading
  var myFile = aqFile["OpenTextFile"]( sPath, aqFile["faRead"], aqFile["ctANSI"] );
  
  // Obtains the file length
  var fLen = myFile["FileLength"]();
  
  // Posts the file content by letters to the test log
  for (var i = 0; i < fLen; i++)
    Log["Message"]( myFile["ReadString"](1) );
  
  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
LinesCount Property

Highlight search results