gpt4 book ai didi

excel - 文件系统对象 : Retrieving folder name from UNC path

转载 作者:行者123 更新时间:2023-12-04 20:21:19 27 4
gpt4 key购买 nike

我不知道如何 获取文件夹名称 UNC 路径 FileSystemObject目的。例如:
从“\\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 对象找到一个。至少一圈可以解决问题。我的解决方案是添加一个 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

关于excel - 文件系统对象 : Retrieving folder name from UNC path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71864215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com