Windows – Parsing Command Prompt Output

command lineparsingwindowswindows 10wmic

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?

Best Answer

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