aleph01

Ejecting a removable drive

5 posts in this topic

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. :'(

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
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. :'(

Share this post


Link to post
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