Parsing wmic logicaldisk get Information

1

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?

oldboy

Posted 2019-12-31T09:12:15.833

Reputation: 212

Question was closed 2019-12-31T19:52:00.733

Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. Essentially we are not going to write a batch script from scratch for you. And JavaScript is off topic and should be asked on [so].

– DavidPostill – 2019-12-31T19:09:27.130

@PimpJuiceIT all id like to do is ultimately have output like at the very bottom of my question, but, if a drive volume name has multiple words, i dont want them being split up as multiple array items. in other words, i want to end up with an array of arrays, whereby the nested arrays each correspond to a single drive, and the items in those nested arrays always have 4 values that each correspond to the originally queried info – oldboy – 2019-12-31T19:11:27.753

@PimpJuiceIT thats exactly what im asking. glad im not the only one who understands it lol – oldboy – 2019-12-31T19:17:30.340

@PimpJuiceIT i just realized due to somebody elses response on the reposted question that i can change my regex query to /\s{2,}/ to achieve the exact output i want. but yeah im wondering if theres a way i can do it with wmic or a command line statement instead. also, ive updated the question. look at the very bottom for my desired output.

– oldboy – 2019-12-31T19:24:02.990

@PimpJuiceIT i do have one separate question tho. is there a simple way to remove the column headers from the cmd output? – oldboy – 2019-12-31T19:48:02.547

@PimpJuiceIT ok, interesting. does that logic work in command prompt too? or only powershell? so would the ultimate command be for /f "skip=1 tokens=2-4 delims=: " %f in ('wmic logicaldisk get size^,name^,caption') do wmic logicaldisk get freespace,name,size,volumename? – oldboy – 2019-12-31T19:56:49.000

1

Actually, I think you want to use something like wmic logicaldisk get freespace,name,size,volumename /FORMAT:csv and then the fields are delimited by a comma and you can split on that and not worry about the line feel and carriage return spaces or whatever. Play with that a bit actually. And here's a question David wrote about skipping the WMIC column headers.... https://superuser.com/questions/1192107/wmic-output-property-value-without-property-name. The skip=1 is what I believe skips the first iteration in the loop which will be the column definitions.

– Pimp Juice IT – 2019-12-31T21:06:06.073

@PimpJuiceIT the /format:csv is perfect, although for some reason it spits out an extra column of values, node: [the name of my PC]. thanks. really appreciate your help! i just realized that wmic logicaldisk get size does not account for reserved space. found out diskpart and then list disk lists the true total size, but not sure if i can extract this via JS :( – oldboy – 2020-01-01T01:06:42.333

No answers