From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

need to convert output of this command "wmic printer list brief" send on system exec.vi to 2d array of strings

Hello

I would like to get the output of this command <wmic printer list brief>  send to system exec.vi,  in a formatted manner in 2d array of strings

I would like to know the share name of the printer and if its status from the formatted array

 

I tried this

1.png

 standard output is like this

 

vihang_0-1654354223602.png

 

But the array is not what I expect, 

I get something like this

 

vihang_1-1654354323738.png

 

I need something like this

 

vihang_2-1654354516318.png

 

Thanks

 

0 Kudos
Message 1 of 10
(1,073 Views)

It is because you expect the delimiter to be a tab (default for spreadsheet string to array) whereas the output has no delimiter and uses fixed width spacing for each column.

 

santo_13_1-1654355936299.png

 

 

 

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 10
(1,062 Views)

Well, the data is just a string with tons of spaces, no tabs and some /n and /r, etc.

 

I would read the lines as 1D array of strings, then process each using "scan string for tokens ". This has the great feature to (optionally) contract successive delimiters into one. (default delimiters are \s,\t,\r,\n and since empty tokens are not allowed, and any consecutive sequence of such delimiters counts as one. Very useful!**)

 

This is just a very quick draft that might need to be tuned. (Note that it cannot tell the difference between a space in a printer name and a field separator. If the fields are truly fixed with (as santo claims above), simpler solutions are of course possible. I am no expert with these cmd outputs.

 

 

altenbach_0-1654356872023.png

 

 

** If you like that usefulness, consider voting for this old idea. 😄

 

 

0 Kudos
Message 3 of 10
(1,051 Views)

Assuming fixed fields width (until Microsoft changes it!), here's one possible solution:

 

altenbach_0-1654359132711.png

 

(Note that you'll still get two rows with empty strings at the end. Would be easy to filter out, of course.)

 

0 Kudos
Message 4 of 10
(1,041 Views)

@altenbach wrote:

(Note that you'll still get two rows with empty strings at the end. Would be easy to filter out, of course.)

 


One out of a million solutions:

 

altenbach_0-1654359756930.png

 

0 Kudos
Message 5 of 10
(1,033 Views)

Dear Altenbach

 

This solution works only for my laptop where I input different Field sizes, but not on other PC

But a good solution anyway 🙂

 

Thanks

 

abc.png

0 Kudos
Message 6 of 10
(990 Views)

@vihang wrote:

 

This solution works only for my laptop where I input different Field sizes, but not on other PC


I would generally recommend to avoid parsing console output. It feels incredibly brittle and tends to make code less readable due to the magic constants involved. Imagine coming across that code, two years from now, after installing a windows update broke the code, and having to piece together what was supposed to go into these fields, solely by looking at their supposed width and downstream usage.

 

As an alternative to what you are trying to do, I would propose using .NET calls directly. This will also make it easier if you then want to interact with the printers later down the line. For example, try: PrinterSettings.InstalledPrinters https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printersettings.installedprinter...

 

LLindenbauer_1-1654594048237.png

You can even call the wmic from .NET and get proper .NET-Objects. I believe this to be more readable, portable and debuggable. When Microsoft decides to change the API, string parsing will almost certainly fail in very strange ways, whereas library calls give you a chance to catch the error at compile time.

0 Kudos
Message 7 of 10
(984 Views)

Thanks

This will give me the printer name, but I wanted the Printer Status , i.e. if its online / offline and also printer share name , which I am unable to find through dot net properties

 

Thanks

 

0 Kudos
Message 8 of 10
(976 Views)

@vihang wrote:

Thanks

This will give me the printer name, but I wanted the Printer Status , i.e. if its online / offline and also printer share name , which I am unable to find through dot net properties

 

Thanks

 


I see - this touches on the way that windows organizes its print system, which, when diving into, provides an unending source of astonishment and opportunities to learn. I guess that the System.Printing.PrintQueue class has more useful properties:

https://docs.microsoft.com/en-us/dotnet/api/system.printing.printqueue?view=windowsdesktop-6.0

 

Just as an aside: I found that the information that windows gives you about the connected printers are not necessarily connected to an underlying physical reality, to the point that "Printer X is Online" has very little bearing on a printer named "X" actually being present, powered and responding to protocol. This is doubly so for network printers.

0 Kudos
Message 9 of 10
(967 Views)

@vihang wrote:

This solution works only for my laptop where I input different Field sizes, but not on other PC

But a good solution anyway 🙂


That's why I said "Assuming fixed fields width (until Microsoft changes it!)". Maybe if there are some long printer names, the fields get adjusted automatically?

 

You can probably get the field start positions by parsing the header line. (Which seems to only have single words/field). So just inspect the header line to dynamically get all the positions, the parse the rest. Very little extra code needed!

0 Kudos
Message 10 of 10
(949 Views)