aqFileSystem.DeleteFolder Method

Applies to TestComplete 14.10, last modified on June 5, 2019

Description

Use the DeleteFolder method to delete one or more folders.

Declaration

aqFileSystem.DeleteFolder(PathRemoveNonEmpty)

Path [in]    Required    String    
RemoveNonEmpty [in]    Optional    Boolean Default value: False   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Path

The fully qualified path to the folder to be deleted. To delete several folders, use wildcards (* and ?) to specify the mask. Note, that wildcards are only allowed in the last component of the path.

The path may or may not include a trailing backslash (\).

An empty string, Empty (in VBScript) or undefined (in JavaScript, JScript, C++Script and C#Script) means the current working folder, which can be read and set by using the aqFileSystem.GetCurrentFolder and aqFileSystem.SetCurrentFolder methods, respectively.

RemoveNonEmpty

Specifies whether the method should remove non-empty folders. If the parameter is False, the method removes a folder only if it does not contain any files. If the parameter is True, the method removes non-empty folders as well.

Result Value

True if all the specified folders were deleted successfully, and False otherwise.

If the specified folder was not found, the method returns False.

Remarks

If the method fails to delete the folder, TestComplete will post an information message to the test log explaining the cause of the failure.

aqFileSystem.DeleteFile cannot be used to delete the current project folder.

Example

The code below creates a folder, adds some files to it and then deletes this folder with all the files at the end of the script.

JavaScript

function Test()
{
  var strTmpFolder = Sys.OSInfo.TempDirectory;
  // Generate a temporary folder name
  var strTempName = getActiveXObject("Scripting.FileSystemObject").GetTempName();
  var strFolder = strTmpFolder + strTempName;

  // Create a temporary folder and check if it was created successfully
  if (aqFileSystem.CreateFolder(strFolder) == 0)
  {
    Log.Message("Created new folder " + strFolder + ".");

    // Add files to the folder
    aqFile.Create(strFolder + "\TempFile1.txt");
    aqFile.Create(strFolder + "\TempFile2.txt");
    Log.Message("Created files TempFile1.txt and TempFile2.txt in that folder.");

    // Delete the created folder and files
    aqFileSystem.DeleteFolder(strFolder, true);
    Log.Message("Deleted the folder and files.");
  }
  else
    Log.Error("Could not create folder " + strFolder + ".");
}

JScript

function Test()
{
  var strTmpFolder = Sys.OSInfo.TempDirectory;
  // Generate a temporary folder name
  var strTempName = Sys.OleObject("Scripting.FileSystemObject").GetTempName();
  var strFolder = strTmpFolder + strTempName;

  // Create a temporary folder and check if it was created successfully
  if (aqFileSystem.CreateFolder(strFolder) == 0)
  {
    Log.Message("Created new folder " + strFolder + ".");

    // Add files to the folder
    aqFile.Create(strFolder + "\TempFile1.txt");
    aqFile.Create(strFolder + "\TempFile2.txt");
    Log.Message("Created files TempFile1.txt and TempFile2.txt in that folder.");

    // Delete the created folder and files
    aqFileSystem.DeleteFolder(strFolder, true);
    Log.Message("Deleted the folder and files.");
  }
  else
    Log.Error("Could not create folder " + strFolder + ".");
}

Python

def Test():
  strTmpFolder = Sys.OSInfo.TempDirectory
  # Generate a temporary folder name
  strTempName = Sys.OleObject["Scripting.FileSystemObject"].GetTempName()
  strFolder = strTmpFolder + strTempName
  # Create a temporary folder and check if it was created successfully
  if aqFileSystem.CreateFolder(strFolder) == 0:
    Log.Message("Created new folder " + strFolder + ".")
    # Add files to the folder
    aqFile.Create(strFolder + "\TempFile1.txt")
    aqFile.Create(strFolder + "\TempFile2.txt")
    Log.Message("Created files TempFile1.txt and TempFile2.txt in that folder.")
    # Delete the created folder and files
    aqFileSystem.DeleteFolder(strFolder, True)
    Log.Message("Deleted the folder and files.")
  else:
    Log.Error("Could not create folder " + strFolder + ".")

VBScript

Sub Test
  Dim strTmpFolder, strTempName, strFolder

  strTmpFolder = Sys.OSInfo.TempDirectory
  ' Generate a temporary folder name
  strTempName = CreateObject("Scripting.FileSystemObject").GetTempName
  strFolder = strTmpFolder & strTempName

  ' Create a temporary folder and check if it was created successfully
  If aqFileSystem.CreateFolder(strFolder) = 0 Then
    Log.Message "Created new folder " & strFolder & "."

    ' Add files to the folder
    aqFile.Create strFolder & "\TempFile1.txt"
    aqFile.Create strFolder & "\TempFile2.txt"
    Log.Message "Created files TempFile1.txt and TempFile2.txt in that folder."

    ' Delete the created folder and files
    aqFileSystem.DeleteFolder strFolder, True
    Log.Message "Deleted the folder and files."
  Else
    Log.Error "Could not create folder " & strFolder & "."
  End If
End Sub

DelphiScript

procedure Test;
var strTmpFolder, strTempName, strFolder;
begin
  strTmpFolder := Sys.OSInfo.TempDirectory;
  // Generate a temporary folder name
  strTempName := Sys.OleObject('Scripting.FileSystemObject').GetTempName;
  strFolder := strTmpFolder + strTempName;

  // Create a temporary folder and check if it was created successfully
  if aqFileSystem.CreateFolder(strFolder) = 0 then
  begin
    Log.Message('Created new folder ' + strFolder + '.');

    // Add files to the folder
    aqFile.Create(strFolder + '\TempFile1.txt');
    aqFile.Create(strFolder + '\TempFile2.txt');
    Log.Message('Created files TempFile1.txt and TempFile2.txt in that folder.');

    // Delete the created folder and files
    aqFileSystem.DeleteFolder(strFolder, true);
    Log.Message('Deleted the folder and files.');
  end
  else
    Log.Error('Could not create folder ' + strFolder + '.');
end;

C++Script, C#Script

function Test()
{
  var strTmpFolder = Sys["OSInfo"]["TempDirectory"];
  // Generate a temporary folder name
  var strTempName = Sys["OleObject"]("Scripting.FileSystemObject")["GetTempName"]();
  var strFolder = strTmpFolder + strTempName;

  // Create a temporary folder and check if it was created successfully
  if (aqFileSystem["CreateFolder"](strFolder) == 0)
  {
    Log["Message"]("Created new folder " + strFolder + ".");

    // Add files to the folder
    aqFile["Create"](strFolder + "\TempFile1.txt");
    aqFile["Create"](strFolder + "\TempFile2.txt");
    Log["Message"]("Created files TempFile1.txt and TempFile2.txt in that folder.");

    // Delete the created folder and files
    aqFileSystem["DeleteFolder"](strFolder, true);
    Log["Message"]("Deleted the folder and files.");
  }
  else
    Log["Error"]("Could not create folder " + strFolder + ".");
}

See Also

Working With Files From Scripts
aqFileSystem.CreateFolder Method
aqFileSystem.GetCurrentFolder Method
aqFileSystem.SetCurrentFolder Method
aqFileSystem.DeleteFile Method

Highlight search results