Parsing Command Prompt Output

I am retrieving disk information via JavaScript with the wmic statement, wmic logicaldisk get freespace,name,size,volumename, which produces the following output, but in a single string.

"FreeSpace Name Size VolumeName "
"560232755200 C: 999526756352 System "
"999369699328 D: 999558213632 SSD "
"1511570386944 E: 8001545039872 Get "
"4620751712256 F: 8001545039872 BR "
"788449492992 G: 4000650883072 Seen "
"2296009408512 H: 4000768323584 Seen 2 "
"3594248679424 I: 8001545039872 2160 "
"3507750227968 J: 8001545039872 1080 "
"945300619264 K: 999625322496 Trailers "

I parse the above output with the following JavaScript.

window.onload = function(){ const cp = require('child_process') let drives cp.exec('wmic logicaldisk get freespace,name,size,volumename', (error, stdout)=>{ drives = stdout.trim().split('\r\r\n') .map(value => value.trim().split(/\s{2,0}/)) .slice(1) })
}

That JavaScript already produces my desired output. It produces an array of nested arrays. Each row of information in the command prompt output corresponds to a nested array. Each nested array has four values that correspond to the originally queried data points.

[ ["560232439808", "C:", "999526756352", "System" ] ["999369699328", "D:", "999558213632", "SSD" ] ["1511570386944", "E:", "8001545039872", "Get" ] ["4620751712256", "F:", "8001545039872", "BR" ] ["788449492992", "G:", "4000650883072", "Seen" ] ["2296009408512", "H:", "4000768323584", "Seen 2" ] ["3594248679424", "I:", "8001545039872", "2160" ] ["3507750227968", "J:", "8001545039872", "1080" ] ["945300619264", "K:", "999625322496", "Trailer" ]
]

I am wondering whether or not there is a way to similarly parse the information, but with command prompt instead?

2 Answers

I believe its in part because of your regex you have

 .map(value => value.trim().split(/\s+/))

the /\s+/ is grabbing every all white space in the output from what I tested

I wrote up a quick regex string that may help though it is a bit messy and likely wont catch everything. I tested it with the output your provided though and it did the job. So its worth a shot.

/(\d)*(\s)*(\w:)(\s)*(\d)*(\s)*(\w(\s)?)*/gim
3

The issue from wmic output is just the carriage return (CR) character. It's possible to workaround that in cmd but it's a lot messier because of unfixable legacy issues and the lack of tools. That's why instead of fixing that MS just write a new shell named PowerShell from scratch. Please just use PowerShell for new code and avoid cmd. Try the below commands and you'll get a nicely formatted list immediately. You can then do any manipulation on the list easily

Get-WmiObject -Class Win32_LogicalDisk | Select-Object Name, FreeSpace, Size, VolumeName
Get-WmiObject -Class Win32_LogicalDisk
Get-WmiObject -Class Win32_LogicalDisk | Format-List -Property Name, FreeSpace, Size, VolumeName
Get-WmiObject -Class Win32_LogicalDisk | Format-List -Property *

In case you really want to do in cmd then here's the code

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Get CR, LF and TAB characters
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
(set LF=^
%=EMPTY=%
)
for /f "delims= " %%t in ('forFiles /p "." /m "%~nx0" /c "cmd /c echo(0x09"') do set "TAB=%%t"
rem Parse the command
for /f "tokens=* delims=" %%a in ('wmic logicaldisk get freespace^,name^,size^,volumename') do ( set "line=%%a" rem Remove the CR character from each line for /f "delims=" %%c in ("!CR!") do set "line=!line:%%c=!" rem Parse each line for /f "tokens=1-3,* delims= " %%f in ("!line!") do ( echo %%f !TAB! %%g !TAB! %%h !TAB! %%i )
)

Volume name can contain spaces so you must use * for the last token to capture everything. Sample output:

C:\>wmic logicaldisk get freespace,name,size,volumename & parse_wmic.bat
FreeSpace Name Size VolumeName
87090261312 C: 248695820288 Windows 10
189214661732 E: 637248991232 Data F:
FreeSpace Name Size VolumeName
87090261312 C: 248695820288 Windows 10
189214661732 E: 637248991232 Data
F:

As you can see many drives (like CD/DVD drives) don't have a size or name so the parsing will break. You'll need more code to fix that if you use cmd. It'll also break if you need any columns that can contain spaces after VolumeName. In that case it's extremely hard to tokenize

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like