aqFileSystem.GetRelativePath Method

Applies to TestComplete 14.72, last modified on December 24, 2020

Description

Use the GetRelativePath method to convert the fully qualified path specified by the AbsoluteFileName parameter to a relative path calculated against the folder specified by the CurrentFolder parameter.

Declaration

aqFileSystem.GetRelativePath(CurrentFolderAbsoluteFileName)

CurrentFolder [in]    Required    String    
AbsoluteFileName [in]    Required    String    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

CurrentFolder

Specifies the fully qualified name of the folder to which the resulting path name should be relative.

The Path parameter 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.

AbsoluteFileName

Specifies the absolute path (either to a file or to a folder) that should be transformed to a relative path.

Result Value

A string that holds the path to the file or folder specified by the AbsoluteFileName parameter relative to CurrentFolder.

Example

The code below specifies the current folder and the fully qualified name of a file, obtains the file's path which is relative to the current folder, and posts this path to the test log.

JavaScript

function GetRelativePathExample1()
{

  // Specify the fully qualified file name
  let sFileName = "C:\\Folder1\\FileName.txt";

  // Specify the current folder
  let sCurrentFolder = "C:\\Folder2\\";

  // Get the file's relative path
  let sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Post the relative path to the test log
  Log.Message(sRelPath);
}

JScript

function GetRelativePathExample1()
{

  // Specify the fully qualified file name
  var sFileName = "C:\\Folder1\\FileName.txt";

  // Specify the current folder
  var sCurrentFolder = "C:\\Folder2\\";

  // Get the file's relative path
  var sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Post the relative path to the test log
  Log.Message(sRelPath);
}

Python

def GetRelativePathExample1():
  # Specify the fully qualified file name
  sFileName = "C:\\Folder1\\FileName.txt"

  # Specify the current folder
  sCurrentFolder = "C:\\Folder2\\"

  # Get the file's relative path
  sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName)

  # Post the relative path to the test log
  Log.Message(sRelPath)

VBScript

Sub GetRelativePathExample1()
  ' Specify the fully qualified file name
  sFileName = "C:\Folder1\FileName.txt"

  ' Specify the current folder
  sCurrentFolder = "C:\Folder2\"

  ' Get the file's relative path
  sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName)

  ' Post the relative path to the test log
  Log.Message(sRelPath)
End Sub

DelphiScript

procedure GetRelativePathExample1();
var sFileName, sCurrentFolder, sRelPath;
begin
  // Specify the fully qualified file name
  sFileName := 'C:\Folder1\FileName.txt';

  // Specify the current folder
  sCurrentFolder := 'C:\Folder2\';

  // Get the file's relative path
  sRelPath := aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Post the relative path to the test log
  Log.Message(sRelPath);
end;

C++Script, C#Script

function GetRelativePathExample1()
{

  // Specify the fully qualified file name
  var sFileName = "C:\\Folder1\\FileName.txt";

  // Specify the current folder
  var sCurrentFolder = "C:\\Folder2\\";

  // Get the file's relative path
  var sRelPath = aqFileSystem["GetRelativePath"](sCurrentFolder, sFileName);

  // Post the relative path to the test log
  Log["Message"](sRelPath);
}

The code below gets the project’s current folder and the fully qualified name of the Notepad executable file, gets the executable file name relative to the project’s folder, and uses that path to add the Notepad to the list of tested applications.

For this routine to run, you project must have the TestedApps collection.

JavaScript

// For this routine to run, you project must have the TestedApps collection
function GetRelativePathExample2()
{

  // Specify the fully qualified file name
  let sFileName = "C:\\Windows\\System32\\notepad.exe";

  // Specify the current folder
  let sCurrentFolder = Project.Path + "TestedApps";

  // Get the file's relative path
  let sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Add the Notepad to the list of tested applications by using its relative path
  TestedApps.Add(sRelPath);

}

JScript

// For this routine to run, you project must have the TestedApps collection
function GetRelativePathExample2()
{

  // Specify the fully qualified file name
  var sFileName = "C:\\Windows\\System32\\notepad.exe";

  // Specify the current folder
  var sCurrentFolder = Project.Path + "TestedApps";

  // Get the file's relative path
  var sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Add the Notepad to the list of tested applications by using its relative path
  TestedApps.Add(sRelPath);
}

Python

# For this routine to run, you project must have the TestedApps collection
def GetRelativePathExample2():
  # Specify the fully qualified file name
  sFileName = "C:\\Windows\\System32\\notepad.exe"

  # Specify the current folder
  sCurrentFolder = Project.Path + "TestedApps"

  # Get the file's relative path
  sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName)

  # Add the Notepad to the list of tested applications by using its relative path
  TestedApps.Add(sRelPath)

VBScript

' For this routine to run, you project must have the TestedApps collection
Sub GetRelativePathExample2()
  ' Specify the fully qualified file name
  sFileName = "C:\\Windows\\System32\\notepad.exe"

  ' Specify the current folder
  sCurrentFolder = Project.Path + "TestedApps"

  ' Get the file's relative path
  sRelPath = aqFileSystem.GetRelativePath(sCurrentFolder, sFileName)

  ' Add the Notepad to the list of tested applications by using its relative path
  TestedApps.Add(sRelPath)
End Sub

DelphiScript

// For this routine to run, you project must have the TestedApps collection
procedure GetRelativePathExample2(); var sFileName, sCurrentFolder, sRelPath;
begin

  // Specify the fully qualified file name
  sFileName := 'C:\Windows\System32\notepad.exe';

  // Specify the current folder
  sCurrentFolder := Project.Path + 'TestedApps';

  // Get the file's relative path
  sRelPath := aqFileSystem.GetRelativePath(sCurrentFolder, sFileName);

  // Add the Notepad to the list of tested applications by using its relative path
  TestedApps.Add(sRelPath);
end;

C++Script, C#Script

// For this routine to run, you project must have the TestedApps collection
function GetRelativePathExample2()
{

  // Specify the fully qualified file name
  var sFileName = "C:\\Windows\\System32\\notepad.exe";

  // Specify the current folder
  var sCurrentFolder = Project["Path"] + "TestedApps";

  // Get the file's relative path
  var sRelPath = aqFileSystem["GetRelativePath"](sCurrentFolder, sFileName);

  // Add the Notepad to the list of tested applications by using its relative path
  TestedApps["Add"](sRelPath);
}

The code below gets the HTML document, the image file name and adds an IMG element to the document using the image file’s relative path.

JavaScript

function GetRelativePathExample3()
{
  // Set the HTML document name
  let sPath = "C:\\Data\\FileName.html";

  if (aqFile.Exists(sPath))
  {

  // Set the image name
  let sImagePath = "C:\\Data\\images\\img1.png";

  // Get the relative path to the image
  let sRelPath = aqFileSystem.GetRelativePath(aqFileSystem.GetFileInfo(sPath).ParentFolder.Path, sImagePath);

  // Get an HTML document
  // Add a new img element to it
  // And set the src attribute of the added image to the relative path

  let Doc = getActiveXObject("Msxml2.DomDocument.6.0");
  Doc.async = false;
  Doc.load(sPath);

  let Node = Doc.documentElement;
  if (Node != null)
    {
    let body = Node.selectSingleNode("//body");
    if (body != null)
      {
      let newImageElement = Doc.createElement("img");
      let attr = Doc.createAttribute("src");
      attr.value = sRelPath;
      newImageElement.setAttributeNode(attr);
      body.appendChild(newImageElement);

      Doc.Save(sPath);
      }

    }

  }
  else
    Log.Message("File " + sPath + " does not exist.");
}

JScript

function GetRelativePathExample3()
{
  // Set the HTML document name
  var sPath = "C:\\Data\\FileName.html";

  if (aqFile.Exists(sPath))
  {
  // Set the image name
  var sImagePath = "C:\\Data\\images\\img1.png";

  // Get the relative path to the image
  var sRelPath = aqFileSystem.GetRelativePath(aqFileSystem.GetFileInfo(sPath).ParentFolder.Path, sImagePath);

  // Get an HTML document
  // Add a new img element to it
  // And set the src attribute of the added image to the relative path

  var Doc = Sys.OleObject("Msxml2.DOMDocument.6.0");
  Doc.async = false;
  Doc.load(sPath);

  var Node = Doc.documentElement;
  if (Node != null)
    {
    var body = Node.selectSingleNode("//body");
    if (body != null)
      {
      var newImageElement = Doc.createElement("img");
      var attr = Doc.createAttribute("src");
      attr.value = sRelPath;
      newImageElement.setAttributeNode(attr);
      body.appendChild(newImageElement);
      Doc.Save(sPath);
      }
    }

  }
  else
    Log.Message("File " + sPath + " does not exist.");
}

Python

def GetRelativePathExample3():
  # Set the HTML document name
  sPath = "C:\\Data\\FileName.html"

  if (aqFile.Exists(sPath)):
    # Set the image name
    sImagePath = "C:\\Data\\images\\img1.png"

    # Get the relative path to the image
    sRelPath = aqFileSystem.GetRelativePath(aqFileSystem.GetFileInfo(sPath).ParentFolder.Path, sImagePath);

    # Get an HTML document
    # Add a new img element to it
    # And set the src attribute of the added image to the relative path

    Doc = Sys.OleObject["Msxml2.DomDocument.6.0"]
    Doc.async = False
    Doc.load(sPath)

    Node = Doc.documentElement;
    if (Node != None):
      body = Node.selectSingleNode("//body")
      if (body != None):
        newImageElement = Doc.createElement("img")
        attr = Doc.createAttribute("src")
        attr.value = sRelPath
        newImageElement.setAttributeNode(attr)
        body.appendChild(newImageElement)

        Doc.Save(sPath)

  else:
    Log.Message("File " + sPath + " does not exist.")

VBScript

Sub GetRelativePathExample3()
  ' Set the HTML document name
  sPath = "C:\\Data\\FileName.html"

  If aqFile.Exists(sPath) Then
  ' Set the image name
  sImagePath = "C:\\Data\\images\\img1.png"

  ' Get the relative path to the image
  sRelPath = aqFileSystem.GetRelativePath(aqFileSystem.GetFileInfo(sPath).ParentFolder.Path, sImagePath)

  ' Get an HTML document
  ' Add a new img element to it
  ' And set the src attribute of the added image to the relative path

  Set Doc = Sys.OleObject("Msxml2.DOMDocument.6.0")
  Doc.async = False
  Doc.load(sPath)

  Set Node = Doc.documentElement
  If Not Node Is Nothing Then
    Set body = Node.selectSingleNode("//body")
    If Not body Is Nothing Then
      Set newImageElement = Doc.createElement("img")
      Set attr = Doc.createAttribute("src")
      attr.value = sRelPath
      newImageElement.setAttributeNode(attr)
      body.appendChild(newImageElement)
      Doc.Save(sPath)
    End If

  End If
  Else
    Log.Message("File " + sPath + " does not exist.")
  End If
End Sub

DelphiScript

procedure GetRelativePathExample3();
var sPath, sImagePath, sRelPath, Doc, Node, body, newImageElement, attr;
begin
  // Set the HTML document name
  sPath := 'C:\Data\FileName.html';

  if aqFile.Exists(sPath) then
  begin
  // Set the image name
  sImagePath := 'C:\Data\images\img1.png';

  // Get the relative path to the image
  sRelPath := aqFileSystem.GetRelativePath(aqFileSystem.GetFileInfo(sPath).ParentFolder.Path, sImagePath);

  // Get an HTML document
  // Add a new img element to it
  // And set the src attribute of the added image to the relative path

  Doc := Sys.OleObject('Msxml2.DOMDocument.6.0');
  Doc.async := false;
  Doc.load(sPath);

  Node := Doc.documentElement;
  if Node <> nil then
    begin
    body := Node.selectSingleNode('//body');
    if body <> nil then
      begin
      newImageElement := Doc.createElement('img');
      attr := Doc.createAttribute('src');
      attr.value := sRelPath;
      newImageElement.setAttributeNode(attr);
      body.appendChild(newImageElement);
      Doc.Save(sPath);
      end;
    end;

  end
  else
    Log.Message('File ' + sPath + ' does not exist.');
end;

C++Script, C#Script

function GetRelativePathExample3()
{
  // Set the HTML document name
  var sPath = "C:\\Data\\FileName.html";

  if (aqFile["Exists"](sPath))
  {
  // Set the image name
  var sImagePath = "C:\\Data\\images\\img1.png";

  // Get the relative path to the image
  var sRelPath = aqFileSystem["GetRelativePath"](aqFileSystem["GetFileInfo"](sPath)["ParentFolder"]["Path"], sImagePath);

  // Get an HTML document
  // Add a new img element to it
  // And set the src attribute of the added image to the relative path

  var Doc = Sys["OleObject"]("Msxml2.DOMDocument.6.0");
  Doc.async = false;
  Doc.load(sPath);

  var Node = Doc["documentElement"];
  if (Node != null)
    {
    var body = Node["selectSingleNode"]("//body");
    if (body != null)
      {
      var newImageElement = Doc["createElement"]("img");
      var attr = Doc["createAttribute"]("src");
      attr.value = sRelPath;
      newImageElement["setAttributeNode"](attr);
      body["appendChild"](newImageElement);
      Doc["Save"](sPath);
      }
    }

  }
  else
    Log["Message"]("File " + sPath + " does not exist.");
}

See Also

Working With Files From Scripts
ExpandFileName Method
ExpandUNCFileName Method

Highlight search results