aqFile.Exists Method

Applies to TestComplete 15.63, last modified on April 10, 2024

Description

Use the aqFile.Exists method to check whether the specified file, folder or drive exists.

Declaration

aqFile.Exists(Path)

Path [in]    Required    String    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Path

Specifies the fully-qualified path to the file, folder or drive to be checked.

If Path specifies a folder path, it may or may not include a trailing backslash (\). You can add a trailing backslash to or remove it from the path by using the IncludeTrailingBackSlash or ExcludeTrailingBackSlash method.

Result Value

True if the specified file, folder or drive was found. Otherwise - False.

Example

The code below checks whether the file exists. If it does, the routine writes a string to the file. Otherwise, it creates the specified file and fills it with the desired text.

JavaScript, JScript

function Test()
{
  var sPath;
  sPath = "c:\\MyFile.txt";

  if (!aqFile.Exists(sPath))
    aqFile.Create(sPath);
  aqFile.WriteToTextFile(sPath, "Hello, world!" + "\r\n", aqFile.ctANSI, false);
}

Python

def Test():
  sPath = "c:\\MyFiles\\MyFile.txt"
  if not aqFile.Exists(sPath):
    aqFile.Create(sPath)
  aqFile.WriteToTextFile(sPath, "Hello, world!" + "\r\n", aqFile.ctANSI, False)

VBScript

Sub Test
  Dim sPath
  sPath = "c:\MyFile.txt"
  
  If Not aqFile.Exists(sPath) Then 
    Call aqFile.Create(sPath)
  End If 
  Call aqFile.WriteToTextFile(sPath, "Hello, world!" & vbCrlf , aqFile.ctANSI, False)
End Sub

DelphiScript

procedure Test;
var 
  sPath;
begin
  sPath := 'c:\MyFile.txt';
  
  if not aqFile.Exists(sPath) then 
    aqFile.Create(sPath);
  aqFile.WriteToTextFile(sPath, 'Hello, world!' + #13#10, aqFile.ctANSI, false);
end;

C++Script, C#Script

function Test()
{
  var sPath;
  sPath = "c:\\MyFile.txt";
  
  if (!aqFile["Exists"](sPath))
    aqFile["Create"](sPath);
  aqFile["WriteToTextFile"](sPath, "Hello, world!" + "\r\n", aqFile["ctANSI"], false);
}

See Also

Working With Files From Scripts
Create Method
Rename Method
Copy Method
Exists Method

Highlight search results