检查驱动器是否已映射且处于活动状态
Check if a drive is mapped and active
我的 VBScript 中有一项要求,以确保驱动器已映射且可联系。
最初我只使用 FileSystemObject
的 DriveExists()
方法,但这并不理想,因为在某些情况下,用户在办公室开始新的一天,然后带着他们的笔记本电脑出门旅行,而没有关闭吃下;因此,有问题的驱动器仍列为已映射,但不可联系。
为了解决这个问题,我创建了下面的函数,但由于使用了 On Error Resume Next
.
,它看起来有点脏
所以我的问题是这样的;是否有一种方法可以在 VBS 中内置实现我的目标,或者我的方法(或类似方法)是唯一可以实现的方法吗?
Function CheckDriveExists(drive)
CheckDriveExists = false
If FSO.DriveExists(drive) Then ' A drive is mapped for the required 'drive'
'** Create a file name for the test file *'
Dim tfDate, tfName
tfDate = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)
tfName = "tstfile-" & tfDate & ".bucf"
'** Try to create and then delete a file on the usrs backup drive *'
On Error Resume Next
FSO.CreateTextFile(drive & ":\" & tfName)
FSO.DeleteFile(drive & ":\" & tfName)
CheckDriveExists = (Err.Number = 0) ' Check to see if the file was created and deleted successfully
Err.Clear ' Clear any possible error
On Error GoTo 0 ' Reset error handling
End If
End Function
来自 Windows 脚本文档
Function ShowDriveList
Dim fso, d, dc, s, n
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
n = ""
s = s & d.DriveLetter & " - "
If d.DriveType = 3 Then
n = d.ShareName
ElseIf d.IsReady Then
n = d.VolumeName
Else
n = "[Drive not ready]"
End If
s = s & n & "<BR>"
Next
ShowDriveList = s
End Function
也许你还需要
Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvpath)
Select Case d.DriveType
Case 0: t = "Unknown"
Case 1: t = "Removable"
Case 2: t = "Fixed"
Case 3: t = "Network"
Case 4: t = "CD-ROM"
Case 5: t = "RAM Disk"
End Select
ShowDriveType = "Drive " & d.DriveLetter & ": - " & t
End Function
我的 VBScript 中有一项要求,以确保驱动器已映射且可联系。
最初我只使用 FileSystemObject
的 DriveExists()
方法,但这并不理想,因为在某些情况下,用户在办公室开始新的一天,然后带着他们的笔记本电脑出门旅行,而没有关闭吃下;因此,有问题的驱动器仍列为已映射,但不可联系。
为了解决这个问题,我创建了下面的函数,但由于使用了 On Error Resume Next
.
所以我的问题是这样的;是否有一种方法可以在 VBS 中内置实现我的目标,或者我的方法(或类似方法)是唯一可以实现的方法吗?
Function CheckDriveExists(drive)
CheckDriveExists = false
If FSO.DriveExists(drive) Then ' A drive is mapped for the required 'drive'
'** Create a file name for the test file *'
Dim tfDate, tfName
tfDate = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)
tfName = "tstfile-" & tfDate & ".bucf"
'** Try to create and then delete a file on the usrs backup drive *'
On Error Resume Next
FSO.CreateTextFile(drive & ":\" & tfName)
FSO.DeleteFile(drive & ":\" & tfName)
CheckDriveExists = (Err.Number = 0) ' Check to see if the file was created and deleted successfully
Err.Clear ' Clear any possible error
On Error GoTo 0 ' Reset error handling
End If
End Function
来自 Windows 脚本文档
Function ShowDriveList
Dim fso, d, dc, s, n
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
n = ""
s = s & d.DriveLetter & " - "
If d.DriveType = 3 Then
n = d.ShareName
ElseIf d.IsReady Then
n = d.VolumeName
Else
n = "[Drive not ready]"
End If
s = s & n & "<BR>"
Next
ShowDriveList = s
End Function
也许你还需要
Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvpath)
Select Case d.DriveType
Case 0: t = "Unknown"
Case 1: t = "Removable"
Case 2: t = "Fixed"
Case 3: t = "Network"
Case 4: t = "CD-ROM"
Case 5: t = "RAM Disk"
End Select
ShowDriveType = "Drive " & d.DriveLetter & ": - " & t
End Function