Sign in to follow this  
Followers 0
bartonjd

Detecting USB Removal

4 posts in this topic

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? 

Share this post


Link to post
Share on other sites



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

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  
Followers 0