Jump to content

Eject USB drive while running script


Chorda
 Share

Recommended Posts

Hi,

I'm trying the script that's been shared to safely eject a USB Flash drive. It works fine except that since I am running the script from that USB drive I get a message that: 'Removable Disk (K:) is currently in use. If you eject this now...'

Well, that makes sense. So essentially I guess what I need to do is to close my script and free all resources used by my USB drive, and then eject it. But then again, if my script is closed, I can't eject the drive... Any idea? Thanks

CODE
#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

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

photonbuddy

Trying to simply FileDelete() doesn't work because the file is locked

Example:

$sCMDLine = ':CheckFile' & @CRLF & _
            'If exist "' & @ScriptFullPath & '" GOTO :DelFile' & @CRLF & _
            'Goto :CheckFile' & @CRLF & _
            ':DelFile' & @CRLF & _
            'del /f /q "' & @ScriptFullPath & '"' & @CRLF & _
            'del /f /q "' & @TempDir & '\~deltmp.bat"'
            
$sCMDLine = _StringANSI2OEM($sCMDLine)

$hFile = FileOpen(@TempDir & "\~deltmp.bat", 2)
FileWrite($hFile, $sCMDLine)
FileClose($hFile)

MsgBox(0, "Warning", "Now script will be deleted")
Run(@TempDir & "\~deltmp.bat", @TempDir, @SW_HIDE)

Func _StringANSI2OEM($strText)
    Local $buf = DllStructCreate("char["& StringLen($strText)+1 &"]")
    Local $ret = DllCall("User32.dll", "int", "CharToOem", "str", $strText, "ptr", DllStructGetPtr($buf))
    If Not(IsArray($ret)) Then Return SetError(1, 0, '')
    If $ret[0]=0 Then Return SetError(2, $ret[0], '')
    Return DllStructGetData($buf, 1)
EndFunc  ;==> _StringANSI2OEM

:)

Edited by rasim
Link to comment
Share on other sites

I have been trying to get my script working for some time now, but it fails miserably.

I copy the executable to @AppDataDir and run it from there, exiting the original script.

The problem is, while the program is running, I can't eject the USB drive, and get the "The device 'Generic volume' cannot be stopped right now" error message.

I've tried running the script directly from the USB drive from explorer, but get the same results.

Any ideas on how to get this to work?

I'm using WinXP SP3.

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