Gary''s Student said:
I think you have a great idea. There is probably an API that can get this
MAC address directly. I'll rummage around and update the post tomorrow if I
find anything
It is possible to get a collection of MAC addresses for a computer using WMI
Win32_NetworkAdapter class MACAddress property, then compare that collection
with an array of MAC addresses:
Sub MACTest()
s = Timer
MACArray = Array _
("00:04:61:50:20:07" & _
"01:03:51:60:11:08" & _
"BB:02:12:57:21:09" & _
"CC:04:41:45:00:55")
WQLQuery = "Select * From Win32_NetworkAdapter"
Set objWMI = GetObject("winmgmts:root\cimv2")
Set colAdapters = objWMI.ExecQuery(WQLQuery)
For Each objAdapter In colAdapters
For Each MACAddress In MACArray
If MACAddress = objAdapter.MACAddress Then
'Debug.Print MACAddress, objAdapter.Name
End If
Next
Next
Debug.Print "WMI:", Timer - s
End Sub
I also modified Steve Yandl's code using objStdOut.ReadAll to store the
entire stdOut in a string and perform the same comparison:
Sub GetMyMAC()
s = Timer
MACArray = Array _
("00-04-61-50-20-07", _
"01-03-51-60-11-08", _
"BB-02-12-57-21-09", _
"CC-04-41-45-00-55")
Set objShell = CreateObject("WScript.Shell")
Set objWshExec = objShell.Exec("ipconfig /all")
Set objStdOut = objWshExec.StdOut
strIpConfig = objStdOut.ReadAll
For Each MACAddress In MACArray
If InStr(strIpConfig, MACAddress) Then
'Debug.Print MACAddress
End If
Next
Debug.Print "WshShell: ", Timer - s
End Sub
I then added code to your sample using FileSystemObject to read ip.txt into
a string:
Sub BigMac()
s = Timer
MACArray = Array _
("00-04-61-50-20-07", _
"01-03-51-60-11-08", _
"BB-02-12-57-21-09", _
"CC-04-41-45-00-55")
x = Shell("cmd.exe /c ipconfig/all > c:\ip.txt", 0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\ip.txt", 1)
strIpConfig = f.ReadAll
For Each MACAddress In MACArray
If InStr(strIpConfig, MACAddress) Then
'Debug.Print MACAddress
End If
Next
f.Close
Debug.Print "FSO:", Timer - s
End Sub
I ran all three subs a few times and it looks like that FSO method is the
fastest (about 15 times faster than WshShell sample and 20 times than WMI on
my machine) even with opening and reading a text file from the disk.