Processing Files in a Folder

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

One of the common file system operations used in tests is processing files in a folder and performing a certain operation on each file. This topic explains how to do this through scripting. Related file operations are explained in Working With Files From Scripts, Getting File Size, and Reading Text Files.

In TestComplete tests, you can work with files and folders using TestComplete utility objects that provide access to the file system and individual files and folders, such as aqFileSystem, aqFolderInfo, aqFileInfo and others. The two examples below show how you can iterate through all files in a folder and post their names to the test log. The first example uses the file enumerator and the second one accesses files by indexes:

JavaScript, JScript

function ListFiles()
{
  var oFolder, colFiles, f;

  oFolder = aqFileSystem.GetFolderInfo("C:\\");
  colFiles = oFolder.Files;

  while (colFiles.HasNext())
  {
    f = colFiles.Next();
    Log.Message(f.Name);
  }
}

Python

def ListFiles():
    
  oFolder = aqFileSystem.GetFolderInfo("C:\\")
  colFiles = oFolder.Files
  
  while colFiles.HasNext():
    f = colFiles.Next()
    Log.Message(f.Name)

VBScript

Sub ListFiles
  Dim oFolder, colFiles, f

  Set oFolder = aqFileSystem.GetFolderInfo("C:\")
  Set colFiles = oFolder.Files

  While colFiles.HasNext
    Set f = colFiles.Next
    Log.Message f.Name
  Wend
End Sub

DelphiScript

procedure ListFiles;
var oFolder, colFiles, f;
begin
  oFolder := aqFileSystem.GetFolderInfo('C:\');
  colFiles := oFolder.Files;

  while colFiles.HasNext do
  begin
    f := colFiles.Next;
    Log.Message(f.Name);
  end;
end;

C++Script, C#Script

function ListFiles()
{
  var oFolder, colFiles, f;

  oFolder = aqFileSystem["GetFolderInfo"]("C:\\");
  colFiles = oFolder["Files"];

  while (colFiles["HasNext"]())
  {
    f = colFiles["Next"]();
    Log["Message"](f["Name"]);
  }
}

Alternatively, you can work with files and folders using the FileSystemObject scripting object included in Windows. For information on it and its object model, see the FileSystemObject Object article in the MSDN library. The example below shows how you can use the FileSystemObject object to get a list of files in a folder:

JavaScript

function ListFiles()
{
  var fso, folder, myEnum, f;

  fso = getActiveXObject("Scripting.FileSystemObject");
  folder = fso.GetFolder("C:\\");
  for (let f of folder.Files)
  {
    Log.Message(f.Name);
  }
}

JScript

function ListFiles()
{
  var fso, folder, myEnum, f;

  fso = new ActiveXObject("Scripting.FileSystemObject");
  folder = fso.GetFolder("C:\\");
  myEnum = new Enumerator(folder.Files);
  for ( ; !myEnum.atEnd(); myEnum.moveNext())
  {
    f = myEnum.item();
    Log.Message(f.Name);
  }
}

Python

def ListFiles():
  fso = Sys.OleObject["Scripting.FileSystemObject"]
  folder = fso.GetFolder("C:\\")
  myEnum = Utils.Enumerator(folder.Files)
  while not myEnum.atEnd:
    f = myEnum.item;
    Log.Message(f.Name)
    myEnum.Movenext

VBScript

Sub ListFiles
  Dim fso, folder, f

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set folder = fso.GetFolder("C:\")
  For Each f In folder.Files
    Log.Message f.Name
  Next
End Sub 

DelphiScript

procedure ListFiles;
var fso, folder, enum, f;
begin
  fso := Sys.OleObject('Scripting.FileSystemObject');
  folder := fso.GetFolder('C:\');
  enum := Utils.Enumerator(folder.Files);
  while not enum.AtEnd do
  begin
    f := enum.item;
    Log.Message(f.Name);
    enum.MoveNext;
  end;
end;

C++Script, C#Script

function ListFiles()
{
  var fso, folder, myEnum, f;

  fso = new ActiveXObject("Scripting.FileSystemObject");
  folder = fso["GetFolder"]("C:\\");
  myEnum = new Enumerator(folder["Files"]);
  for ( ; !myEnum["atEnd"](); myEnum["moveNext"]())
  {
    f = myEnum["item"]();
    Log["Message"](f["Name"]);
  }
}

See Also

Working With Files From Scripts
Getting File Size
Reading Text Files
About File Checkpoints

Highlight search results