[Function] DriveLetter

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
FanaticGuru
Posts: 1945
Joined: 30 Sep 2013, 22:25

[Function] DriveLetter

07 Nov 2013, 17:54

[Function] DriveLetter

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
}
;}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: [Function] DriveLetter

11 Nov 2013, 02:15

or this way

Code: Select all

/*
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx
0 = DRIVE_UNKNOWN
1 = DRIVE_NO_ROOT_DIR
2 = DRIVE_REMOVABLE
3 = DRIVE_FIXED
4 = DRIVE_REMOTE
5 = DRIVE_CDROM
6 = DRIVE_RAMDISK
*/

Type := 3 ; FIXED

loop 26
{
    if (Type = DllCall("GetDriveType", "Str", Chr(A_Index + 64) ":\"))
    {
        found_drives .= Chr(A_Index + 64)
    }
}

MsgBox % found_drives
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
User avatar
FanaticGuru
Posts: 1945
Joined: 30 Sep 2013, 22:25

Re: [Function] DriveLetter

11 Nov 2013, 22:59

DllCall is very nice for many things but not in this case when AHK already has a command for that.

If all I wanted to do was find all the drive letters of a certain type I could just do this.

Code: Select all

DriveGet z, List, Fixed
MsgBox % z
The function does more than that. If finds the drive letters based on multiply types as wells as most importantly the name of the drive either using partial or exact matching.

I use it mainly to find various removable drives that get assigned different letters depending on what computer and what order I plug them in.

It allows me to easily get a list of all removable drives that contain FG in the name for example whether it is FG_04(I:) or FG_08(H:).

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 127 guests