Reading Text Files

Applies to TestComplete 15.70, last modified on December 17, 2024

There are at least three different ways to read data from a text file. First of all, you can use the special aqFile and aqTextFile objects that are provided by TestComplete. Second, you can obtain the Scripting.FileSystemObject object and use its methods to work with any file system object, including text files. For a complete description of the object and its methods, see the documentation on the FileSystemObject object in the MSDN library. Finally, you can use native routines of DelphiScript (a list of the routines is given in the File and Folder Routines topic). The first two techniques are more versatile, since the corresponding objects are available for all the supported scripting languages.

The sections below demonstrate each of the above-mentioned techniques:

  • Reading a text file using the aqFile and aqTextFile objects

    The ReadWholeFile routine reads text from a file as a single string and posts it to the test log. The ReadFileLines routine reads text line by line and posts the retrieved lines to the log.

    JavaScript, JScript

    function ReadWholeFile(AFileName)
    {
      var s = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI);
      Log.Message("File entire contents:");
      Log.Message(s);
    }

    function ReadFileLines(AFileName)
    {
      var F, s;
       
      F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI);
      F.Cursor = 0;
      Log.Message("File by lines:");
      while(! F.IsEndOfFile()){
        s = F.ReadLine();
        Log.Message(s);
      }
      F.Close();
    }

    Python

    def ReadWholeFile(AFileName):
      s = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI)
      Log.Message("File entire contents:")
      Log.Message(s)
    
    def ReadFileLines(AFileName):
      F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI)
      F.Cursor = 0
      Log.Message("File by lines:")
      while not F.IsEndOfFile():
        s = F.ReadLine()
        Log.Message(s)
      F.Close()

    VBScript

    Sub ReadWholeFile(AFileName)
      s = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI)
      Log.Message("File entire contents:")
      Log.Message(s)
    End Sub

    Sub ReadFileLines(AFileName)
      Set F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI)
      F.Cursor = 0
      Log.Message("File by lines:")
      While Not F.IsEndOfFile()
        s = F.ReadLine()
        Log.Message(s)
      WEnd
      F.Close()
    End Sub

    DelphiScript

    procedure ReadWholeFile(AFileName);
    var s: String;
    begin
      s := aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI);
      Log.Message('File entire contents:');
      Log.Message(s);
    end;

    procedure ReadFileLines(AFileName);
    var F: OleVariant;
        s: String;
    begin
      F := aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI);
      F.Cursor := 0;
      Log.Message('File by lines:');
      while not F.IsEndOfFile() do
        begin
          s := F.ReadLine();
          Log.Message(s);
        end;
      F.Close();
    end;

    C++Script, C#Script

    function ReadWholeFile(AFileName)
    {
      var s;
      s = aqFile["ReadWholeTextFile"](AFileName, aqFile["ctANSI"]);
      Log["Message"]("File entire contents:");
      Log["Message"](s);
    }

    function ReadFileLines(AFileName)
    {
      var F, s;
       
      F = aqFile["OpenTextFile"](AFileName, aqFile["faRead"], aqFile["ctANSI"]);
      F.Cursor = 0;
      Log["Message"]("File by lines:");
      while(! F["IsEndOfFile"]()){
        s = F["ReadLine"]();
        Log["Message"](s);
      }
      F["Close"]();
    }

  • Reading a text file using FileSystemObject

    The following routine reads text lines from a file and posts them to the test log.

    JavaScript

    function ReadFile(AFileName)
    {
      const ForReading = 1;
      const ForWriting = 2;
      const ForAppending = 8;
      let FS = getActiveXObject("Scripting.FileSystemObject");

      let F = FS.OpenTextFile(AFileName, ForReading);
      while(! F.AtEndOfStream){
        let s = F.ReadLine();
        Log.Message(s);
      }
      F.Close();
    }

    JScript

    function ReadFile(AFileName)
    {
      var ForReading = 1;
      var ForWriting = 2;
      var ForAppending = 8;
      var FS = Sys.OleObject("Scripting.FileSystemObject");
      // Note that you can create FileSystemObject
      // using the following code:
      // var FS = new ActiveXObject("Scripting.FileSystemObject")
      var F = FS.OpenTextFile(AFileName, ForReading);
      var s;
      while(! F.AtEndOfStream){
        s = F.ReadLine();
        Log.Message(s);
      }
      F.Close();
    }

    Python

    def ReadFile(AFileName):
      ForReading = 1
      ForWriting = 2
      ForAppending = 8
      FS = Sys.OleObject["Scripting.FileSystemObject"]
      F = FS.OpenTextFile(AFileName, ForReading)
      while not F.AtEndOfStream:
        s = F.ReadLine()
        Log.Message(s)
    
      F.Close()

    VBScript

    Sub ReadFile(AFileName)
      ForReading = 1
      ForWriting = 2
      ForAppending = 8
      Set FS = Sys.OleObject("Scripting.FileSystemObject")
      Set F = FS.OpenTextFile(AFileName, ForReading)
      While Not F.AtEndOfStream
        s = F.ReadLine
        Log.Message s
      WEnd
      F.Close
    End Sub

    DelphiScript

    procedure ReadFile(AFileName : String);
    const
      ForReading = 1;
      ForWriting = 2;
      ForAppending = 8;
    var
      FS, f, s : OleVariant;
    begin
      FS := Sys.OleObject('Scripting.FileSystemObject');
      F := FS.OpenTextFile(AFileName, ForReading);
      while not F.AtEndOfStream do
      begin
        s := F.ReadLine;
        Log.Message(s);
      end;
      F.Close;
    end;

    C++Script, C#Script

    function ReadFile(AFileName)
    {
      var FS, F, s;
      var ForReading = 1;
      var ForWriting = 2;
      var ForAppending = 8;
      FS = Sys["OleObject"]("Scripting.FileSystemObject");
      // Note that you can create FileSystemObject
      // using the following code:
      // FS = new ActiveXObject("Scripting.FileSystemObject")
      F = FS["OpenTextFile"](AFileName, ForReading);
      while(! F["AtEndOfStream"])
      {
        s = F["ReadLine"]();
        Log["Message"](s);
      }
      F["Close"]();
    }

  • Reading a text file using DelphiScript routines

    The following script routine reads text lines from a file and posts them to the test log.

    DelphiScript

    procedure ReadFile(AFileName : String);
    var
      FileVar, s : OleVariant;
    begin
      AssignFile(FileVar, AFileName);
      Reset(FileVar);
      while not Eof(FileVar) do
      begin
        Readln(FileVar, s);
        Log.Message(s);
      end;
      CloseFile(FileVar);
    end;

See Also

Working With External Data Sources
Processing Files in a Folder
Getting File Size
About File Checkpoints

Highlight search results