When Connect/Dissconnect a specific USB Drive.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
KBeeeR
Posts: 5
Joined: 10 Apr 2021, 19:40

When Connect/Dissconnect a specific USB Drive.

10 Apr 2021, 20:27

Hello,

I need when I plug in a specific USB device {do something}, and when I plug out that USB {do something}.
I searched a lot in the forums before I asked for a help, and the closest code I found for what I need was this:

Code: Select all

OnMessage(0x219, "WM_DEVICECHANGE")
return

EventHandler(letter, event)
	{
	if event
		{
      		SoundBeep
		SoundBeep
		}

	else
		{
		SoundBeep
		}
	}

WM_DEVICECHANGE(wp, lp)
{
   static DBT_DEVICEARRIVAL := 0x8000, DBT_DEVICEREMOVECOMPLETE := 0x8004, DBT_DEVTYP_VOLUME := 2
   
   if NumGet(lp + 4, "UInt") = DBT_DEVTYP_VOLUME && (wp = DBT_DEVICEARRIVAL || wp = DBT_DEVICEREMOVECOMPLETE) {
      dbcv_unitmask := NumGet(lp + 12, "UInt")
      letter := Chr(Asc("A") + ln(dbcv_unitmask)/ln(2))
      timer := Func("EventHandler").Bind(letter, wp = DBT_DEVICEARRIVAL)
      SetTimer, % timer, -10
   }
}
This code does what I need but for every USB device..
I want it only to make the SoundBeep on a specific usb device that I changed it's drive letter to "S" in Disk Management.

Thankyou !!
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: When Connect/Dissconnect a specific USB Drive.

10 Apr 2021, 20:58

You could check to see whether that drive letter exists, or you could use the drive's serial number.
KBeeeR
Posts: 5
Joined: 10 Apr 2021, 19:40

Re: When Connect/Dissconnect a specific USB Drive.

10 Apr 2021, 21:09

mikeyww wrote:
10 Apr 2021, 20:58
You could check to see whether that drive letter exists, or you could use the drive's serial number.
I did the "check drive letter" option and it worked perfectly but the USB sometimes get unreadable error.. and I want the code to also work even when the USB device get a not accessible (corrupted and unreadable).
Can you please help in the second option (drive's serial number) ? I'm new to AHK.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: When Connect/Dissconnect a specific USB Drive.

10 Apr 2021, 21:46

If this specific drive is corrupted or unreadable, then how do you hope to determine that it is plugged into your computer? Perhaps you want to fix the drive first?

Some resources in case helpful:

https://www.uwe-sieber.de/usbdlm_e.html

https://www.wikihow.com/Repair-a-USB-Flash-Drive

https://www.rizonesoft.com/downloads/usb-repair/
KBeeeR
Posts: 5
Joined: 10 Apr 2021, 19:40

Re: When Connect/Dissconnect a specific USB Drive.

10 Apr 2021, 22:05

I think we can't check the serial number of the USB if its unreadable .. that's what you mean ?

what i'm trying to do is:
I have a "Relay Timer Device" connected between the USB and my PC and it connect/disconnect the USB automatically, when it connect or disconnect I want to run a script,
So it's unnecessary to make the USB readable because I don't need any files from inside it..
and because of the repeated connect/disconnect the USB gets corrupted, but it keeps connecting and showing in "This PC" as drive "S:".

isn't there a way to check if Drive "S:" is connected, not by folder or USB serial number ?
:cry:
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: When Connect/Dissconnect a specific USB Drive.

11 Apr 2021, 06:52

I believe that your question is how to detect a specific storage device that is connected but not mapped to a drive letter. I do not have the answer, but someone else on the forum probably knows.
KBeeeR
Posts: 5
Joined: 10 Apr 2021, 19:40

Re: When Connect/Dissconnect a specific USB Drive.

11 Apr 2021, 07:35

Thank you sir!
Still waiting for a help guys ❣

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter, Google [Bot] and 234 guests