Get command prompt out on one line

三世轮回 提交于 2019-12-11 19:44:41

问题


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) }"


来源:https://stackoverflow.com/questions/56492715/get-command-prompt-out-on-one-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!