【发布时间】:2020-03-02 10:18:20
【问题描述】:
我已经创建并正在使用以下函数来映射和缩短网络驱动器的路径长度,使用 SUBST 命令与我的实现 ADO 的工具一起使用。
Function MapBasePathToDrive(FullDirectory As String, strDrive As String, blnReadAttr As Boolean) As String
Dim objShell As Object
Dim sCmd$
Dim WaitOnReturn As Boolean: WaitOnReturn = True
Dim WindowStyle As Integer: WindowStyle = 0
Dim i&, lngErr&
' remove backslash for `SUBST` dos command to work
If Right(FullDirectory, 1) = "\" Then FullDirectory = Left(FullDirectory, Len(FullDirectory) - 1)
' prefix & suffix directory with double-quotes
FullDirectory = Chr(34) & FullDirectory & Chr(34)
Set objShell = CreateObject("WScript.Shell")
For i = 1 To 2
If i = 1 Then
'remove drive
sCmd = "SUBST" & " " & strDrive & " " & "/D"
lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
Else
'add drive
sCmd = "SUBST" & " " & strDrive
lngErr = objShell.Run(sCmd & " " & FullDirectory, WindowStyle, WaitOnReturn)
End If
Next i
' remove read-only attribute from Destination folder if you plan to copy files
If blnReadAttr Then
sCmd = "ATTRIB " & "-R" & " " & strDrive & "\*.*" & " " & "/S /D"
lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
End If
' to refresh explorer to show newly created drive
sCmd = "%windir%\explorer.exe /n,"
lngErr = objShell.Run(sCmd & strDrive, WindowStyle, WaitOnReturn)
' add backslash to drive if absent
MapBasePathToDrive = PathWithBackSlashes(strDrive)
End Function
上述功能大部分时间都可以很好地缩短长网络路径,然后将其传递给Application.FileDialog.InitialFilename。但是,如果一个驱动器(比如 Y:) 已经被映射,那么问题就接踵而至,因为Application.FileDialog.InitialFilename 进行了折腾,最终用户无法选择所需的文件,但看到了Y:\ 的文件!
我想做什么:
- 查看相关驱动器,例如
Y:是否可用。 - 如果正在使用,请将
Y:的网络路径分配给下一个免费可用的驱动器。 - 断开连接(删除)
Y: - 将
Y:分配给相关目录。
我有下面的批处理文件可以做到这一点,但我不知道如何将此批处理代码转换为 VBA 函数,即类似于上面显示的函数。任何帮助将不胜感激。
@echo off
if exist y:\ (
for /F "tokens=1,2,3" %%G in ('net use^|Find /I "Y:"^|Find "\\"') do ( net use * %%H >nul 2>&1)
net use y: /delete >nul 2>&1
)
net use y: \\xx.xx.xx.xx\SomeFolder >nul 2>&1
编辑:
我修改了上面的函数来添加这段代码。问题仅在于 sCMD 字符串由于不正确的双引号而没有被 WScript.Shell 执行。
- 谁能帮我正确的语法?
- 如果是我需要映射的本地文件夹,语法会如何变化?
...
Sub TestDriveMapping()
MapBasePathToDrive "\\xx.xx.xx.xx\SomeFolder", "Y:", True
End Sub
Function MapBasePathToDrive(FullDirectory As String, strDrive As String, blnReadAttr As Boolean) As String
Dim objShell As Object
Dim sCmd$
Dim WaitOnReturn As Boolean: WaitOnReturn = True
Dim WindowStyle As Integer: WindowStyle = 0
Dim i&, lngErr&
' remove backslash for `NET USE` dos command to work
If Right(FullDirectory, 1) = "\" Then FullDirectory = Left(FullDirectory, Len(FullDirectory) - 1)
' prefix & suffix directory with double-quotes
FullDirectory = Chr(34) & FullDirectory & Chr(34)
Set objShell = CreateObject("WScript.Shell")
sCmd = ""
sCmd = "@Echo Off " & vbCrLf
sCmd = sCmd & " IF EXIST " & strDrive & " (" & vbCrLf
sCmd = sCmd & " FOR /F " & Chr(34) & "TOKENS=1,2,3" & Chr(34) & " %G IN (" & Chr(39) & "NET USE ^|Find /I " & Chr(34) & strDrive & Chr(34) & "^|Find ""\\""" & Chr(39) & ") DO ( NET USE * %H >NUL 2>&1)" & vbCrLf
sCmd = sCmd & " NET USE " & strDrive & " /DELETE >NUL 2>&1" & vbCrLf
sCmd = sCmd & " )" & vbCrLf
sCmd = sCmd & " NET USE " & strDrive & " " & FullDirectory & " >NUL 2>&1"
lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
' remove read-only attribute from Destination folder if you plan to copy files
If blnReadAttr Then
sCmd = "ATTRIB " & "-R" & " " & strDrive & "\*.*" & " " & "/S /D"
lngErr = objShell.Run(sCmd, WindowStyle, WaitOnReturn)
End If
' to refresh explorer to show newly created drive
sCmd = "%windir%\explorer.exe /n,"
lngErr = objShell.Run(sCmd & strDrive, WindowStyle, WaitOnReturn)
' add backslash to drive if absent
MapBasePathToDrive = PathWithBackSlashes(strDrive)
End Function
【问题讨论】:
-
也许我过于简化了,但如果您只是想在对驱动器号进行任何操作之前检查驱动器是否正在使用,您可以使用
FileSystemObject来执行此操作。 This might help
标签: excel vba batch-file net-use subst