Jump to content
Sign in to follow this  
Diana (Cda)

Removing USB flash drive safely,

Recommended Posts

Diana (Cda)

I've searched the board and this comes close to doing what I need: http://www.autoitscript.com/forum/index.ph...remove+hardware

However, again, a drive letter has to be specified which is something I hate. I like solutions that operate using relative paths vs. absolute ones, for example. The above won't work on all computers as the USB flash drive usu. gets assigned a different letter from computer to computer and I'd rather have an automated solution rather than asking the user each time what the drive is. Defeats the purpose of making a script I feel.

I'd rather have as straightfoward and simple non-GUI solution that searches out the name of the device instead, if such a thing is possible.

Every host computer lists it as:

"Memorex Mini TD 001B USB Device"

so was hoping that a script would be able to remove the drive _without_ perhaps unmounting something it shouldn't. That would be bad. But a script would be great and save the hassle of the systray thing and searching through a dozen lines or so of displayed components.

TIA. :P

Share this post


Link to post
Share on other sites
rover

there seems to be 2 modes of USB ejection, one powers off the port

(I have USB keys with power leds that turn off when ejected using tray icon)

and ChrisL's VBS to AutoIt script 'Eject' method from that thread you linked to in your post.

(leds in my USB keys remain on and drive label in explorer changes to 'Removable Disk' and drive is no longer accessible)

I also use sync and removedrive, until someone comes up with an all script code method that powers off the USB port (power on port would be nice too)

and flushes drive.

WMI gives the USB model information for a USB drive as you posted

however the Scriptomatic examples I tried don't return a drive letter or drive label along with the model and physical disk info

there is also a WMI script that detects USB drives when connected and reports drive model

ideally a method of ejecting or powering off usb ports that uses physical drive info or model would be preferable,

especially for usb hard drives with 2 or more partitions.

USB drive eject by drive label

Use Sysinternals sync.exe to flush drive and ejectdrive function to eject.

-r flush followed by -e eject didn't always work for me, anyway no confirmation info without returning and parsing stdout

and _rundos function always returns 0 with sync.exe, the eject function is better

If any file handles are open when attempting eject, windows pops up a warning message box (In WIN XP SP2+ at any rate)

#include <Process.au3>
$DriveLabel = "USBDRIVELABEL" ; your drive label here
$aDrives = DriveGetDrive("all") ; USB external hard drives report as "Fixed", not "Removable", so use "All" to cover both kinds
If NOT @error Then
    For $i = 1 to $aDrives[0]
        If DriveGetLabel($aDrives[$i]) = $DriveLabel Then
            If DriveStatus($aDrives[$i]) = "READY" Then
                _RunDos("sync.exe -r "&$aDrives[$i]) ; Flush drive cache
                Sleep(2000)
                EjectDrive($aDrives[$i] & "\")
            EndIf
            Exit
        EndIf
    Next
EndIf

Exit

Func EjectDrive($dLetter) ; From VB script, Authors XoLoX and ChrisL
    Local CONST $SSF_DRIVES = 17
    Local $oShell, $oNameSpace, $oDrive, $strDrive
    $strDrive = $dLetter
    $strName=DriveGetLabel($strDrive) & "(" & $strDrive & ")"
    $oShell = ObjCreate("Shell.Application")
    $oNamespace = $oShell.NameSpace($SSF_DRIVES)
    $oDrive = $oNamespace.ParseName($strDrive)
    $oDrive.InvokeVerb ("E&ject")
    If DriveGetLabel($aDrives[$i]) <> $DriveLabel Or DriveGetLabel($aDrives[$i]) = "Removeable Disk" Then
    TrayTip("USB Drive "&$strName&" ejected", "You can now remove the device safely.", 5, 1)
    MsgBox(0,"", $strName & " drive ejected. You can now remove the device safely.",5)
    EndIf
Endfunc
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites
Diana (Cda)

Hmmm, okay. How about a command, then, to just open up the "remove hardware" box? I guess it's the hunting around for the sometimes hidden icon (despite permanently showing, XP likes play games <g>), right-clicking and then scrolling through. A fast click on an AU3 script to bring up that box will at least make it less fiddly. :P

Share this post


Link to post
Share on other sites
rover

Hmmm, okay. How about a command, then, to just open up the "remove hardware" box? I guess it's the hunting around for the sometimes hidden icon (despite permanently showing, XP likes play games <g>), right-clicking and then scrolling through. A fast click on an AU3 script to bring up that box will at least make it less fiddly. :)

Uten supplied that answer in this thread:

SCRIPT: usb_sling, Quickly remove a usb key or other usb device

this launches the hotplug dll Safely Remove Hardware dialog

Run("rundll32.exe shell32.dll,Control_RunDLL hotplug.dll", @SystemDir)

there is yet to be seen a script that does it all:

1 - warn of open file handles (displays open file handles and owning process, option to cancel, continue or close process or handle)

2 - flush drive cache (without need for extra utility)

2 - remove or eject drive as option by drive letter (C:), physical drive name (\\.\PHYSICALDRIVE1) or model of drive (Memorex Mini TD 001B USB Device)

a real problem with removing usb devices is explorer and many other processes hang on to open file handles long after they should have been closed

this is a real annoyance with windows

Quote: 'XP likes play games'

M$ seems to have based the Tray Toolbar behaviour on the "Observer Effect"

http://en.wikipedia.org/wiki/Observer_effect

a process can crash and it's icon is still in the tray until you go to click it and then it disappears.


I see fascists...

Share this post


Link to post
Share on other sites
Diana (Cda)

Quote: 'XP likes play games'

M$ seems to have based the Tray Toolbar behaviour on the "Observer Effect"

http://en.wikipedia.org/wiki/Observer_effect

a process can crash and it's icon is still in the tray until you go to click it and then it disappears.

<lol> Couldn't resist an aside ... that is sooo funny, read the article. Learned a new term. Yes, I now have all icons showing in XP because it kept forgetting which ones I told it to show all the time and which ones to keep hidden all the time and would have to go back constantly and re-direct XP in which it had forgotten to do. So I just changed screen resolution and now have them all showing. I like to be in control and monitor what's happening and have easy access to what's happening in the systray and loss of valuable toolbar real estate offset by this result of XP stupidity (or how 'bout XPidity. Lots of that going on.) Anyway, thanks for the new term based on Star Trek. Love it.

Now going back to message and the issue at hand <g> ...

Share this post


Link to post
Share on other sites
Diana (Cda)

Uten supplied that answer in this thread:

SCRIPT: usb_sling, Quickly remove a usb key or other usb device

this launches the hotplug dll Safely Remove Hardware dialog

Run("rundll32.exe shell32.dll,Control_RunDLL hotplug.dll", @SystemDir)
Works for me! This is great. It at least eliminates the fiddly step of rooting around for the icon in the systray to get at this and the right-clicking.

there is yet to be seen a script that does it all:

1 - warn of open file handles (displays open file handles and owning process, option to cancel, continue or close process or handle)

2 - flush drive cache (without need for extra utility)

2 - remove or eject drive as option by drive letter (C:), physical drive name (\\.\PHYSICALDRIVE1) or model of drive (Memorex Mini TD 001B USB Device)

a real problem with removing usb devices is explorer and many other processes hang on to open file handles long after they should have been closed

this is a real annoyance with windows

Well, with the way USB is becoming so widespread, eventually someone will figure something out, I'm sure. Thankfully the simple solution above will help and I like anything that reduces fiddle-time. I'm in my first manager-level contract and anything that saves even minor fiddles and delays and trivial aggravations is a big help! <g>

Thanks. :P

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
Sign in to follow this  

×