GetParentFolderName 方法

返回一个字符串,其中包含指定路径中最后一个组件的父文件夹的名称。

语法

object.GetParentFolderName(path)

参数:

  • Object必需。始终为 FileSystemObject 的名称。
  • Path必需。指定要返回其父文件夹名称的组件所在的路径。

注:

GetParentFolderName 方法仅对提供的路径字符串有效。它不会尝试解析路径,也不会检查指定的路径是否存在。

以下示例说明了 GetParentFolderName 方法的用法。

示例 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

示例 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