Get command prompt out on one line

2019-08-29 09:21发布

问题:

I am trying to get Model and Size of all local disks.
I want to output on one line for each disk.
Example: Model Samsung Size 500GB

@echo off
for /f "tokens=2 delims==" %%d in ('wmic diskdrive get Model,Size /value') do (
    set Model=%%d
set Size=%%d
)
echo Model %Model% Size %Size%
pause

But nothing.

回答1:

As model descriptions may contain spaces, you need to format the output as csv, so the output is delimited by commas (I hope there aren't model descriptions that contain commas - I didn't see one so far).

Without /value each disk is listed in one line (Node,Model,Size), so you need tokens=2,3. Add a skip=2 to remove the header line and add your fixed strings to the final output:

for /f "skip=2 tokens=2,3 delims=," %%a in ('"wmic diskdrive get Model,Size /format:csv"') do @echo Model %%a Size %%b


回答2:

Here is one way to do it on all current day, supported Windows machines. The stated goal is "one line." If you want model and size in separate variables, there is more to do.

powershell -NoLogo -NoProfile -Command ^
    "Get-CimInstance -ClassName CIM_DiskDrive |" ^
    "ForEach-Object { 'Model {0} Size {1}' -f @($_.Model, $_.size) }"


标签: cmd wmi wmic