Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Ejecting USB Drive

$
0
0

I'm working on a script that will eject a usb flash drive.  I did a search on google and the forums and I found a script that people seem to be using for this purpose.  However when I try to us it, the script runs but it doesn't eject the usb flash drive

 

Here is the script exactly as I am trying to test it

AutoIt         
  1. #include <Process.au3>
  2.  
  3. $DriveLabel = "BACKUP" ; your drive label here
  4.  
  5. $aDrives = DriveGetDrive("all") ; USB external hard drives report as "Fixed", not "Removable", so use "All" to cover both kinds
  6.  
  7.     For $i = 1 to $aDrives[0]
  8.         If DriveGetLabel($aDrives[$i]) = $DriveLabel Then
  9.             Local $dstatus = DriveStatus($aDrives[$i])
  10.             MsgBox(0, "1", "Found Drive, it's status is: " & $dstatus)
  11.             If DriveStatus($aDrives[$i]) = "READY" Then
  12.                 _RunDos("sync.exe -r "&$aDrives[$i]) ; Flush drive cache
  13.                 Sleep(2000)
  14.                 EjectDrive($aDrives[$i] & "\")
  15.             EndIf
  16.             Exit
  17.         EndIf
  18.     Next
  19.  
  20.  
  21. Func EjectDrive($dLetter) ; From VB script, Authors XoLoX and ChrisL
  22. Local CONST $SSF_DRIVES = 17
  23. Local $oShell, $oNameSpace, $oDrive, $strDrive
  24. $strDrive = $dLetter
  25. $strName=DriveGetLabel($strDrive) & "(" & $strDrive & ")"
  26. $oShell = ObjCreate("Shell.Application")
  27. $oNamespace = $oShell.NameSpace($SSF_DRIVES)
  28. $oDrive = $oNamespace.ParseName($strDrive)
  29. $oDrive.InvokeVerb ("E&ject")
  30. MsgBox(0, "2", "We're here in the function")
  31. If DriveGetLabel($aDrives[$i]) <> $DriveLabel Or DriveGetLabel($aDrives[$i]) = "Removeable Disk" Then
  32.     MsgBox(0, "3", "We're here in the if statement")
  33.     TrayTip("USB Drive "&$strName&" ejected", "You can now remove the device safely.", 5, 1)
  34.     MsgBox(0,"", $strName & " drive ejected. You can now remove the device safely.",5)

It's pretty much the exact script found in numerous posts here on the fourm.  I only modified the drive label and added some message boxes to see where in the script the problem was.

 

the script runs fine up until the "if" statement in function EjectDrive().  If I understand the script, that if statement determines if the drive has actually been ejected.  If it has then it send the message that it's save to remove the device.

 

When I run the script is does not actually eject the drive so I'm sure that's why the code in the if statement isn't executed.

 

The Question: What do I need to do to get the script to actually eject the flash drive.

 

P.S. I'm using windows 7 32-bit if it makes a difference.


Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>