2011-08-11 1 views
1

모든 이동식 드라이브의 문자를 변수에 저장하는 Vb 스크립트가 있습니다. 플로피 드라이브와 USB 드라이브가 모두 포함되어 있기 때문에 분리해야합니다. 내가 VBScript를 호출 할 수있는 소프트웨어를 사용하고VBScript를 통해 USB 및 플로피 드라이브의 문자 감지

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colDisks = objWMIService.ExecQuery _ 
    ("Select * from Win32_LogicalDisk") 

Removable = "" 
For Each objDisk in colDisks 
    if objDisk.DriveType = 2 then 
    if Removable > "" then 
     Removable = Removable & ";" 
    end if 
    Removable = Removable & objDisk.DeviceID & "\" 
    end if 
Next 

: 다른 변수에 저장하는 USB 드라이브 '변수에 문자와 플로피들, 여기

는 스크립트입니다. 그러나 그것은 내가 게시 한 것과 같은 어떤 종류의 지원 만합니다. 그렇다면 내가 말한 것을 어떻게 할 수 있습니까?

미리 감사드립니다.

답변

2

objDisk.MediaType을 확인하십시오. Here에는 MediaTypes 목록이 있습니다. 언뜻보기에 MediaType 1 ... 10은 '정상적인'플로피를 나타냅니다. 내 (가상) 컴퓨터의 빠른 확인에서 USB 드라이브가 Null (Unknown의 경우 Zero가 아님)의 MediaType을 보여 주므로 조심해야합니다. 두 번째로 (주의 사항에 대해) : 대부분의 정의 된 미디어 유형은 플로피 (일부는 이국적인)를 식별합니다. BTW - USB 플로피 드라이브는 어떨까요?

Const cnRemovableDisk = 2 
Const cnMTypeUnknown = 0 
Const cnMTypeNoFloppy = 11 
Const cnMTypeFixedHD = 12 
Dim strComputer : strComputer  = "." 
Dim objWMIService : Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Dim colDisks  : Set colDisks = objWMIService.ExecQuery _ 
    ("Select * from Win32_LogicalDisk") 
Dim Removable  : Removable = "" 
Dim Floppy  : Floppy = "" 
Dim USBDrive  : USBDrive = "" 
Dim objDisk 
For Each objDisk in colDisks 
    If objDisk.DriveType = cnRemovableDisk Then 
    Removable = Removable & ";" & objDisk.DeviceID & "\" 
    Select Case True 
     Case IsNull(objDisk.MediaType) 
      WScript.Echo objDisk.DeviceID, "has MediaType null - assuming USB Drive." 
      USBDrive = USBDrive & ";" & objDisk.DeviceID & "\" 
     Case objDisk.MediaType = cnMTypeNoFloppy 
      WScript.Echo objDisk.DeviceID, "has MediaType 11 - assuming USB Drive." 
      USBDrive = USBDrive & ";" & objDisk.DeviceID & "\" 
     Case objDisk.MediaType = cnMTypeUnknown 
      WScript.Echo objDisk.DeviceID, "has MediaType 0 - assuming USB Drive." 
      USBDrive = USBDrive & ";" & objDisk.DeviceID & "\" 
     Case objDisk.MediaType = cnMTypeFixedHD 
      WScript.Echo objDisk.DeviceID, "has MediaType 12 - how can this happen?" 
     Case Else 
      WScript.Echo objDisk.DeviceID, "has MediaType", objDisk.MediaType, " - surely some kind of floppy." 
      Floppy = Floppy & ";" & objDisk.DeviceID & "\" 
    End Select 
    End If 
Next 
Removable = Mid(Removable, 2) 
Floppy = Mid(Floppy , 2) 
USBDrive = Mid(USBDrive , 2) 
WScript.Echo "Removable:", Removable 
WScript.Echo "Floppy: ", Floppy 
WScript.Echo "USBDrive: ", USBDrive 

내 출력은 다음과 같습니다 :

A: has MediaType 5 - surely some kind of floppy. 
F: has MediaType null - assuming USB Drive. 
Removable: A:\;F:\ 
Floppy: A:\ 
USBDrive: F:\ 


나는 '진짜'컴퓨터에서 테스트 할 수 없습니다, 당신은 다음과 같은 코드를 한 번 확인해야합니다 USBDrive의 MediaType이 이상한 사고 일 수 있습니다. "Select Case True"제어 구조를 사용하여 MediaType을 쉽게 평가할 수 있도록 노력했습니다. VBScript는 첫 번째 진실이 될 때까지 사례의 조건을 테스트하고 해당 진술을 실행 한 다음 End Select로 '중단'합니다. 특별한 경우를 추가하거나 순서를 바꾸는 것은 간단합니다. IsNull 검사를 처음 위치에 두십시오.

+0

안녕하세요 Ekkehard.Horner, 잘 했어요 :) Huuuuuuuuuge Thanks. 잘 작동합니다. 당신은 위대하다 :) 나에게 당신의 우편물을 줄 수 있니? – Nofuzy

2

당신은 또한 더 자세한 내용은이 쿼리

set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2") 

을 시도 this 링크를 확인할 수 있습니다. 행운을 빕니다