ContainsFile Method

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

Description

The ContainsFile method lets you find out whether the file with the specified name is already referred to by an item that belongs to the Files, Objects or Regions collection.

Declaration

ProgObj.ContainsFile(FileName)

ProgObj An expression, variable or parameter that specifies a reference to one of the objects listed in the Applies To section
FileName [in]    Required    String    
Result Boolean

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameter:

FileName

The full or relative path to the file whose presence in the collection you want to check.

Result Value

True if the given file is stored as an item in the collection and False otherwise.

Example

The following example demonstrates how to add a file to the Stores | Files collection using the methods of the Files object. This example verifies whether the desired file is already in the Files collection, and if it is not there, adds the file to it.

JavaScript, JScript

function StoreFile()
{
  var FileName = "C:\\MyFile.txt";
  var StoredItemName = "MyFile";

  // Verify that StoredItemName is not taken
  if (Files.ContainsFile (FileName))
  {
    Log.Message ("The Files collection already contains this file.");
    return;
  }

  // Add the file to the Files collection
  if (Files.Add(FileName, StoredItemName))
    Log.Message("The file has been successfully saved in Stores.");
  else
    Log.Message (Files.LastError);
}

Python

def StoreFile():
  FileName = "C:\\MyFile.txt"
  StoredItemName = "MyFile"
  # Verify that StoredItemName is not taken
  if Files.Contains(StoredItemName):
    Log.Message ("The Files collection already contains this file.");
    return
  # Add the file to the Files collection
  if Files.Add(FileName, StoredItemName):
    Log.Message("The file has been successfully saved in Stores.")
  else:
     Log.Message (Files.LastError)

VBScript

Sub StoreFile
  Dim FileName, StoredItemName

  FileName = "C:\MyFile.txt"
  StoredItemName = "MyFile"

  ' Verify that StoredItemName is not taken
  If Files.ContainsFile (FileName) Then
    Log.Message ("The Files collection already contains this file.")
  End If

  ' Add the file to the Files collection
  If Files.Add(FileName, StoredItemName) Then
    Log.Message ("The file has been successfully saved in Stores.")
  Else
    Log.Message (Files.LastError)
  End If
End Sub

DelphiScript

procedure StoreFile;
var FileName, StoredItemName;
begin
  FileName := 'C:\MyFile.txt';
  StoredItemName := 'MyFile';

  // Verify that StoredItemName is not taken
  if Files.ContainsFile(FileName) then
    Log.Message ('The Files collection already contains this file.');

  // Add the file to the Files collection
  if Files.Add(FileName, StoredItemName) then
    Log.Message('The file has been successfully saved in Stores.')
  else
    Log.Message (Files.LastError);
end;

C++Script, C#Script

function StoreFile()
{
  var FileName = "C:\\MyFile.txt";
  var StoredItemName = "MyFile";

  // Verify that StoredItemName is not taken
  if (Files["ContainsFile"](FileName))
  {
    Log["Message"]("The Files collection already contains this file");
    return;
  }

  // Add the file to the Files collection
  if (Files["Add"](FileName, StoredItemName))
    Log["Message"]("The file has been successfully saved in Stores.");
  else
    Log["Message"](Files["LastError"]);
}

See Also

Count Method
Contains Method

Highlight search results