# Drive

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

    Drive, SubCommand , Value1, Value2

The SubCommand, Value1, and Value2 parameters are dependent upon each other and their usage is described below.

## Sub-commands

For SubCommand, specify one of the following:

- Label: Renames the volume label of a drive.
- Lock: Prevents a drive's eject feature from working.
- Unlock: Restores a drive's eject feature.
- Eject: Ejects or retracts the tray of a CD or DVD drive.

### Label

Changes Drive's volume label to be NewLabel.

    Drive, Label, Drive , NewLabel

Drive is the drive letter followed by a colon and an optional backslash (might also work on UNCs and mapped drives). If NewLabel is omitted, the drive will have no label.

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

### Lock

Prevents a drive's eject feature from working.

    Drive, Lock, Drive

Drive is the drive letter followed by a colon and an optional backslash (might also work on UNCs and mapped drives). 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 sub-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

Restores a drive's eject feature.

    Drive, Unlock, Drive

Drive is the drive letter followed by a colon and an optional backslash (might also work on UNCs and mapped drives). The Unlock sub-command 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

Ejects or retracts the tray of a CD or DVD drive.

    Drive, Eject , Drive, 1

To eject other types of media or devices, see example #2 at the bottom of this page.

Drive is the drive letter followed by a colon and an optional backslash (might also work on UNCs and mapped drives). 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`.

This sub-command 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").

This sub-command 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 sub-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.

## Error Handling

[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.

## Related

DriveGet, DriveSpaceFree

## Examples

#1

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

#2: This 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)
    }