Jump to content

Ejecting a removable drive


aleph01
 Share

Recommended Posts

I currently have a script ejecting USB drives in public computers by calling a third party software.  Being the purist I am, I would rather have a script that doesn't need to call other software to do the work.  I don't see any DriveEject or similar command.  Am I missing something?  How would this be done purely in AutoIt?  My code (for the curious):

#include <AutoItConstants.au3>

Local $aArray = DriveGetDrive($DT_REMOVABLE)
If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(0, "", "It appears an error occurred.")
Else
    For $i = 1 To $aArray[0]
        ; Convert the drive letter to uppercase.
        $L = StringUpper ($aArray[$i])
        ; Eject the drive.
        RunWait('"C:\Program Files (x86)\USB_Disk_Eject\USB_Disk_Eject.exe" /REMOVELETTER " & $L', "", @SW_HIDE)
    Next
EndIf

MsgBox (0, _
        "Safe Removal", _
        "It is now safe to remove your USB drive(s).")

If I can get out of using USB_Disk_Eject.exe, I would be a happy camper.

Thanks,

_aleph_

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

  • Moderators
#RequireAdmin

Local $WMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Local $oUSB = $WMI.ExecQuery("SELECT * FROM Win32_Volume where DriveType = '2'")
Local $oObj = ObjCreate("Shell.Application")

For $sItem In $oUSB
    $oObj.NameSpace(17).ParseName($sItem.driveletter).InvokeVerb("Eject")
Next

Admittedly, I have not tried it when many USB drives are attached, but it should work.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

aleph01,

Win7 Pro 64bit at current maintenance levels...This works for multiple USB sticks but not my Passport external drive...

#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>
#include <array.au3>

Local $aArray = DriveGetDrive($DT_REMOVABLE)
;_arraydisplay($aArray)

If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(0, "", "It appears an error occurred.")
Else
    For $i = 1 To $aArray[0]
        if not _WinAPI_EjectMedia(stringupper($aArray[$i])) then exit msgbox(17,'Error Ejecting Drive','Drive = ' & $aArray[$i])
    Next
EndIf

MsgBox (0, _
        "Safe Removal", _
        "It is now safe to remove your USB drive(s).")

kylomas

edit:  JL, your script behaves the same as mine.  I'm verifying results by WIN+E before and after execution.

Edited by kylomas
additional data

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Wow, jackpot!  Thanks JLogan3o13, kylomas, and j0kky.  I now have WMI, WinAPI and C++ to work with.  I looked for the WinAPI in the help file.  I must have skimmed over it by accident.  Thanks again all!  I'll keep each method squirrelled away for future reference regardless of which I end up implementing this time around.

_aleph_

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...