vbs 检测 USB 驱动器并运行一些命令

vbs 检测 USB 驱动器并运行一些命令

我有两个脚本。它们都位于硬盘上的同一个文件夹中。“vbs”调用“bat”来检测 USB 并运行一些命令。

我想加入选择 usb 到 vbs 的功能。谢谢

通用串行总线

Option Explicit
On Error Resume next
mensaje = MSGBOX ("Start USB fix", vbOKCancel, "USB fix")
If mensaje = vbOK Then
SCRIPT = "usb.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.run NewPath, 1, true
    Else
End If
On Error GoTo 0

启动脚本

@echo off
set /p drive=Choose the drive usb letter:
if exist %drive%: (goto target)
:target
attrib /d /s -r -h -s %drive%:\*.*
:: ext
call:ext "*.ini*"
call:ext "*.lnk*"
exit

:: funcion ext
@echo off
pause
goto:eof
:ext
set ext=%1
del /f/q/s %drive%:\"%ext%"
goto:eof

重要提示:没有答案是正确的。

答案1

  1. VBScript:请阅读 Rob Haupt 的五项 VBScript“最佳实践”和 Rob van der Woude 的调试脚本; 简而言之:

  2. 批处理脚本:请阅读 Rob van der Woude 的调试批处理文件

  3. 为什么不将所有给定的任务都保留在批处理脚本中?让我们应用wmic命令: 这Win32_Volume 类(表示硬盘上的存储区域)或Win32_LogicalDisk WMI 类(表示解析为运行 Windows 的计算机系统上的实际本地存储设备的数据源)。

尝试一下 *** 看看编辑以下:

wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label
wmic path Win32_LogicalDisk get DeviceID, DriveType, Description, VolumeName, FileSystem
rem or full output:
wmic path Win32_Volume get /value
wmic path Win32_LogicalDisk get /value

以下是批处理脚本的可能框架(将其与usb.bat脚本放在一起你自己):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_drives="
for /F "tokens=*" %%G in ('
  wmic path Win32_LogicalDisk where "DriveType=2" get DeviceID /value ^|findstr "="
  ') do for %%g in ("%%~G") do (
    set "__%%~g"
    echo(
    rem echo going to process drive __%%~g
    call :processDrive
  )

ENDLOCAL
goto :eof

:processDrive
  echo processing drive %__DeviceID%
  pushd "%__DeviceID%\"
    2>NUL dir /S /B /A *.ini
    2>NUL dir /S /B /A *.lnk
  popd
goto :eof

以下for是循环

  • %%G检索DeviceID值;
  • %%g删除结尾回车在返回的值中:wmic行为:每个输出行以0x0D0D0A<CR><CR><LF>)结尾,而不是常见的0x0D0A<CR><LF>)。

查看 Dave Benham 的WMICFOR /F:修复尾随<CR>问题

输出

==> D:\bat\SF\786392.bat

processing drive F:
F:\Shortcut.lnk
F:\vbScriptDoc\Hey_Scripting_Guy.lnk

processing drive G:
G:\SPSS\admin\SCRIPTS.INI
G:\SPSS\admin\system32\GroupPolicy\GPT.INI
G:\VB_scripts\Net\nethood_create_a_link.vbs.lnk

==>

更多资源(批处理脚本编写者必读):

***编辑:不幸的是,查询和wmi 类DriveType中的属性可能会产生错误的结果,请参见下一个输出,其中和都是 USB 可移动介质,因此属性Win32_VolumeWin32_LogicalDiskF:G:DriveType应该2

==> wmic path Win32_LogicalDisk get DeviceID,DriveType,Description,VolumeName, FileSystem,Size
Description       DeviceID  DriveType  FileSystem  Size           VolumeName
Local Fixed Disk  C:        3          NTFS        119664537600
Local Fixed Disk  D:        3          NTFS        1000202039296  DataDisk
CD-ROM Disc       E:        5
Removable Disk    F:        2          FAT         519274496      KINGSTON
Local Fixed Disk  G:        3          FAT32       500044136448   GOG

==> wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label, Capacity
BlockSize  Capacity       DriveLetter  DriveType  Label
4096       1000202039296  D:           3          DataDisk
4096       366997504                   3          Rezervováno systémem
8192       519274496      F:           2          KINGSTON
65536      500044136448   G:           3          GOG
4096       119664537600   C:           3
                          E:           5

您需要结合以下 WMI/WMIC 查询来获取可移动磁盘的正确驱动器号:

==> wmic path Win32_DiskDrive get DeviceID, InterfaceType, MediaType

DeviceID            InterfaceType  MediaType
\\.\PHYSICALDRIVE1  IDE            Fixed hard disk media
\\.\PHYSICALDRIVE2  USB            Removable Media
\\.\PHYSICALDRIVE0  IDE            Fixed hard disk media
\\.\PHYSICALDRIVE3  USB            External hard disk media


==> wmic path Win32_DiskDriveToDiskPartition get /value

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE2""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE0""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE3""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""



==> wmic path Win32_LogicalDiskToPartition get /value

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="C:""
EndingAddress=120032591871
StartingAddress=368050176

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="D:""
EndingAddress=1000203091967
StartingAddress=1048576

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="F:""
EndingAddress=519569407
StartingAddress=16384

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="G:""
EndingAddress=500105249279
StartingAddress=32256

答案2

以下是我对你的 的粗略修改usb.vbs。请注意,大多数修改都MSGBOX被替换了,因为我讨厌所有对话框、弹出框等,如果它们的输出在给定上下文中毫无用处的话:

' VB Script Document
option explicit
'On Error Resume Next
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName

Dim objFSO, objShell, objDrive, scriptBaseName
Dim mensaje, SCRIPT
Dim strPath, objFile, strFolder, NewPath, strDrive, strcmd, iResult

mensaje = MSGBOX ("Start USB fix", vbOKCancel, "USB fix")
If mensaje = vbOK Then
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  Set objShell = CreateObject("Wscript.Shell")
  scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
      SCRIPT = "usb.bat"
      strPath = Wscript.ScriptFullName
      Set objFile = objFSO.GetFile(strPath)
      strFolder = objFSO.GetParentFolderName(objFile) 
      NewPath = objFSO.BuildPath(strFolder, SCRIPT)
  For Each objDrive In objFSO.Drives
    strResult = strResult & vbNewLine & objDrive.DriveLetter & vbTab & objDrive.DriveType
    If objDrive.DriveType = 1 And objDrive.IsReady Then ''

      strDrive = objDrive.DriveLetter & ":\"
      '''xmsgbox objDrive.DriveLetter & ":\ = " & objDrive.VolumeName, vbInformation, scriptBaseName
      'strcmd = "cmd.exe /C """ & NewPath & """ " & strDrive ' executable
      strcmd = """" & NewPath & """ " & strDrive                 ' batch script
      iResult = objshell.run( strcmd , 1, true)
      strResult = strResult & vbTab & strcmd & vbTab & iResult
      '''CALL xMSGBOX ("Finish USB fix", VBOKONLY, "USB fix")
    End If
  Next
Else
  CALL xMSGBOX ("Cancel USB fix", VBOKONLY, "USB fix")
End If

'strResult = strResult & vbNewLine & 

Wscript.Echo strResult
Wscript.Quit

Sub xMSGBOX(byVal sPrompt, byVal sButtons, byVal sTitle)
  strResult = strResult & vbNewLine & "=" & sPrompt & " =" & sButtons  & " =" & sTitle  
End Sub

使用 next 进行测试usb.bat

@ECHO OFF
SETLOCAL EnableExtensions
echo 0th parameter = %0
echo     script = %~f0
echo parameters = %*
pause
ENDLOCAL
exit /B

编辑很遗憾,DriveTypeDrive对象的属性FileSystemObject可能会产生与查询和wmi 类DriveType中的属性相同的错误结果。参见上述脚本的输出(在与Win32_VolumeWin32_LogicalDisk786392.vbs我对当前问题的另一个回答中的例子)事实上,开车G这是一个 USB 可移动磁盘,应该属于DriveType 1

==> cscript D:\VB_scripts\SF\786392.vbs
786392.vbs
C       2       drive type=2
D       2       drive type=2
E       4       drive type=4
F       1       "D:\VB_scripts\SF\usb.bat" F:\  0
G       2       drive type=2

==>

相关内容