Drive

AutoHotkey GUI

Drive

Ejects/retracts the tray in a CD or DVD drive, or sets a drive's volume label.

Drive, Sub-command , Drive, Value

The Sub-command, Drive, and Value parameters are dependent upon each other and their usage is described below.

Label, Drive [, NewLabel]: Changes Drive's volume label to be NewLabel (if NewLabel is omitted, the drive will have no label). Drive is the drive letter followed by a colon and an optional backslash (might also work on UNCs and mapped drives). For example: Drive, Label, C:, Seagate200.

To retrieve the current label, follow this example: DriveGet, OutputVar, Label, C:.

Lock, Drive: Prevents a drive's eject feature from working. For example: Drive, Lock, D:. Most drives cannot be "locked open". However, locking the drive while it is open will probably result in it becoming locked the moment it is closed. This command has no effect on drives that do not support locking (such as most read-only drives). If a drive is locked by a script and that script exits, the drive will stay locked until another script or program unlocks it, or the system is restarted. If the specified drive does not exist or does not support the locking feature, ErrorLevel is set to 1. Otherwise, it is set to 0.


Unlock, Drive: Reverses the above. Unlock needs to be executed multiple times if the drive was locked multiple times (at least for some drives). For example, if Drive, Lock, D: was executed three times, three executions of Drive, Unlock, D: might be needed to unlock it. Because of this and the fact that there is no way to determine whether a drive is currently locked, it is often useful to keep track of its lock-state in a variable.


Eject [, Drive, 1]: Ejects or retracts the tray of a CD or DVD drive (to eject other types of media or devices, see the DllCall example at the bottom of this page).

If Drive is omitted, the default CD/DVD drive will be used. To eject the tray, omit the last parameter. To retract/close the tray, specify 1 for the last parameter; for example: Drive, Eject, D:, 1.

Drive Eject waits for the ejection or retraction to complete before allowing the script to continue. If the tray is already in the correct state (open or closed), ErrorLevel is set to 0 (i.e. "no error").

Drive Eject will probably not work on a network drive or non-CD/DVD drive. If it fails in such cases or for any other reason, ErrorLevel is set to 1.

It may be possible to detect the previous tray state by measuring the time the command takes to complete. For example, the following hotkey toggles the tray to the opposite state (open or closed):

#c::
Drive, Eject
; If the command completed quickly, the tray was probably already ejected.
; In that case, retract it:
if A_TimeSinceThisHotkey < 1000  ; Adjust this time if needed.
    Drive, Eject,, 1
return

To determine the media status of a CD or DVD drive (playing, stopped, open, etc.), see DriveGet.

ErrorLevel

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to 1 if there was a problem or 0 otherwise.

Remarks

The following is an alternate ejection method that also works on types of media/devices other than CD/DVD:

; Update the first line below to match the desired drive letter (you can ignore all the other lines below).
DriveLetter = I:  ; Set this to the drive letter you wish to eject.

hVolume := DllCall("CreateFile"
    , Str, "\\.\" . DriveLetter
    , UInt, 0x80000000 | 0x40000000  ; GENERIC_READ | GENERIC_WRITE
    , UInt, 0x1 | 0x2  ; FILE_SHARE_READ | FILE_SHARE_WRITE
    , UInt, 0
    , UInt, 0x3  ; OPEN_EXISTING
    , UInt, 0, UInt, 0)
if hVolume <> -1
{
    DllCall("DeviceIoControl"
        , UInt, hVolume
        , UInt, 0x2D4808   ; IOCTL_STORAGE_EJECT_MEDIA
        , UInt, 0, UInt, 0, UInt, 0, UInt, 0
        , UIntP, dwBytesReturned  ; Unused.
        , UInt, 0)
    DllCall("CloseHandle", UInt, hVolume)
}

Related

DriveGet, DriveSpaceFree

Example

Drive, Label, D:, BackupDrive
Drive, Eject,, 1 ; Retract (close) the tray of the default CD or DVD drive.