【问题标题】:Check if a drive is mapped and active检查驱动器是否已映射并处于活动状态
【发布时间】:2015-06-17 14:12:42
【问题描述】:

我的 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

【问题讨论】:

  • 创建文本文件恕我直言不是好方法。也许你没有足够的权限来写,尤其是根文件夹。抱歉,我没有其他建议。 On Error Resumt Next 是一个很好的做法。
  • 没有权限的好点。在这种情况下,它不应该是一个问题,但值得注意的是未来。

标签: vbscript mapped-drive


【解决方案1】:

来自 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2019-12-10
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多