我被困住了,请你帮忙:
我正在尝试测试批处理文件中是否存在特定的应用程序和版本,因此我可以卸载非当前版本。
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set currver=7.1.10.21187
set appname=Juniper Installer Service
set junver=1
echo %currver%
echo %appname%
FOR /F "tokens=2 delims==" %%i in ('wmic product where "name='%appname%'" get version /VALUE ^| find "Version="') do (echo version: %%i
set junver=%%i
echo Juniper version detected: !junver!
if !junver! NEQ %currver% (echo non-current version found...
echo ...uninstalling %appname%
wmic product where ^( name='%appname%' and version='!junver!' ^) call uninstall
echo !errorlevel!
echo %appname% uninstalled))
echo.
echo %junver%
除了第二个包含代码的wmic命令外,所有代码都有效:和version ='!junver!' 我试过添加双克拉来逃避惊呼标记无济于事,同样使用%% i变量也行不通。
我也试过使用不同的where过滤器语法也无济于事。
答案 0 :(得分:1)
编辑以循环处理子程序:
如果您使用这种编码方式,那么您在转义时就不需要额外的谨慎了
我无法测试代码...... call uninstall
对我来说很奇怪。
空格和&
字符可能是一个问题,如果你有它们并确保WMIC命令中的junvar
变量没有尾随CR。
@echo off
set currver=7.1.10.21187
set appname=Juniper Installer Service
set junver=1
echo %currver%
echo %appname%
FOR /F "tokens=2 delims==" %%i in ('wmic product where "name='%appname%'" get version /VALUE ^| find "Version="') do set "junver=%%i" & call :next
echo done
pause
goto :EOF
:next
echo Juniper version detected: "%junver%"
if %junver% EQU %currver% goto :EOF
echo non-current version found...
echo ...uninstalling %appname%
wmic product where (name='%appname%' and version='%junver%') call uninstall.bat
echo %errorlevel%
echo %appname% uninstalled
echo.
echo %junver%
goto :EOF
答案 1 :(得分:1)
我相信这是你的问题:http://www.dostips.com/forum/viewtopic.php?f=3&t=4266
您也可以尝试在代码中进行此更改:
FOR /F "tokens=2 delims==" %%B in ('wmic product where "name='%appname%'" get version /VALUE ^| find "Version="') do @for /f "delims=" %%i in ("%%B") do (
echo version: %%i
set junver=%%i
echo Juniper version detected: --!junver!--
)
答案 2 :(得分:0)
如果您想使用原始脚本(这样您就可以检查计算机上安装的每个实例是否为当前版本并删除非当前版本):
编辑此行:
wmic product where ^( name='%appname%' and version='!junver!' ^) call uninstall
到此:
set juniperuninstall=wmic product where ^( name='%appname%' and version='!junver!' ^) call uninstall
然后在以下后直接添加此行:
cmd /c !juniperuninstall!
这会将wmic行更改为可读格式,并将其读入新的命令实例。 不干净,但有效。