我正在编写一个 AutoIt 脚本,其中有磁盘号、分区号和标签,并且我需要找到相应的驱动器号。我尝试了多种方法,但似乎没有一种方法能够始终如一地发挥作用。这是当前代码:
$query = "SELECT DriveLetter FROM Win32_DiskDriveToDiskPartition WHERE DiskNumber = " & $targetDiskNumber & _
" AND PartitionNumber = " & $targetPartitionNumber & _
" AND Name = '" & $targetLabel & "'"
$result = $WMBIObj.ExecQuery($query)
变量 $diskNumber、$partitionNumber 和 $label 是已知的。有人可以帮助我提供可靠的方法来查找与指定磁盘号、分区号和标签关联的驱动器号吗?
我在 AutoIt 中通过 WMI 使用 Win32_DiskPartition 和 Win32_LogicalDisk 类。 标签是指分区的卷标。 该脚本应该适用于各种 Windows 系统。
这里有完整的解决方案
Func GetSelectedDriveLetter($targetLabel)
; Get the selected disk number from the combo box
Local $selectedDiskNumber = StringRegExp(GUICtrlRead($cCombo), "Disk (\d+):", 1)
; Iterate through the disk drives to find the matching disk
For $SingleDiskDrive In $WMBIObj.ExecQuery("SELECT * FROM Win32_DiskDrive")
If StringRegExp($SingleDiskDrive.DeviceID, "PHYSICALDRIVE" & $selectedDiskNumber) Then
; Find the partition with the label "UTZ_BOOT"
For $Partition In $WMBIObj.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $SingleDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
For $SingleLogicalDisk In $WMBIObj.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $Partition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
If $SingleLogicalDisk.VolumeName == "$targetLabel" Then
; Found the partition, extract the drive letter
Local $driveLetter = StringLeft($SingleLogicalDisk.DeviceID, 2)
Return $driveLetter
ExitLoop ; Exit the loop since we found the match
EndIf
Next
Next
EndIf
Next
EndFunc