我正在尝试找到像 Environ
这样的函数来查找我的企业中的主驱动器已映射到特定 PC 上的驱动器。
使用文件路径“G:\Eworking\SET\Operations\file”我知道我的电脑已被映射,因此该文件路径位于 G 驱动器内,但其他文件路径可能会以不同方式映射,因此我想确定它是哪个.
我之前尝试过 if else 方法来遍历字母表并执行 if Dir([filepath]) then
(见下文),但我想知道是否有更好的方法这个?
Sub LoopThroughDrives()
sFilePath As String
sFilePath = ":\Eworking\SET\Operations\file"
If Dir("A" & sFilePath) > 0 Then
msgbox ("It's in drive A")
Else
If Dir("B" & sFilePath) > 0 Then
msgbox ("It's in drive B")
Else
If Dir("C" & sFilePath) > 0 Then
msgbox ("It's in drive C")
Else
'...........................
'All other letters of the alphabet, checking each possibility
'...........................
End If
End If
End If
End Sub
最佳答案
您可以像这样获取驱动器号和路径:
For Each d In CreateObject("Scripting.FileSystemObject").Drives
Debug.Print d.DriveLetter, d.Path, d.ShareName
Next
关于vba - 查找分配给主驱动器的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210888/