#Batch |将Programm ID设置为Variable并删除它

时间:2018-05-15 12:14:42

标签: batch-file windows-installer msiexec

我希望有人可以帮助我: - )

我需要删除OpenOffice,我发现如何获取ID并将其写入变量,然后通过mcshes删除。但是我无法在变量中写出正确的值。

这是我的代码:

@echo on & setlocal EnableExtensions EnableDelayedExpansion

FOR /F "tokens=*"  %%i IN ('wmic product where "name like 'OpenOffice%%'" get IdentifyingNumber /format:value') Do Set id=%%~i

    echo Die ID ist %id% 

    pause

    msiexec /uninstall !id!  /quiet /norestart

    echo OpenOffice Installation Errorlevel %Errorlevel%

    pause

1 个答案:

答案 0 :(得分:1)

您的for /f有两个问题:

  1. 您不能将命令括在'中,也可以在命令中使用它们。 (解决方案usebackq)
  2. wmic输出因结尾CR的行中的额外CRCRLF而瘫痪 (解决方案,使用另一个处理输出,或解析消除行结束的内容)
  3. :: Q:\Test\2018\05\15\SO_50349950.cmd
    @Echo off&SetLocal EnableExtensions EnableDelayedExpansion
    
    for /f "usebackq tokens=2 delims={}" %%A in (
      `wmic product where "name like 'OpenOffice%%'" get IdentifyingNumber /format^:value 2^>NUL`
    ) Do Set "id={%%~A}"
    
    echo Die ID ist %id% 
    pause
    
    msiexec /uninstall !id!  /quiet /norestart
    echo OpenOffice Installation Errorlevel %Errorlevel%
    pause
    

    示例输出(搜索此处存在的LibreOffice)

      

    Q:\测试\ 2018 \ 05 \ 15 \ SO_50349950.cmd
      Die ID ist {DD7E9D37-CA78-459A-8BA8-29BBF29CF257}
      DrückenSieeine beliebige Taste。 。