Hello,
How to uniquely differentiate two similar substrings in a string (Shell.Result)? I tried several approaches, but all my attempts failed because it systematically detects “Hardware Port: Ethernet” in “Hardware Port: Ethernet Adapter”!
Var sh As New Shell
sh.Execute("/usr/sbin/networksetup -listallhardwareports")
if sh.IsRunning Then
sh.Poll
end if
Dim inputString As String = sh.Result
Dim pattern As String = "\bHardware Port: Ethernet\b"
Dim regex As New RegEx
regex.SearchPattern = pattern
Dim match As RegExMatch
match = regex.Search(inputString)
If sh.ErrorCode = 0 Then
If match <> Nil Then
MsgBox "An Ethernet port is present""
Else
MsgBox "No Ethernet port is present!”
End If
End If
Thanks.
Var sh As New Shell
sh.Execute("/usr/sbin/networksetup -listallhardwareports")
if sh.IsRunning Then
sh.Poll
end if
Dim inputString As String = sh.Result
Dim pattern As String = "Hardware Port: Ethernet"
Dim pattern1 As String = "Hardware Port: Ethernet Adapter"
var test as Boolean = False
for x as integer = 1 to inputString.CountFields(EndOfLine)
var part as String = inputString.NthField(EndOfLine,x)
if part.IndexOf(pattern)>=0 and part.IndexOf(pattern1)<0 then test=true
next
If sh.ErrorCode = 0 Then
If test Then
MsgBox "An Ethernet port is present!"
Else
MsgBox "No Ethernet port is present!"
End If
End If
Also :
Var sh As New Shell
Var EthernetPort As Boolean = False
sh.Execute("/usr/sbin/networksetup -listallhardwareports")
if sh.IsRunning Then
sh.Poll
end if
If sh.ErrorCode = 0 Then
Var inputString As String = sh.Result
Var lines() As String
lines = Split(inputString, EndOfLine)
For Each line As String In lines
If line = "Hardware Port: Ethernet" Then
EthernetPort = true
'MsgBox "An Ethernet port is present!"
End If
Next
Else
MsgBox("Error")
End If
not exactly true, when You take a look at actual terminal output of “”/usr/sbin/networksetup -listallhardwareports"…
macbook-m1props:~ polonus$ /usr/sbin/networksetup -listallhardwareports
Hardware Port: Ethernet Adapter (en4)
Device: en4
Ethernet Address: 76:aa:c6:a9:ce:6e
Hardware Port: Ethernet Adapter (en5)
Device: en5
Ethernet Address: 76:aa:c6:a9:ce:6f
Hardware Port: Ethernet Adapter (en6)
Device: en6
Ethernet Address: 76:aa:c6:a9:ce:70
Hardware Port: Thunderbolt Bridge
Device: bridge0 (...)
Lines with …Adapter" don’t end just like that…