批处理文件将wmi输出设置为变量

批处理文件将wmi输出设置为变量

本文介绍了批处理文件将wmi输出设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我第一次使用BATCH脚本,我得到的HDD大小如下:

Hello having my first go with a BATCH script, I'm getting the size of the HDD as follow:

wmic diskdrive get size

哪个可以正常工作,但我想将此值存储到变量中以供以后使用,例如使用ECHO显示该值.

Which works fine but I'd like to store this value into a variable for later use, such as using ECHO to display the value.

我不确定如何将上述命令的输出设置为变量.我去了:

I'm not sure how I set the output of the above command to a variable. I went with:

SET hddbytes=wmic diskdrive get size

但这只是将变量设置为上面的文本字符串,而不是输出.

But this just sets the variable to the above text string and not the output.

推荐答案

用于批处理文件.在命令行中,将%%替换为%

For usage in batch file. From command line, replace %% with %

for /f "tokens=*" %%f in ('wmic diskdrive get size /value ^| find "="') do set "%%f"
echo %size%

或者,如果您想使用自己喜欢的变量

Or, if you want to use you prefered variable

for /f "tokens=2 delims==" %%f in ('wmic diskdrive get size /value ^| find "="') do set "myVar=%%f"
echo %myVar%

这篇关于批处理文件将wmi输出设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:39