Line Property

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

Description

Use the Line property to set or get the number of the current line. The number is zero-based. To learn the total number of lines in a text file, use the LinesCount property.

Declaration

aqTextFileObj.Line

Read-Write Property Integer
aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object

Applies To

The property is applied to the following object:

Property Value

A zero-based integer that indicates the number of the current line.

If the current column number cannot be determined (for example, when the cursor points to one of the first 3 bytes that indicate character encoding, or the cursor is placed within the newline character), the property returns -1.

Example

The code below obtains the specified file's lines and posts both their numbers and contents to the test log.

JavaScript, JScript

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

   // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Obtains the total number of lines in the file
  var lNum = myFile.LinesCount;
  
  for (var i = 0; i < lNum; i++)
  {
    // Obtains the line number
    var curLine = myFile.Line+1;
    Log.Message("The " + curLine + " line is:");
    // Obtains the current line
    var Line = myFile.ReadLine();
    // Posts the current line to the test log
    Log.Message(Line);
  }
  
  // Closes the file
  myFile.Close();
}

Python

def ReadLineFromTextFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Opens the specified file for reading 
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  # Obtains the total number of lines in the file
  lNum = myFile.LinesCount
  for i in range(0, lNum):
    # Obtains the line number
    curLine = myFile.Line + 1
    Log.Message("The " + str(curLine) + " line is:")
    # Obtains the current line
    Line = myFile.ReadLine()
    # Posts the current line to the test log
    Log.Message(Line)
  # Closes the file 
  myFile.Close()

VBScript

Sub ReadLineFromTextFile

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

   ' Opens the specified file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  
  ' Obtains the total number of lines in the file
  lNum = myFile.LinesCount
  
  For i = 0 to lNum-1
    ' Obtains the line number
    curLine = myFile.Line+1
    Log.Message("The " & curLine & " line is:")
    ' Obtains the current line
    Line = myFile.ReadLine
    ' Posts the current line to the test log
    Log.Message(Line)
  Next
  
  ' Closes the file
  myFile.Close()
  
End Sub

DelphiScript

function ReadLineFromTextFile;
var sPath, myFile, lNum, i, curLine, Line;
begin
  sPath := 'D:\Files\MyFile.txt';

   // Opens the specified file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Obtains the total number of lines in the file
  lNum := myFile.LinesCount;
  
  for i := 0 to lNum-1 do
  begin
    // Obtains the line number
    curLine := myFile.Line+1;
    Log.Message('The ' + IntToStr(curLine) + ' line is:');
    // Obtains the current line
    Line := myFile.ReadLine();
    // Posts the current line to the test log
    Log.Message(Line);
  end;
  
  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

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

   // Opens the specified file for reading
  var myFile = aqFile["OpenTextFile"]( sPath, aqFile["faRead"], aqFile["ctANSI"] );
  
  // Obtains the total number of lines in the file
  var lNum = myFile["LinesCount"];
  
  for (var i = 0; i < lNum; i++)
  {
    // Obtains the line number
    var curLine = myFile["Line"]+1;
    Log["Message"]("The " + curLine + " line is:");
    // Obtains the current line
    var Line = myFile["ReadLine"]();
    // Posts the current line to the test log
    Log["Message"](Line);
  }
  
  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
Column Property
LinesCount Property
Cursor Property
SetPosition Method

Highlight search results