bartonjd Posted January 23, 2014 Posted January 23, 2014 I would like to write a script that will perform an action when I pull my USB drive out of the slot, e.g. lock my windows session. I found >this script which can detect a usb insertion. However I want to detect if a USB drive with a specific name is ejected. Is there any way to do this?
MilesAhead Posted January 23, 2014 Posted January 23, 2014 You could convert this C# WMI call: http://stackoverflow.com/questions/620144/detecting-usb-drive-insertion-and-removal-using-windows-service-and-c-sharp My Freeware Page
Inverted Posted January 24, 2014 Posted January 24, 2014 Assuming the USB is already inserted when the script runs, I'd use this: #include <MsgBoxConstants.au3> Local $aArray = DriveGetDrive("ALL") Local $ourDrive = "" If @error Then MsgBox($MB_SYSTEMMODAL, "", "It appears an error occurred while enumerating the drives, exiting...") Else For $i = $aArray[0] To 1 Step -1 If DriveGetLabel($aArray[$i]) = "win7_usb" Then $ourDrive = $aArray[$i] ExitLoop EndIf Next EndIf If Not $ourDrive Then MsgBox($MB_SYSTEMMODAL, "", "Our drive was not found, exiting...") Exit EndIf While True Sleep(2000) if DriveStatus($ourDrive) <> "READY" Then MsgBox($MB_SYSTEMMODAL, "", "Our drive was REMOVED !!!") Exit EndIf WEnd
jaberwacky Posted January 24, 2014 Posted January 24, 2014 If you want to know when the user ejects a usb device then look at RegisterDeviceNotifications in my signature. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
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