WMIC返回无实例直接运行.cmd文件时可用

时间:2019-06-21 09:22:27

标签: batch-file cmd wmi wmic

我正在尝试在for循环中查询WMIC。当我在cmd.exe中运行查询命令时

WMIC PATH Win32_DiskPartition WHERE (DeviceID="Disk #0, Partition #1") GET Type /value

结果很好。但是我运行query.cmd,它显示没有可用的实例。

https://abload.de/img/ooo6vkr6.png

@ECHO OFF
SET "InputFile=C:\Users\NIMDA\AppData\Local\Temp\aa.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /F "delims=" %%G IN ('"WMIC PATH Win32_DiskPartition WHERE (DeviceID="Disk #0, Partition #1") GET Type /value"') DO (
    FOR /F "tokens=*" %%H IN ("%%G") DO (
        CALL ECHO %%H>> "%InputFile%"
    )
)
ENDLOCAL
pause

如何解决此问题?。

1 个答案:

答案 0 :(得分:1)

这里有几个例子,以不同的方式来做!

使用Find

@Echo Off
Set "InputFile=%TEMP%\aa.txt"
For /F Tokens^=4* %%A In ('
    "WMIC Partition Where "DeviceID Like 'Disk #0%%'" Get DeviceID,Type 2>NUL|Find "Partition #1""
')Do @For /F Tokens^=* %%A In ("%%B")Do >>"%InputFile%" Echo Type=%%A

使用Like

@Echo Off
Set "InputFile=%TEMP%\aa.txt"
For /F Tokens^=* %%A In ('
    "WMIC Partition Where "DeviceID Like 'Disk #0%%' And DeviceID Like '%%Partition #1'" Get Type /Value 2>NUL"
')Do @For /F Tokens^=* %%B In ("%%A")Do >>"%InputFile%" Echo %%B