aqFileSystem.GetFileExtension Method

Applies to TestComplete 15.68, last modified on October 03, 2024

Description

Use the GetFileExtension method to extract the extension from the specified path to a file.

Declaration

aqFileSystem.GetFileExtension(PathToFile)

PathToFile [in]    Required    String    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

PathToFile

Specifies the path to be processed.

Result Value

A string that holds the extension of the specified file.

Example

The code below demonstrates how to get an extension of the first file from the specified folder and post this information to the test log:

JavaScript, JScript

function FileExtensionExample()
{
  // Get a list of files from the specified folder
  var folderName = "C:\\Work";
  if (aqFileSystem.Exists(folderName))
  {
    var files = aqFileSystem.GetFolderInfo(folderName).Files;

    if (files != null)
    {
      // Get the path to the first found file
      var firstFilePath = files.Item(0).Path;
        
      // Obtain the file's extension
      var fileExtension = aqFileSystem.GetFileExtension(firstFilePath);
    
      // Post the extension to the test log
      Log.Message("The extension of the first file: " + fileExtension);
    }
    else
    {
      Log.Message("The specified folder does not contain any files");
    }
  }
}

Python

def FileExtensionExample():
  # Get a list of files from the specified folder
  folderName = "C:\\Work"
  if aqFileSystem.Exists(folderName):
    files = aqFileSystem.GetFolderInfo(folderName).Files
    
    if files != None:
     # Get the path to the first file
      firstFilePath = files.Item(0).Path
  
      # Obtain the file's extension 
      fileExtension = aqFileSystem.GetFileExtension(firstFilePath)
  
      # Post the extension to the test log
      Log.Message("The extension of the first file: " + fileExtension)
    else:
      Log.Message("ThXtuje specified folder does not contain any files")

VBScript

Sub FileExtensionExample

  ' Get a list of files from the specified folder
  folderName = "C:\Work"
  If aqFileSystem.Exists(folderName) Then
    Set files = aqFileSystem.GetFolderInfo(folderName).Files

    If Not files Is Nothing Then
      ' Get the path to the first file
      firstFilePath = files.Item(0).Path
    
      ' Obtain the file's extension
      fileExtension = aqFileSystem.GetFileExtension(aqConvert.VarToStr(firstFilePath))
    
      ' Post the extension to the test log
      Log.Message("The extension of the first file: " + fileExtension)
    Else
      Log.Message("The specified folder does not contain any files")
    End If
   
  End If
End Sub

DelphiScript

procedure FileExtensionExample;
var folderName, files, i, firstFilePath, fileExtension;
begin

  // Get a list of files from the specified folder
  folderName := 'C:\\Work';
  if aqFileSystem.Exists(foldername) then
  begin
    files := aqFileSystem.GetFolderInfo(folderName).Files;
    
    if files <> nil then
    begin
      // Get the path to the first file
      firstFilePath := files.Item(0).Path;
    
      // Obtain the file's extension
      fileExtension := aqFileSystem.GetFileExtension(firstFilePath);
    
      // Post the extension to the test log
      Log.Message('The extension of the first file: ' + fileExtension);
    end
    else
    Log.Message('The specified folder does not contain any files');
    
  end;
end;

C++Script, C#Script

function FileExtensionExample()
{
  // Get a list of files from the specified folder
  var folderName = "C:\\Work";
  if (aqFileSystem.Exists(folderName))
  {
    var files = aqFileSystem["GetFolderInfo"](folderName)["Files"];
    
    if (files != null)
    {
      // Get the path to the first file
      var firstFilePath = files["Item"](0)["Path"];
    
      // Obtain the file's extension
      var fileExtension = aqFileSystem["GetFileExtension"](firstFilePath);
    
      // Post the extension to the test log
      Log["Message"]("The extension of the first file: " + fileExtension);
    }
    else
    {
      Log["Message"]("The specified folder does not contain any files");
    }
  }
}

See Also

Working With Files From Scripts
GetFileName Method
GetFileFolder Method
GetFileDrive Method

Highlight search results