aqFileSystem.Exists Method

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

Description

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

Declaration

aqFileSystem.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 drive, folder or file to be checked. Only fully qualified paths are acceptable. For example, "C:","C:\TMP", "C:\TMP\myfile.txt".

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 drive, folder or file 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 FileExistsExample()
{
  var sPath;
  sPath = "c:\\MyFile.txt";

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

Python

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

VBScript

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

DelphiScript

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

C++Script, C#Script

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

See Also

Working With Files From Scripts
Exists Method

Highlight search results