I needed in a script to find a drive letter based on drive label.
I thought it might be useful to others so added some versatility and turned it into a function.
Added commenting that should explain how to use it.
Code: Select all
;{[Function] DriveLetter
; Fanatic Guru
; 2013 11 07
;
; Function that will find the drive letter based on drive label.
;
;---------------------------------------------------------------------------------
;
; Method:
; DriveLetter(DriveLabel, Option*)
;
; Parameters:
; 1) {DriveLabel} drive label to search for
; Optional, Default = "" which returns all drive letters of proper type
; 2) {Option*} Variadic List of Options
; {Option = "Exact"} require exact match including case sensitive
; {Option = "CDROM" or "REMOVABLE" or "FIXED" or "NETWORK" or "RAMDISK" or "UNKNOWN"} search for this type drive, if no type is given all types are searched
; Optional, Default = Partial Matching, All Types
;
; Returns:
; String containing the drive letters that match drive label
;
; Examples:
; MsgBox % DriveLetter("Flash", "Exact", "Removable", "Ramdisk") ; All removable and ramdisk drives named exactly "Flash"
; MsgBox % DriveLetter(, "Removable", "Ramdisk") ; All removable and ramdisk drives
; MsgBox % DriveLetter("Flash") ; All drives named "Flash"
; MsgBox % DriveLetter() ; All drives
;
DriveLetter(DriveLabel := "", Option*)
{
Types := "CDROM|REMOVABLE|FIXED|NETWORK|RAMDISK|UNKNOWN"
Loop, % Option.MaxIndex()
{
Option := Option[A_Index ]
Loop, Parse, Types, |
{
If (Option = A_LoopField)
Option_Type .= A_LoopField "|"
}
if (Option = "Exact")
Option_Exact := true
}
if !Option_Type
Option_Type := Types
DriveGet DriveLetterList, List
Loop, Parse, DriveLetterList
{
DriveGet Found_DriveLabel, Label, %A_LoopField%:
DriveGet Found_DriveType, Type, %A_LoopField%:
if InStr(Option_Type,Found_DriveType)
{
If Option_Exact
{
If (Found_DriveLabel == DriveLabel or !DriveLabel)
{
DriveLetter .= A_LoopField
}
}
else
{
If (InStr(Found_DriveLabel,DriveLabel) or !DriveLabel)
{
DriveLetter .= A_LoopField
}
}
}
}
return DriveLetter
}
;}