你好,我第一次尝试使用批处理脚本,正在按以下方式获取硬盘驱动器的大小:
wmic diskdrive get size
这个命令很有效,但我想将这个值存储到一个变量中以便以后使用,比如使用ECHO显示该值。
我不确定如何将上述命令的输出设置为一个变量。我尝试了:
SET hddbytes=wmic diskdrive get size
但这只是将变量设置为上面的文本字符串,而不是输出。
你好,我第一次尝试使用批处理脚本,正在按以下方式获取硬盘驱动器的大小:
wmic diskdrive get size
这个命令很有效,但我想将这个值存储到一个变量中以便以后使用,比如使用ECHO显示该值。
我不确定如何将上述命令的输出设置为一个变量。我尝试了:
SET hddbytes=wmic diskdrive get size
但这只是将变量设置为上面的文本字符串,而不是输出。
用于批处理文件。从命令行中,将 %% 替换为 %
for /f "tokens=*" %%f in ('wmic diskdrive get size /value ^| find "="') do set "%%f"
echo %size%
或者,如果您想使用您喜欢的变量
for /f "tokens=2 delims==" %%f in ('wmic diskdrive get size /value ^| find "="') do set "myVar=%%f"
echo %myVar%
set
。 - npocmaka"delims="
和 set %%f
,但这只是我在使用 wmic 时喜欢设置值的方式。 - npocmakadelims=
改为tokens=*
,你就会得到相同的解决方案set "%%f"
。 - MC NDwmic
在您的安装中是否可用?路径有问题吗? - MC NDfor /f %%a in ('wmic diskdrive get size^|findstr [0-9]') do echo %%a
更新:
WMIC's output could get a trailing Carriage Return in some environment:
Size <CR>
<CR><LF>
256052966400<CR>
<CR><LF>
500105249280<CR>
<CR><LF>
15496427520 <CR>
<CR><LF>
use csv format, and use FOR loop to truncate the wanted value from the wmic output:
For /F "tokens=2,3 delims=," %%a in ('"wmic diskdrive get size,Status
/format:csv |findstr "[0-9]" "') do (
echo "%%a"
)
另一个批处理脚本示例:
setlocal ENABLEDELAYEDEXPANSION
:: You can change active code page as well::
@chcp 936 >NUL
:: [example] Remove all network printers:
for /F "tokens=2,3 delims=," %%a in ('"wmic printer where 'local=FALSE' get Name,PrinterStatus /format:csv |findstr "." "') do (
echo "%%a"
rundll32 printui.dll,PrintUIEntry /n "%%a" /dn /q
)
for /f "delims=" %%w in ('wmic diskdrive get size /format:Textvaluelist.xsl') do for /f "usebackq delims=" %%a in ('%%w') do set %%a
echo %size%