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
  • 345
  • 2
  • 5
  • 15
  • 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?](https://superuser.com/help/how-to-ask). 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 Dec 31 '19 at 19:09
  • @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 Dec 31 '19 at 19:11
  • @PimpJuiceIT thats exactly what im asking. glad im not the only one who understands it lol – oldboy Dec 31 '19 at 19:17
  • @PimpJuiceIT i just realized due to somebody elses response on the [reposted question](https://superuser.com/q/1513761/650420) 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 Dec 31 '19 at 19:24
  • @PimpJuiceIT i do have one separate question tho. is there a simple way to remove the column headers from the cmd output? – oldboy Dec 31 '19 at 19:48
  • @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 Dec 31 '19 at 19:56
  • 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. – Vomit IT - Chunky Mess Style Dec 31 '19 at 21:06
  • @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 Jan 01 '20 at 01:06

0 Answers0