You can use a WMI query to find out if the device is an USB one or not.
Here is an example :
Local $aUSBDrives = _ListUSBVolumes()
Local $iRunFromUSB = 0
For $i = 1 To $aUSBDrives[0]
If StringRegExp( @ScriptDir, "(?i)^" & $aUSBDrives[$i]) Then
$iRunFromUSB = 1
ExitLoop
EndIf
Next
If $iRunFromUSB Then
MsgBox(0, "", "OK")
Else
MsgBox(16, "", "The program must be launched from an USB drive !")
EndIf
; #FUNCTION# ====================================================================================================================
; Name ..........: _ListUSBVolumes
; Description ...: List USB volumes
; Syntax ........: _ListUSBVolumes()
; Parameters ....: None
; Return values .: An array of strings (USB drive letter followed by colon) of drives found.
; The zeroth array element contains the number of drives.
; Author ........: jguinch
; ===============================================================================================================================
Func _ListUSBVolumes()
Local $aResult[1]
Local $oDrive, $sDeviceID, $oPartitions, $oPartition, $oLogicalDisks, $oLogicalDisk, $sDriveLetter
Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
Local $oDiskDrives = $oWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive Where InterfaceType = 'USB'")
For $oDrive In $oDiskDrives
$sDeviceID = StringReplace($oDrive.DeviceID, "\", "\\")
$oPartitions = $oWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
$sDeviceID & """} WHERE AssocClass = " & _
"Win32_DiskDriveToDiskPartition")
For $oPartition In $oPartitions
$oLogicalDisks = $oWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
$oPartition.DeviceID & """} WHERE AssocClass = " & _
"Win32_LogicalDiskToPartition")
For $oLogicalDisk In $oLogicalDisks
$sDriveLetter = $oLogicalDisk.DeviceID
If $sDriveLetter <> "" Then
Redim $aResult[ UBound($aResult) + 1]
$aResult[ UBound($aResult) - 1] = $sDriveLetter
EndIf
Next
Next
Next
$aResult[0] = UBound($aResult) - 1
Return $aResult
EndFunc