List of USB serial numbers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

List of USB serial numbers

28 Mar 2024, 20:50

Hi there ! I saw a script in viewtopic.php?t=98931 by garry, the 1st script in that post. May I ask, if I have a list of USB serial numbers, and the USB I just plugged in does NOT match any in the list, it'll notify me with msg "USB NOT RECOGNIZED". Can you help me with this ? This is what I mean. This is part of garry's script. Thankyou very much.

Code: Select all

DriveGet    , serial2 , serial   , %last%     
if (serial2 in "4383746507,2957886184,7333182867")         ; LIST OF SERIAL NUMBERS
{ msg USB NOT RECOGNIZED}
  else
 {  run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}  }
return
Last edited by yabab33299 on 28 Mar 2024, 21:26, edited 3 times in total.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

28 Mar 2024, 21:07

I haven't looked at garry's script, but knowing garry, I assure you that he did not have this line:

if (serial2="111,222,333,aaa,bbb,ccc") ; LIST OF SERIAL NUMBERS

That statement does NOT check for a match in a list (and garry surely knows that). I can't speak to the rest of your script, but if you want to check if a variable matches one of the items in a list, try this:

if serial2 in 111,222,333,aaa,bbb,ccc ; LIST OF SERIAL NUMBERS

Doc here: https://www.autohotkey.com/docs/v1/lib/IfIn.htm

Regards, Joe
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

28 Mar 2024, 21:12

JoeWinograd wrote:
28 Mar 2024, 21:07
I haven't looked at garry's script, but knowing garry, I assure you that he did not have this line:
I tried your suggestion. It seems script's msg box pops up regardless of whether the serial numbers match with current plugged-in USB one.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

28 Mar 2024, 21:25

You may have misunderstood my comment.

I think you are trying to see if the serial number that comes back from the DriveGet command (in the serial2 variable) matches one of the serial numbers in a list.

Is that right?

If so, the If statement that you want is:

if serial2 in 111,222,333,aaa,bbb,ccc ; LIST OF SERIAL NUMBERS

where 111,222,333,aaa,bbb,ccc etc. is a list of the serial numbers separated by commas, as documented at the If ... in command. Regards, Joe
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

28 Mar 2024, 21:38

JoeWinograd wrote:
28 Mar 2024, 21:25
I think you are trying to see if the serial number that comes back from the DriveGet command (in the serial2 variable) matches one of the serial numbers in a list. Is that right?
YES! got it. Can I inquire how do I divide those actions such as if the serial number were to match a certain serial number, it'll open explorer.exe. If it matches the another one, it'll run notepad.exe, so on and so forth.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

28 Mar 2024, 22:41

The Switch statement is good for that. Something like this:

Code: Select all

Switch serial2
{
  Case "111","222","333":
  Run,explorer.exe

  Case "aaa","bbb","ccc":
  Run,notepad.exe

  Default:
  Msgbox,262192,Error,Serial number not recognized:`n`n%serial2%
}  
Add as many Case statements as you need for different serial numbers and different programs to run.

In addition to the AHK documentation on Switch/Case, you may find my article about it to be helpful:

AutoHotkey Switch-Case
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

29 Mar 2024, 15:25

Hi, thankyou for your suggestion. How come the script opens so many notepad programs when USB is plugged in? I tried return and exitapp but still don't work. I removed the if statement and just only used your switch.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

29 Mar 2024, 15:42

It's impossible for us to guess what your script is doing. If you want help troubleshooting it, post the entire script, or at least a sufficient enough portion of it for us to test it and see the problem. The DriveGet statement works fine, the If ... in works fine, Switch/Case works fine, etc., so it's clearly code/logic in your script that is not working, and we'd have to see it to help in debugging it. Regards, Joe
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

29 Mar 2024, 15:55

Everything else is exact from 1st script in viewtopic.php?p=439323#p439323. I just removed the IF, else

Code: Select all

DriveGet    , serial2 , serial   , %last%     ;- volume serial number
Switch serial2
{
  Case 2957886184:
  Run,explorer.exe

  Case 9383746507:
  Run,notepad.exe

  Default:
  Msgbox,262192,Error,Serial number not recognized:`n`n%serial2%
}  

return
esc::exitapp
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: List of USB serial numbers

29 Mar 2024, 16:35

I tried this , if see these serial-numbers e.g. > serialx:="2565867498,3000530768,0123456789" , show it in GUI-EDIT when USB connect / disconnect > OnMessage(0x219, "notify_change")
( Lastdrive is not always the last new connected drive, it sorts from A-Z , ( I have a drive defined "G" ) )
you can minimize the script, see GUI when connect/disconnect a drive

Code: Select all

;- MODIFIED = 20240329/2
;- CREATED  = 20240329
;-
#Requires AutoHotkey v1.1.33.11
#Warn
#NoEnv
;-
serialx:="2565867498,3000530768,0123456789"                     ;- <<<< add wanted serialnumbers
;-
DetectHiddenWindows, On
Setworkingdir,%a_scriptdir%
setformat,float,0.2
SendMode Input
SetBatchLines -1
SetWinDelay, 0
filename1:="Active Drives  / ESC= minimize  / F9= Show again "
Gui,2:default
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
Gui,2: -dpiscale
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
 Gui,2:Font,s12 ,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
 Gui,2:Font,s10 ,Lucida Console
else
 Gui,2:Font,s8  ,Lucida Console
;=============================================================
;Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,+AlwaysOnTop
Gui,2:color,black,black
;Gui,2: font,s10,Tahoma
Gui,2: Margin,0,0
T1:=(wa*3)/xx,T2:=(wa*5)/xx,T3:=(wa*5)/xx,T4:=(wa*5)/xx,T5:=(wa*5)/xx,T6:=(wa*5)/xx,T7:=(wa*5)/xx,T8:=(wa*5)/xx,T9:=(wa*5)/xx,T10:=(wa*7)/xx,T11:=(wa*10)/xx,T12:=(wa*5)/xx
diff1:=(wa*2)/xx
wlv:=(t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12+diff1)
x:=(wa*1)/xx,y:=(ha*1)/xx,h:=(ha*14)/xx
col:="F0F0F0"
Gui,2: Add, ListView,x%x% y%y% w%wlv% h%h% Grid vLV1 +altsubmit background%col%, Drive|STATUS|TYPE|FORMAT|Capacity-MB|FREE-MB|Capacity-GB|FREE-GB|Procent|SerialNR|LABEL|StatusCD
LV_ModifyCol(1,T1),LV_ModifyCol(2,T2),LV_ModifyCol(2,T2 "Center"),LV_ModifyCol(3,T3),LV_ModifyCol(3,"Center"),LV_ModifyCol(4,T4),LV_ModifyCol(4,"Center"),LV_ModifyCol(5,T5),LV_ModifyCol(5,"Right"),
LV_ModifyCol(6,T6),LV_ModifyCol(6,"Right"),LV_ModifyCol(7,T7),LV_ModifyCol(7,"Right"),LV_ModifyCol(8,T8),LV_ModifyCol(8,"Right"),LV_ModifyCol(9,T9),LV_ModifyCol(9,"Center"),
LV_ModifyCol(10,T10),LV_ModifyCol(10,"Right"),LV_ModifyCol(11,T11),LV_ModifyCol(11,"Right"),LV_ModifyCol(12,T12),LV_ModifyCol(12,"Center")
x:=(wa*1)/xx,y:=(ha*16.5)/xx
Gui,2: add,edit, x%x% y%y% w%wlv% r1 cyellow vED1,
wg:=(wlv+diff1)
x:=(wa*2)/xx,y:=(ha*2)/xx,h:=(ha*21)/xx
Gui,2: Show,x%x% y%y% w%wg% h%h% ,%filename1%
gosub,drivegetshow
OnMessage(0x219, "notify_change")
Return
;---------------
2GuiClose:
exitapp
;---------------
~ESC::
Gui,2:minimize
return
;---------------
notify_change(wParam, lParam, msg, hwnd)
{
Gui,2: Show,
gosub,drivegetshow
}
;---------------
~$F9::
Gui,2: Show,
drivegetshow:
Guicontrol,2:,ED1,
LV_Delete()
I:=0
DriveGet, A, List,
allserial:=""
loop,10
  d%a_index%:=""
loop,parse,A
 {
 i++
 type1:="",cap1:="",free1:="",fs1:="",label1:="",serial1:="",status1:="",statusCD:="",drv:=""
 x:=a_loopfield
 drv=%x%:\
      DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
      DriveGet       , cap1    , capacity , %DRV%
      DrivespaceFree , free1   ,            %DRV%
      DriveGet       , fs1     , fs       , %DRV%     ;- FAT
      DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
      DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
      DriveGet       , status1 , status   , %DRV%
      DriveGet       , statusCD, StatusCD , %DRV%     ;- CDRom CDFS Audio CD open / stopped 
 prc   :=(free1*100)/cap1
 cap1a :=StrReplace(cap1 , Substr(cap1 , -2), "'" Substr(cap1 , -2))
 free1a:=StrReplace(free1, Substr(free1, -2), "'" Substr(free1, -2))
 cap2  :=Floor(cap1 /1024)
 free2 :=Floor(free1/1024)
 allserial .= serial1 . ","
 d%i% := serial1
 if (type1="CDROM")
   LV_Add("", drv,status1,type1,fs1,,,,,,serial1,label1,statuscd)
 else
   LV_Add("", drv,status1,type1,fs1,cap1a,free1a,cap2,free2,prc . "  %",serial1,label1,statuscd)
 } 
 u:=0, e:=""
loop,parse,serialx,`,
  {
  u++
  y:=a_loopfield
  if Y=
    continue
  f:=d%u%
  if f in %serialx%
     e .= f . ","  
  }
 stringtrimright,e,e,1  
 if e<>  
   Guicontrol,2:,ED1,HERE the SEARCHED SERIAL-Numbers > %e%  
 else
   Guicontrol,2:,ED1,
return
;========================================================== 

Last edited by garry on 29 Mar 2024, 16:43, edited 2 times in total.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

29 Mar 2024, 16:36

I don't want to guess at how you put together your code with the first script in garry's first post at that other thread. Please post the entire script that you are running.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

29 Mar 2024, 16:40

Code: Select all

#persistent
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
settimer,checkdrive,2000
}
return

checkdrive:
i=0
alldrives:=""
DriveGet, A, List,
StringSplit,D, A,
loop,%d0%
 {
 DRV:= % d%a_index% . ":\"
 i++
      DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
      DriveGet       , cap1    , capacity , %DRV%
      DrivespaceFree , free1   ,            %DRV%
      DriveGet       , fs1     , fs       , %DRV%     ;- FAT
      DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
      DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
      DriveGet       , status1 , status   , %DRV%
 alldrives .= drv . "  " . type1 . "  " . free1 . "-MB`n"
 }
VT:=D0       ;- totaldrives
last         = %drv%
DriveGet    , serial2 , serial   , %last%     ;- volume serial number
Switch serial2
{
  Case 2957886184:
  Run,explorer.exe

  Case 9383746507:
  Run,notepad.exe

  Default:
  Msgbox,262192,Error,Serial number not recognized:`n`n%serial2%
}  
return
esc::exitapp
[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: List of USB serial numbers

29 Mar 2024, 16:43

I added an example above ....
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: List of USB serial numbers

29 Mar 2024, 16:52

garry wrote:
29 Mar 2024, 16:43
I added an example above ....
Hi, Thankyou for your help. In your new example, I do not know where to insert the switch example provided by JoeWinograd. There's a lot of material in that script.
Last edited by yabab33299 on 29 Mar 2024, 17:31, edited 1 time in total.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: List of USB serial numbers

29 Mar 2024, 17:13

garry's and my post just crossed each other. He's the expert in this, so I'm going to drop out now...I'm sure he'll get you across the finish line. Regards, Joe
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: List of USB serial numbers

29 Mar 2024, 17:52

@JoeWinograd , sorry, no , now I have a problem ( and I'm no expert ... :) )
the last part to open notepad or charmap , it starts several times when connect/disconnect USB , I tried with sleep but no success
First script above was ok , show only text in Edit which works
dont know how this works , maybe should only once go to drivegetshow when USB connect/disconnect , and it takes time when a new drive is detected ...
https://www.autohotkey.com/docs/v1/lib/OnMessage.htm

EDIT : I've modified with > OnMessage(0x0219, "WM_DEVICECHANGE")
First run is OK, maybe when later change USB , maybe can notepad, charpad start several times
;
EDIT2 = Test was OK 20240330 so far
only problem if detect more than one drive when connect/disconnect and match with variable serialx , starts e.g. charmap/notepad several times again ( also while disconnect )
( I usually used without onmessage , see last added device and copied files from D to E )
;-
EDIT-3 = 20240330/1 seems OK , only run to Label A1 if new drive connected ( not run to Label A1 if disconnect ) and only run desired program when process not exists
EDIT-4 = 20240331/2 small modified, run hidden, ( show GUI with F9 ) show only msgbox if USB-Device changed ( and then run desired program depending serial-number )
possible to automatically run a specified script from new inserted USB-DEVICE ( don't abuse )

Code: Select all

;- List of USB serial numbers 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=127973
;-
;- MODIFIED = 20240331/2
;- CREATED  = 20240329
;-
#Requires AutoHotkey v1.1.33.11
#Warn
#NoEnv
;-
serialx:="2328642094,2565867498,3000530768,0123456789"                     ;- <<<< add wanted serialnumbers
;-
DetectHiddenWindows, On
Setworkingdir,%a_scriptdir%
setformat,float,0.2
SendMode Input
SetBatchLines -1
SetWinDelay, 0
filename1:="Active Drives  / ESC= minimize  / F9= Show again "
Gui,2:default
device:="",usbx:=""
global device,usbx
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
Gui,2: -dpiscale hwndUID
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
 Gui,2:Font,s12 ,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
 Gui,2:Font,s10 ,Lucida Console
else
 Gui,2:Font,s8  ,Lucida Console
;=============================================================
;Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,+AlwaysOnTop
Gui,2:color,black,black
;Gui,2: font,s10,Tahoma
Gui,2: Margin,0,0
T1:=(wa*3)/xx,T2:=(wa*5)/xx,T3:=(wa*5)/xx,T4:=(wa*5)/xx,T5:=(wa*5)/xx,T6:=(wa*5)/xx,T7:=(wa*5)/xx,T8:=(wa*5)/xx,T9:=(wa*5)/xx,T10:=(wa*7)/xx,T11:=(wa*10)/xx,T12:=(wa*5)/xx
diff1:=(wa*2)/xx
wlv:=(t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12+diff1)
x:=(wa*1)/xx,y:=(ha*1)/xx,h:=(ha*14)/xx
col:="F0F0F0"
Gui,2: Add, ListView,x%x% y%y% w%wlv% h%h% Grid vLV1 +altsubmit background%col%, Drive|STATUS|TYPE|FORMAT|Capacity-MB|FREE-MB|Capacity-GB|FREE-GB|Procent|SerialNR|LABEL|StatusCD
LV_ModifyCol(1,T1),LV_ModifyCol(2,T2),LV_ModifyCol(2,T2 "Center"),LV_ModifyCol(3,T3),LV_ModifyCol(3,"Center"),LV_ModifyCol(4,T4),LV_ModifyCol(4,"Center"),LV_ModifyCol(5,T5),LV_ModifyCol(5,"Right"),
LV_ModifyCol(6,T6),LV_ModifyCol(6,"Right"),LV_ModifyCol(7,T7),LV_ModifyCol(7,"Right"),LV_ModifyCol(8,T8),LV_ModifyCol(8,"Right"),LV_ModifyCol(9,T9),LV_ModifyCol(9,"Center"),
LV_ModifyCol(10,T10),LV_ModifyCol(10,"Right"),LV_ModifyCol(11,T11),LV_ModifyCol(11,"Right"),LV_ModifyCol(12,T12),LV_ModifyCol(12,"Center")
x:=(wa*1)/xx,y:=(ha*16.5)/xx
Gui,2: add,edit, x%x% y%y% w%wlv% r1 cyellow vED1,
wg:=(wlv+diff1)
x:=(wa*2)/xx,y:=(ha*2)/xx,h:=(ha*21)/xx
Gui,2: Show,Hide x%x% y%y% w%wg% h%h% ,%filename1%
firstrun:=0
gosub,drivegetshow
OnMessage(0x0219, "WM_DEVICECHANGE")
Return
;---------------
2GuiClose:
exitapp
;---------------
~ESC::
Gui,2:minimize
return
;---------------
;---------------
~$F9::
Gui,2: Show,
;--------------
drivegetshow:
Guicontrol,2:,ED1,
LV_Delete()
I:=0
DriveGet, A, List,
allserial:=""
loop,10
  d%a_index%:=""
loop,parse,A
 {
 i++
 type1:="",cap1:="",free1:="",fs1:="",label1:="",serial1:="",status1:="",statusCD:="",drv:=""
 x:=a_loopfield
 drv=%x%:\
      DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
      DriveGet       , cap1    , capacity , %DRV%
      DrivespaceFree , free1   ,            %DRV%
      DriveGet       , fs1     , fs       , %DRV%     ;- FAT
      DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
      DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
      DriveGet       , status1 , status   , %DRV%
      DriveGet       , statusCD, StatusCD , %DRV%     ;- CDRom CDFS Audio CD open / stopped 
 prc   :=(free1*100)/cap1
 cap1a :=StrReplace(cap1 , Substr(cap1 , -2), "'" Substr(cap1 , -2))
 free1a:=StrReplace(free1, Substr(free1, -2), "'" Substr(free1, -2))
 cap2  :=Floor(cap1 /1024)
 free2 :=Floor(free1/1024)
 allserial .= serial1 . ","
 d%i% := serial1
 if (type1="CDROM")
   LV_Add("", drv,status1,type1,fs1,,,,,,serial1,label1,statuscd)
 else
   LV_Add("", drv,status1,type1,fs1,cap1a,free1a,cap2,free2,prc . "  %",serial1,label1,statuscd)
 } 
 u:=0, e:=""
loop,parse,serialx,`,
  {
  u++
  y:=a_loopfield
  if Y=
    continue
  f:=d%u%
  if f in %serialx%
     e .= f . ","  
  }
 stringtrimright,e,e,1  
 if e<>
   {
   Firstrun++   
   Guicontrol,2:,ED1,Show defined SerialNumbers %e%
   if (Firstrun=1)
     gosub,a1
   }
 else
   Guicontrol,2:,ED1,
return
;=================================================================
;- a test-script to run from NEW inserted drive
/*
;-Test_msgbox.ahk
F1:=a_scriptfullpath
SplitPath,f1, name, dir, ext, name_no_ext, drive
msgbox, 262208,%f1%,%f1% = This USB-DEVICE drive is > %drive%
;run,notepad "%f1%"
return
*/
;==================================================================
A1:
succ=0
loop,parse,e,`,
  {
  k:=a_loopfield
  if (k="2328642094")
	   {
	   succ=1
	   Process,Exist,charmap.exe
       If Not ErrorLevel
     	   try,run,Charmap
  	   sch:="ahk_exe charmap.exe"
	   try,run,%usbx%Test_msgbox.ahk  ; try to run a program Test_msgbox.ahk from this USB-DEVICE 
	   sleep,1000
	   } 
  else if (k="2565867498")
	   {
	   succ=1
	   Process,Exist,notepad.exe
       If Not ErrorLevel
     	   try,run,Notepad
       sc:="ahk_exe notepad.exe" 
	   sleep,1000
	   } 
   }
if succ=0
  {  
  msgbox, 262208,Serial NOT FOUND, Drive DEVICE-USB with the desired Serial-Number NOT FOUND`n%serialx%,5   
  ;run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
  }
return
;---------------------------------   
WM_DEVICECHANGE(wParam, lParam)
{
    static init := OnMessage(0x0219, "WM_DEVICECHANGE")
    if (wParam = 0x8000) || (wParam = 0x8004) && ((NumGet(lParam+4, "uint")) = 0x2) {
        m := 0, mask := NumGet(lParam+12, "uint")
        while (mask > 1) && (++m < 0x1A)
            mask >>= 1
        DeviceChangeInfo(Chr(0x41 + m) ":\", wParam)
	    }
}
DeviceChangeInfo(device, state)
{
;Gui,2: Show,
sleep,1000
gosub,drivegetshow
if (state=0x8000)
  {
  msgbox, 262208, ,%device% plugged in,5
  USBX:=device
  gosub,A1
  }
if (state=0x8004)
  msgbox, 262208, ,%device% is removed,5
return
}
;========================================================== 


garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: List of USB serial numbers

31 Mar 2024, 15:17

after complicated made , here an easy logic script ( I think ... ) , driveget serial only from new inserted disk ( not show GUI with all drives )
;- Detect Usb Drive ( from user @teadrinker )
;- viewtopic.php?t=76221&p=330409

Code: Select all

;- Detect Usb Drive  ( from user 'teadrinker' )
;- https://www.autohotkey.com/boards/viewtopic.php?t=76221&p=330409
;-
;- MODIFIED = 20240331
;- CREATED  = 20240331
;-

#Requires AutoHotkey v1.1.33.11
#warn
#NoEnv
#Singleinstance,force
Setworkingdir,%a_scriptdir%
global serial1,letter
OnMessage(0x219, "WM_DEVICECHANGE")
return
;----------------------------
EventHandler(letter, event)
{
if event
   {
   drv:=letter . ":\"
   msgbox, 262208, ,%drv%  <  PLUGGED IN,3
   DriveGet,serial1,serial,%DRV% 
   gosub,A1
   }
else
    msgbox, 262208, ,%letter%  > is removed,3
   return
}
;-------------
A1:
serialx:="2328642094,2565867498,3000530768,0123456789"
if serial1 in %serialx%
{
if (serial1="2328642094")
  {
  Process,Exist,charmap.exe
  If Not ErrorLevel
   try,run,Charmap
  try,run,%letter%:\Test_msgbox.ahk  ; try to run a program 'Test_msgbox.ahk' from this USB-DEVICE 
  }
if (serial1="2565867498")
  {
  Process,Exist,notepad.exe
  If Not ErrorLevel
   try,run,Notepad
  } 
}
else
  {
  msgbox, 262208,Serial NOT FOUND, Drive DEVICE-USB with the desired Serial-Number NOT FOUND in:`n%serialx%,5   
  try,run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
  }
return
;==========================
;------------------
WM_DEVICECHANGE(wp, lp)
{  ;- user 'teadrinker'  https://www.autohotkey.com/boards/viewtopic.php?t=76221&p=330409
   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
   }
}
;=====================================
esc::exitapp
;=====================================
;- a test-script which is in NEW inserted drive to run as test
/*
;-Test_msgbox.ahk
F1:=a_scriptfullpath
SplitPath,f1, name, dir, ext, name_no_ext, drive
msgbox, 262208,%f1%,%f1% = This USB-DEVICE drive is > %drive%
;run,notepad "%f1%"
return
*/
;==================================================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: phenazepamka and 115 guests