While using the wmic command with the /format:csv option, it occurred to me that it would be useful to reformat numbers if appropriate and have the CSV fields enclosed in quotes to allow outputting fields containing commas.
Take a copy of the %windir%\system32\wbem\csv.xsl (I've called mine csv2.xsl in this example), and modify the template match for 'VALUE' to the line below:
<xsl:template match="VALUE" xml:space="preserve">"<xsl:choose><xsl:when test="string(number(.))='NaN'"><xsl:value-of select="."/></xsl:when><xsl:when test=". > 1000000000"><xsl:value-of select="string(format-number(.,'###,###,###'))"/></xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>"</xsl:template>
- Check if the value is a number
- If not, output as normal.
- If it is a number, and if the number is greater than 1000000000, reformat with commas as thousand separators.
- If if is a number, and less than 1000000000, output as normal.
Output the results with quotes surrounding the data, useful when the data may contain comma's (as in this case)
I find this useful when I'm querying remote machines for their free/total disk space, when the number comes back as a daunting 227770765312 bytes, which is much easier to interpret when reformatted as 227,770,765,312 (~227GB or ~212 depending on whether you're a 1000 or 1024 kind of person)
A query using this modified xsl transform:
wmic /node:"server-01","server-02","server-03" path Win32_LogicalDisk WHERE "FileSystem='NTFS' AND Name != 'C:' AND Name != 'D:'" GET Name,Size,FreeSpace,VolumeName /format:csv2
Note the double-quotes surrounding the node-names, which is required when a server name contains a hyphen. When specifying more than one node by commas, each node is surrounded by quotes.
You could also actually divide the number by (/1024/1024/1024) to give a GB figure, or any other number of output modifications to the original data.
Note that when redirecting the wmic command to file it will result in a Unicode file, and by default when loading a .csv in Excel it won't split the columns automatically.
To output as UTF-8, you can either:
- Modify the xsl output element to use a different encoding to the default utf-16, such as utf-8 or us-ascii
- Use the 'type' command - eg. type output.csv output8.csv will take a Unicode file and provide as ASCII output
Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.