Chorda Posted October 13, 2008 Share Posted October 13, 2008 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 More sharing options...
dbzfanatic Posted October 13, 2008 Share Posted October 13, 2008 You could copy it to a the tempdir and run it from there. Go to my website. | My Zazzle Page (custom products)Al Bhed Translator | Direct linkScreenRec ProSimple Text Editor (STE) [TUTORIAL]Task Scheduler UDF <--- First ever UDF!_ControlPaste() UDF[quote name='renanzin' post='584064' date='Sep 26 2008, 07:00 AM']whats help ?[/quote] Link to comment Share on other sites More sharing options...
Prab Posted October 13, 2008 Share Posted October 13, 2008 (edited) Use FileInstall() to drop a script in a temp directory. Run that script then exit the original. Make sure the script you drop deletes itself. Edit: Beaten to it. Edited October 13, 2008 by Prab FolderLog GuiSpeech Assist Link to comment Share on other sites More sharing options...
crazydude Posted November 16, 2008 Share Posted November 16, 2008 Use FileInstall() to drop a script in a temp directory. Run that script then exit the original. Make sure the script you drop deletes itself.How would you make the script delete itself? Link to comment Share on other sites More sharing options...
photonbuddy Posted January 15, 2009 Share Posted January 15, 2009 How would you make the script delete itself?Good question. Trying to simply FileDelete() doesn't work because the file is locked.Any help would be appreciated. Link to comment Share on other sites More sharing options...
rasim Posted January 15, 2009 Share Posted January 15, 2009 (edited) photonbuddy Trying to simply FileDelete() doesn't work because the file is lockedExample: $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 January 15, 2009 by rasim Link to comment Share on other sites More sharing options...
photonbuddy Posted January 15, 2009 Share Posted January 15, 2009 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now