[英]FileSystemObject: Retrieving folder name from UNC path
我不知道如何使用FileSystemObject
object 获取UNC 路径中文件夹的名称。例如:从“ \\Server\FolderA
”我希望能够获取“ FolderA
”。 我希望可行的方法是“ GetBaseName
”,但当“ IsRootFolder
”属性为 True 时它似乎不起作用。
以下程序:
Public Sub GetUNCFolderName()
Const stPathA As String = "\\Server\FolderA"
Const stPathB As String = "\\Server\FolderA\FolderB"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Debug.Print "---------"
Debug.Print "Path A: " & stPathA
Debug.Print "GetAbsolutePathName: " & fso.GetAbsolutePathName(stPathA)
Debug.Print "GetBaseName : " & fso.GetBaseName(stPathA)
Debug.Print "IsRootFolder : " & fso.GetFolder(stPathA).IsRootFolder
Debug.Print "---------"
Debug.Print "Path B: " & stPathB
Debug.Print "GetAbsolutePathName: " & fso.GetAbsolutePathName(stPathB)
Debug.Print "GetBaseName : " & fso.GetBaseName(stPathB)
Debug.Print "IsRootFolder : " & fso.GetFolder(stPathB).IsRootFolder
End Sub
返回此结果:
---------
Path A: \\Server\FolderA
GetAbsolutePathName: \\Server\FolderA
GetBaseName :
IsRootFolder : True
---------
Path B: \\Server\FolderA\FolderB
GetAbsolutePathName: \\Server\FolderA\FolderB
GetBaseName : FolderB
IsRootFolder : False
如您所见,“ fso.GetBaseName(stPathA)
”返回一个空字符串,而“fso.GetBaseName(stPathB)”则不会。
我很感激你能给我的任何想法。
它可能不是最好的解决方案,但我无法找到仅使用 FileSystemObject object 的解决方案。至少它是解决问题的一圈。 我的解决方案是添加一个 if 语句: If (fso.GetDrive(fso.GetDriveName(PathA)).DriveType = Network) And fso.GetFolder(PathA).IsRootFolder Then
Public Sub GetUNCFolderName()
Dim PathA As String, PathB As String
PathA = "\\Server\FolderA"
PathB = "\\Server\FolderA\FolderB"
Const Network = 3
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.GetDrive(fso.GetDriveName(PathA)).DriveType = Network) And fso.GetFolder(PathA).IsRootFolder Then
If VBA.Right$(PathA, 1) = "\" Then PathA = VBA.Left$(PathA, Len(PathA) - 1)
Debug.Print "PathA-1: " & VBA.Mid$(PathA, InStrRev(PathA, "\") + 1)
Else
Debug.Print "PathA-2: " & fso.GetBaseName(PathA)
End If
If (fso.GetDrive(fso.GetDriveName(PathB)).DriveType = Network) And fso.GetFolder(PathB).IsRootFolder Then
If VBA.Right$(PathB, 1) = "\" Then PathB = VBA.Left$(PathB, Len(PathB) - 1)
Debug.Print "PathB-1: " & VBA.Mid$(PathB, InStrRev(PathB, "\") + 1)
Else
Debug.Print "PathB-2: " & fso.GetBaseName(PathB)
End If
End Sub
退货
PathA-1: FolderA
PathB-2: FolderB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.