GetParentFolderName Method

Returns a string containing the name of the parent folder of the last component in a specified path.

Syntax

object.GetParentFolderName(path)

Arguments:

  • Object: Required. Always the name of a FileSystemObject.
  • Path: Required. The path specification for the component whose parent folder name is to be returned.

Note:

The GetParentFolderName method works only on the provided path string. It does not attempt to resolve the path, nor does it check for the existence of the specified path.

The following example illustrates the use of the GetParentFolderName method.

Example 1:

Function GetTheParent(DriveSpec)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    GetTheParent = fso.GetParentFolderName(DriveSpec)
End Function

' Usage
Dim parentFolder
parentFolder = GetTheParent("C:\Path\To\Your\File.txt")
 ' Output: C:\Path\To\Your

Example 2:

Function GetTheParent(DriveSpec)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    GetTheParent = fso.GetParentFolderName(DriveSpec)
End Function

' Usage
Dim parentOfDirectory
parentOfDirectory = GetTheParent("C:\Path\To\Your\Folder\SubFolder")
' Output: C:\Path\To\Your\Folder