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.
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.
06-04-2022 09:56 AM
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
standard output is like this
But the array is not what I expect,
I get something like this
I need something like this
Thanks
06-04-2022 10:19 AM
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.
06-04-2022 10:38 AM - edited 06-04-2022 10:46 AM
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.
** If you like that usefulness, consider voting for this old idea. 😄
06-04-2022 11:14 AM - edited 06-09-2022 10:42 AM
Assuming fixed fields width (until Microsoft changes it!), here's one possible solution:
(Note that you'll still get two rows with empty strings at the end. Would be easy to filter out, of course.)
06-04-2022 11:22 AM
@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:
06-06-2022 11:55 PM
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
06-07-2022 04:34 AM
@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...
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.
06-07-2022 09:50 AM
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
06-07-2022 10:45 AM - edited 06-07-2022 10:51 AM
@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.
06-09-2022 10:48 AM
@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!