Hello Daniweb,

I am writing a program to execute the command prompt command "getmac /v >> MACLIST.txt" using a Visual Basic form, including: 2 buttons(getmac and filter the text file) , 2 textboxes (alternate text file name and user name), and two radiobuttons (default text file name or custom name).

Once I select the appropriate radiobutton and push the getmac /v button a text file is created with all of the MAC addresses of the PC.

I want to filter out the Wireless MAC addresses using "regex" or regular expressions.

The initial text file output looks like below:

______________________________________


COMPUTER: TESTMAC2-PC
______________________________________



Connection Name Network Adapter Physical Address    Transport Name
=============== =============== =================== ==========================================================
Wireless Networ Broadcom 802.11 CC-11-FD-1E-7D-A9   \Device\Tcpip_{whatever}
Local Area Conn Atheros AR8132  18-A9-05-E7-84-79   \Device\Tcpip_{whatever}
Bluetooth Netwo Bluetooth Devic 00-11-22-33-44-55   Media disconnected
Local Area Conn TAP-Win32 Adapt 00-AA-59-57-36-39   Media disconnected
VirtualBox Host VMWare Host 00-00-23-20-7D-55   \Device\Tcpip_{whatever}


______________________________________


COMPUTER: TESTMAC1-PC
______________________________________



Connection Name Network Adapter Physical Address    Transport Name
=============== =============== =================== ==========================================================
Wireless Networ Broadcom 802.11 CC-11-FD-1E-7D-A9   \Device\Tcpip_{whatever}
Local Area Conn Atheros AR8132  18-A9-05-E7-84-79   \Device\Tcpip_{whatever}
Bluetooth Netwo Bluetooth Devic 00-11-22-33-44-55   Media disconnected
Local Area Conn TAP-Win32 Adapt 00-AA-59-57-36-39   Media disconnected
VirtualBox Host VMWare Host 00-00-23-20-7D-55   \Device\Tcpip_{whatever}

What I want it to be filtered to look like is below:

______________________________________

COMPUTER: TESTMAC2-PC
______________________________________



Wireless Networ Broadcom 802.11 CC-11-FD-1E-7D-A9   \Device\Tcpip_{whatever}



______________________________________


COMPUTER: TESTMAC1-PC
______________________________________



Wireless Networ Broadcom 802.11 CC-11-FD-1E-7D-A9   \Device\Tcpip_{whateve}

Below is some of my Visual Basic 2008 code:

Imports System.Text.RegularExpressions
Imports System.IO
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, "USER: " & TextBox2.Text.Trim & vbNewLine & vbNewLine, True)


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, "USER: " & TextBox2.Text.Trim & vbNewLine & vbNewLine, True)


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


.THIS IS IF THE DEFAULT MACLIST.TXT RADIO BUTTON IS SELECTED


'HERE I WANT TO USE REGEX TO FILTER THE GETMAC OUTPUT
'I HAVE TO DELETE OR REPLACE THE LINES FROM "COMPUTER: " TO "Wireless etc... to the
'wireless mac address END OF THE LINE AND EVERY OTHER LINE WITH WIRELESS



'THE REST OF THE CODE (OR RADIOBUTTON2.CHECKED) IS SELF EXPLANATORY

Does anybody have any hints?

Thanks in advance,
m1234ike

Because teh getmac command it self has no way to filter the contents, I will suggest not to set the output to the same output file you are writing.

Instead, you can send the output, in overryde mode (only one > sign), to another file name like 'tempmac.txt'.

Then, using the ReadAll function, you can read all the contents ina a variable in your program.

Using the string split function, will separate the variable into lines using the Control Char crlf as separator.

Then you can cicle over the returned lines and consider only those starting with wireless.

Hope this helps

Hello Lolafuertes,

Can you provide me with an example of what you mean?
I wanted to use Regular Expressions but I cannot get the pattern to work.
The key word 'Wireless' is not always at the beginning of the line.
Is what your saying capable of searching for any complete line with the key word?
I trying different Regex.Replace(input, pattern, output) combinations but if you have a way of singling out lines with the key word 'Wireless' please provide an example.

Thanks in advance,
M1234ike

The IndexOf function from the string class will do the trick.

Please read here on how to use it.

Example:

If TheCurrenLineread.ToLower.IndexOf("wireless") <> -1 then
   ' found
Else
   ' Not found
End If

Hope this helps

Hello Lolafuertes,

Thanks for your help.
But, I need something like the following:


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

str = inputFile.ReadAll


'Here I am declaring a "not" found string containing Wireless
Dim strFound As String = Not str.Contains("Wireless")
'Here I am replacing the string with "" or just blank space
str = Replace(str, strFound, "")

I need to get the "Contains" function to work right if it is suitable.
I might need a for each loop to read the text file line-by-line.

Any hints?

Thanks in advance,
M1234ike

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

outputFile.Write(str)

Oops,

Below are the declarations of the input and output files for the application.
'Here is what I am working with. Just so you know;)
Dim fso, inputFile, outputFile
Dim str As String

fso = CreateObject("Scripting.FileSystemObject")

'The previous post shows what goes below

Any hints?

Thanks in advance,
M1234ike

In resumee, you can do some thing like this untested code:

Imports System.IO
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, "USER: " & TextBox2.Text.Trim & vbNewLine & vbNewLine, True)

My.Computer.FileSystem.WriteAllText(path, "______________________________________" & vbNewLine & vbNewLine, True)
My.Computer.FileSystem.WriteAllText(path, "COMPUTER: " & name & vbNewLine, True)
' 
' Here put the wireless if any
'
Dim wirelessStrings() as String = GetWireless()
If Not wirelessStrings Is Nothing then
   ForEach s as String in wirelessStrings
      My.Computer.FileSystem.WriteAllText(path1,s & vbNewLine, True)
   Next 
End If 
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, "USER: " & TextBox2.Text.Trim & vbNewLine & vbNewLine, True)

My.Computer.FileSystem.WriteAllText(path1, "______________________________________" & vbNewLine & vbNewLine, True)
My.Computer.FileSystem.WriteAllText(path1, "COMPUTER: " & name1 & vbNewLine, True)
' 
' Here put the wireless if any
'
Dim wirelessStrings() as String = GetWireless()
If Not wirelessStrings Is Nothing then
   ForEach s as String in wirelessStrings
      My.Computer.FileSystem.WriteAllText(path1,s & vbNewLine, True)
   Next 
End If 

My.Computer.FileSystem.WriteAllText(path1, "______________________________________" & vbNewLine & vbNewLine, True)


End If
End If

TextBox1.Enabled = True

Button2.Select()


End Sub

Private Function GetWireless() as string()

Dim ReturnInfo() as string = nothing
Shell("cmd.exe /c getmac /v > C:\Temp\MacInfo.txt", AppWinStyle.MaximizedFocus, True, -1)

Dim tempFile as system.Io.Fileinfo("C:\Temp\MacInfo.txt")
If tempFile.Exists then
  Dim txtFile as TextReader = tempfile.OpenText()
  Dim lineRead as string = txtfile.ReadLine()
  Do While Not lineRead is nothing
    If lineRead.ToLower().Contains("wireless") then
       If returnInfo is nothing then
          Redim returnInfo(0)
       Else
          Redim preserve returnInfo(returnInfo.Length)
       End If
       returnInfo(returnInfo.Length-1)=lineRead
     End If
     lineread = txtFile.ReadLine()
  Loop
  Return returnInfo
End if
Return returnInfo
End Function


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

Hope this helps

THANX Lolafuertes,

It now works like a charm;)

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.