.NET Framework Bookmark and Share   
 index > Regular Expressions > Extract Printer Names from Text file (net view)
 

Extract Printer Names from Text file (net view)

Hi,

I am trying to isolate printer names within a text file created via "Net View \\Print-Server > (path).printer.txt
I found a post on regular expressions, which seems like the way to go with this, however, I cant isolate the info I want. her is what i have written (mostly coppied and altered):

   Dim tr As TextReader
   tr = File.OpenText("(path)\printers.txt")
   Dim test As String = tr.ReadToEnd()
   Dim mx As Match
   Dim pattern As String = Chr(13) & "*" & "Print"

   For Each mx In Regex.Matches(test, pattern, RegexOptions.Multiline)
     Console.WriteLine("*", mx.Value, mx.Groups("*").Value)
	    'here I've tried "Share Name" as a group too to no avail

     TextBox1.AppendText(mx.Groups("Share Name").Value)
     TextBox1.AppendText(vbCrLf)

   Next


I get a long list of "Print" when I run this, I'm trying to generate the text (printer name) prior to the "Print" ideally with all spaces between the "printer name" and the "Print" removed. Below is an example of the text output:

I don't know if it matters much to say but some of the printer names have spaces in them so this cant simply remove all spaces on a line prior to the "Print"

Additionally,each printer name is unique (I suppose that is obvious :) and there isn't even a real naming convention in place so i can't imagine how to create a pattern for the computer name except that it occurs after a 'chr(13)' and before a "Print"
Shared resources at \\Print-ServerName



Share name        Type  Used as Comment            

-------------------------------------------------------------------------------
PrinterName1      Print     PrinterName1        
PrinterName2      Print     PrinterName2         
PrinterName3      Print     PrinterName3       
PrinterName4      Print     PrinterName4        
PrinterName5     Print     PrinterName5       
PrinterName6     Print     PrinterName6
Any help would be greatly appreciated
Dr.Mig
Dim tr As TextReader
tr = File.OpenText("\path\printers.txt")
Dim test As String = tr.ReadToEnd()

Dim mx As Match
Dim mx2 As Match

Dim Filter1 As String
Dim filter2 As String
Dim pattern As String = "^[\w |\w-]+\s+Print"
Dim pattern2 As String = "^\w+\s?-?\w+\s?-?\w+"

For Each mx In Regex.Matches(test, pattern, RegexOptions.Multiline)
If Filter1 = Nothing Then
Filter1 = mx.Value
 GoTo 0
Else
Filter1 = Filter1 & vbCrLf & mx.Value
End If
 0: Next

For Each mx2 In Regex.Matches(Filter1, pattern2, RegexOptions.Multiline)
If filter2 = Nothing Then
filter2 = mx2.Value
 GoTo 1
Else
filter2 = filter2 & vbCrLf & mx2.Value
End If
 1: Next

TextBox1.Text = filter2
I figured it out...thanks for pointing me in the right direction Eping Wang. If you have any suggestions as to how I could accomplish this without going through two matching processes I'd love to hear your thoughts but if not, this works fine.
  • Marked As Answer byDr.Mig 9 hours 2 minutes ago
  •  
Dr.Mig

Since you use multiline mode, try this

Dim pattern As String = "^(.+?)\s+Print\s+"


and the server name is in the first captured group.
except youi have a server name as "My Print Machine"
then you should consider what's the character(tab or many spaces) before "Print"

www.wonderstudio.cn
Eping Wang
wow...thanks for the help. I altered your line to:

Dim pattern As String = "^(.+?)\s+"

which removes the "Print".

I found a tutorial I'm going through in my spare time but in the mean time perhaps this is an easy question...
how would Iremove the multiple spaces between the printer name and the "Print" so that all I have left is the printer name?
Dr.Mig
ok I've wokred on this most of today and tried to comprehend a tutorial (I feel pretty confused though)

here's what I have so far:

Dim tr As TextReader
tr = File.OpenText("\path\printers.txt")
Dim test As String = tr.ReadToEnd()


Dim mx As Match Dim mx2 As Match
Dim Filter1 As String Dim filter2 As String
Dim pattern As String = "^[\w |\w-]+\s+Print" Dim pattern2 As String = "^[\w |\w-]+" ' +"
For Each mx In Regex.Matches(test, pattern, RegexOptions.Multiline) If Filter1 = Nothing Then Filter1 = mx.Value GoTo 0 Else Filter1 = Filter1 & vbCrLf & mx.Value End If 0: Next
For Each mx2 In Regex.Matches(Filter1, pattern2, RegexOptions.Multiline) If filter2 = Nothing Then filter2 = mx2.Value GoTo 1 Else filter2 = filter2 & vbCrLf & mx2.Value End If 1: Next
TextBox1.Text = filter2

The first mx limits the output to:
PrinterName1 Print PrinterName1
PrinterName2 Print PrinterName2
PrinterName3 Print PrinterName3

mx2 limits the output to:
PrinterName1 (with blank spaces)
PrinterName2 (with blank spaces)
PrinterName3 (with blank spaces)

I haven't been able to eliminate the blank spaces after the printer names.
Also, I can't simply eliminate all white space because some of the printer names have spaces in them ex: "HP 023" etc...

Is there a more efficient way of accomplishing what I've done so far? How can I eliminate the white spaces at the end of the string?

thanks!

Dr.Mig
Dim tr As TextReader
tr = File.OpenText("\path\printers.txt")
Dim test As String = tr.ReadToEnd()

Dim mx As Match
Dim mx2 As Match

Dim Filter1 As String
Dim filter2 As String
Dim pattern As String = "^[\w |\w-]+\s+Print"
Dim pattern2 As String = "^\w+\s?-?\w+\s?-?\w+"

For Each mx In Regex.Matches(test, pattern, RegexOptions.Multiline)
If Filter1 = Nothing Then
Filter1 = mx.Value
 GoTo 0
Else
Filter1 = Filter1 & vbCrLf & mx.Value
End If
 0: Next

For Each mx2 In Regex.Matches(Filter1, pattern2, RegexOptions.Multiline)
If filter2 = Nothing Then
filter2 = mx2.Value
 GoTo 1
Else
filter2 = filter2 & vbCrLf & mx2.Value
End If
 1: Next

TextBox1.Text = filter2
I figured it out...thanks for pointing me in the right direction Eping Wang. If you have any suggestions as to how I could accomplish this without going through two matching processes I'd love to hear your thoughts but if not, this works fine.
  • Marked As Answer byDr.Mig 9 hours 2 minutes ago
  •  
Dr.Mig

You can use google to search for other answers

Custom Search

More Threads

• Match all <script> HTML blocks except ones w/ a src= attribute?
• PostScript File With Regex
• what is wrong with my regex for .QIF string
• Return *only* unique matches?
• Regex.Replace problem
• Regular Expression to check filter
• Replacing national characters in a string
• Microsoft "official" regex for string version of hierarchyid
• Regexp question: add additional attribute to xml elements with same value of another attribute
• UK Currency