Hello Daniweb,

I am writing an App that uses two buttons, two radiobuttons, and a textbox to use the Windows shell command to print the "getmac" command to a text file: i.e.
"getmac /v >> whatever.txt"

I want to save the "wireless" MAC addresses to a text file from a bunch of computers with this app.

i.e. output:

COMPUTER: name-PC
_________________________________________


WIRELESS MAC: 00-22-03-FF-EE-DD


_________________________________________
COMPUTER: nameTwoXP-PC
_________________________________________
WIRELESS MAC: 00-00-FF-32-55-33

and so on by the ">>" symbol in the shell command "getmac \v >> text.txt " and true at the end of the writeAllText (...., true) appends each next PC on to the list.

This app can be great for computer WIFI inventories.

What I need is some code to use the second button to filter out the lines with "wireless" to the end of the that line (which includes the WIFI MAC Address) and then delete everything else until the delimiter "COMPUTER: ".

I have played with the code and below is what I have used so far to get the WIFI MACs but I do not think the output is clean enough.

Any hints?

My code is below:
______________________________________________________________________

Imports System.Text.RegularExpressions
Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton2.Checked = True Then


Dim name As String = My.Computer.Name
Dim path As String = My.Computer.FileSystem.CurrentDirectory & "\MACLIST.txt"
My.Computer.FileSystem.WriteAllText(path, "______________________________________" & vbNewLine & vbNewLine, True)
My.Computer.FileSystem.WriteAllText(path, "COMPUTER: " & name & vbNewLine, True)
' My.Computer.FileSystem.CurrentDirectory &
Shell("cmd.exe /c getmac /v >> MACLIST.txt", AppWinStyle.MaximizedFocus)
My.Computer.FileSystem.WriteAllText(path, "______________________________________" & vbNewLine & vbNewLine, True)



ElseIf RadioButton1.Checked = True Then
TextBox1.Enabled = True
If String.IsNullOrEmpty(TextBox1.Text.Trim()) Or TextBox1.Text.Contains(".") Then
MsgBox("Please enter a valid output file name")
Else
Dim name1 As String = My.Computer.Name
Dim path1 As String = My.Computer.FileSystem.CurrentDirectory & "\" & TextBox1.Text.Trim & ".txt"
My.Computer.FileSystem.WriteAllText(path1, "______________________________________" & vbNewLine & vbNewLine, True)
My.Computer.FileSystem.WriteAllText(path1, "COMPUTER: " & name1 & vbNewLine, True)
' My.Computer.FileSystem.CurrentDirectory &


Shell("cmd.exe /c getmac /v >> " & TextBox1.Text.Trim & ".txt", AppWinStyle.MaximizedFocus)
My.Computer.FileSystem.WriteAllText(path1, "______________________________________" & vbNewLine & vbNewLine, True)



End If
End If


TextBox1.Enabled = True


Button2.Select()



End Sub



Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
TextBox1.Enabled = False
ElseIf RadioButton1.Checked = True Then
TextBox1.Enabled = True



End If



End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


If RadioButton1.Checked = True Then



'"([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])"
Dim fso, inputFile, outputFile
Dim str As String


fso = CreateObject("Scripting.FileSystemObject")


'1 means for reading


inputFile = fso.OpenTextFile(TextBox1.Text.Trim & ".txt", 1)


str = inputFile.ReadAll


'modify this string, replace required characters


str = Replace(str, "Wireless", "_______________________________" & vbNewLine & "******WIRELESS Next Line******" & vbNewLine & "_______________________________" & vbNewLine)
str = Replace(str, "Connection Name Network Adapter Physical Address    Transport Name ", "")
str = Replace(str, "=============== =============== ===================", "")
str = Replace(str, "==========================================================", "")


'write back


outputFile = fso.CreateTextFile(TextBox1.Text.Trim & ".txt", True)


outputFile.Write(str)
ElseIf RadioButton2.Checked = True Then
Dim a As String



Dim fso, inputFile, outputFile
Dim str As String


fso = CreateObject("Scripting.FileSystemObject")


'1 means for reading


inputFile = fso.OpenTextFile("MACLIST.txt", 1)


str = inputFile.ReadAll


'modify this string, replace required characters


str = Replace(str, "Wireless", "_______________________________" & vbNewLine & "******WIRELESS Next Line******" & vbNewLine & "_______________________________" & vbNewLine)
str = Replace(str, "Connection Name Network Adapter Physical Address    Transport Name ", "")
str = Replace(str, "=============== =============== ===================", "")
str = Replace(str, "==========================================================", "")
'str = Replace(str, , "")


'write back


outputFile = fso.CreateTextFile("MACLIST.txt", True)


outputFile.Write(str)
'System.Diagnostics.Process.Start("MACLIST.txt")
End If


End Sub



End Class

Thanks in advance,
m1234ike

Dani AI

Generated

A short, practical plan that solves this reliably and keeps things maintainable.

is already capturing each machine block into a single file; correctly pointed out that producing a stable, machine-name + MAC output up front makes life easier. If you already have the big text file, here are two safe options: (A) a small post‑processor that reads the file block by block using the per‑machine header and writes only wireless MAC lines, or (B) avoid text parsing altogether and query the OS for adapters (WMI / PowerShell) and write just the wireless MACs. Option B is more robust across Windows versions and languages; option A is quick when you already have the file and want a fast cleanup.

The parser below follows option A: it scans the file line by line, treats each header line that starts the next machine block as a delimiter, looks for adapter lines that mention wireless (case‑insensitive), and then searches the same line and a few following lines for a MAC address pattern. It writes one compact record per machine when a wireless MAC is found.

Imports System.IO
Imports System.Text.RegularExpressions

Sub ExtractWirelessMacs(inputPath As String, outputPath As String)
    Dim macRe As New Regex("\b(?:[0-9A-Fa-f]{2}(?:[-:])){5}[0-9A-Fa-f]{2}\b", RegexOptions.Compiled)
    Using sr As New StreamReader(inputPath)
        Using sw As New StreamWriter(outputPath, False)
            Dim currentComputer As String = "(unknown)"
            While Not sr.EndOfStream
                Dim line As String = sr.ReadLine()
                If line Is Nothing Then Continue While
                If line.StartsWith("COMPUTER:", StringComparison.OrdinalIgnoreCase) Then
                    currentComputer = line.Substring(9).Trim()
                    Continue While
                End If
                If line.IndexOf("wireless", StringComparison.OrdinalIgnoreCase) >= 0 _
                   Or line.IndexOf("wi-fi", StringComparison.OrdinalIgnoreCase) >= 0 Then
                    Dim candidate As String = line
                    Dim found As Boolean = False
                    Dim lookahead As Integer = 0
                    While lookahead <= 3 AndAlso Not found
                        Dim m = macRe.Match(candidate)
                        If m.Success Then
                            sw.WriteLine("COMPUTER: " & currentComputer)
                            sw.WriteLine("WIRELESS MAC: " & m.Value)
                            sw.WriteLine()
                            found = True
                        Else
                            If sr.EndOfStream Then Exit While
                            candidate = sr.ReadLine()
                            lookahead += 1
                        End If
                    End While
                End If
            End While
        End Using
    End Using
End Sub

Notes and tips

  • Prefer option B (WMI or PowerShell) when possible: query adapters directly and filter by NetConnectionID/InterfaceDescription containing "Wireless" or "Wi‑Fi" — that avoids brittle text parsing and localization problems.
  • Make the MAC regex accept both hyphens and colons and normalize output (uppercase, consistent separator).
  • Virtual adapters and Bluetooth can appear as wireless; detect and skip known virtual names if needed.
  • If you must parse existing files, increase lookahead if MAC often appears on the following line.

Recommended Answers

All 2 Replies

Wow, I think you are making this way more complicated than it should be. Most of this can be done with batch files easier than with vb.

1. First off you haven't shown how to get the computer name.
echo COMPUTER: %COMPUTERNAME% >>txtfile.txt

2. have the getmac command ( Assuming windows 7 ) get rid of the trash for you.
getmac /FO csv /NH >>txtfile.txt
This will give you one line comma delimited with the mac first and device id second.

we could probably combine all of this into a very small batch file to have it printout computername,mac if we tried hard enough

Hello Daniweb,

The reason I need the delimeter is to filter out the Wireless MAC or MACs.

I want to cleanup the output of the text file to simply show the Wireless adapter lines.

The text file printout is in a table format but as a text file it can be cleaned up line-by-line.

I want just the Wireless MACs for a networking reason or to enter MACs for "MAC Address Filtering".

Any hints?

Thannks in advance,

m1234ike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.