首页 > 解决方案 > 在批处理文件的for循环中使用wmic命令中的变量

问题描述

我无法让这个查询工作:

set APPNAME="App Name"

for /f "skip=1delims= " %%t in ('wmic product where "name=%APPNAME%" get IdentifyingNumber') do set "guid=%%t"& goto printguid
:printguid
    echo %guid%

我收到“无效的别名动词”错误。我无法弄清楚我错过了什么。

标签: windowsbatch-filecmdwmic

解决方案


备注WMIC:不要忘记WMIC的输出是unicode!

<CR>可以通过将值传递给另一个FOR /F循环来删除尾随。这也删除了幻影“空白”行(实际上是 a <CR>

"APPNAME=VirtualDJ 8"所以我在我这边测试了这批产品,它的工作率为 5/5

@echo off
Title Get IdentifyingNumber from Application using WMIC
set "APPNAME=VirtualDJ 8"
set "GUID="
echo    Please Wait a while ... Getting IdentifyingNumber from this Application "%APPNAME%"
@for /f "skip=1 delims=" %%a in ('"wmic product where name="%APPNAME%" get IdentifyingNumber"') do (
    @for /f "delims=" %%b in ("%%a") do if not defined GUID set "GUID=%%~nb"
)
cls
echo The Application "%AppName%" has a Guid like this one : "%GUID%"
pause

推荐阅读