批处理文件在卸载程序中运行office build

时间:2015-04-16 22:43:51

标签: batch-file command line

我正在编写一个我遇到问题的批处理脚本 首先,它检查管理员权限 其次,它会检查系统上安装的Microsoft Office版本 第三,它涉及到不稳定的构建。

问题在于第三部分。我得到了管理权。我可以找到安装了哪个版本的办公室,在%office-version%中进行设置。但我无法在if语句中正确检查%office_version%。这是我的代码:

@echo Off

::----------------------------------------------
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
::------------------------------------------------------------------------------
:: This part of the cod4e found what office is install on the computer 
::-----------------------------------------------------------------------------
setlocal enableDelayedExpansion
for /f "tokens=2 delims==" %%O in ('ftype ^|findstr /r /I "\\OFFICE[0-9]*" 2^>nul') do (
    set "verp=%%~O"
    goto :end_for
)
:end_for

for %%P in (%verp%) do (
    set "off_path=%%~dpP"
    for %%V in ("!off_path:~0,-1!") do (

     set "office_version=%%~nV"
     goto :end_for2
    )
)
:end_for2
echo %office_version%
Pause
::-----------------------------------------------------------------------------
:: That the var of %Offfice_Verson% then  running the  build in remove of that office software.
::-------------------------------------------------------------------------------
        if %office_version%== office12 goto 2007
        If %office_version%== office14 goto 2010
        If %office_version%== office15 goto 2013

            :2007
            Echo Unstailling office 2007
            cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE12\Office Setup Controller"
            Goto remove

            :2010
            Echo Unstailling office 2010
            cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE14\Office Setup Controller"
            Goto remove

            :2013
            Echo Unstailling 2013
            cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE15\Office Setup Controller"
            Goto Remove

            :Remove
                %homedrive%
                SETUP.EXE
                Echo Done!!!!!
                Pause
                Exit

所以你能给我的任何帮助都会有很大的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

问题似乎与您的if声明有关:

If %office_version%== office12 goto 2007

==之后的空格是问题所在。您正在检查%office_version%输出的字符串是否字面上等于" OFFICE12"包括空间角色。

If "%office_version%"=="office12" goto :2007

如果没有满足任何条件,你应该在if语句之后也有一些东西。类似的东西:

echo Unable to identify Office version
goto :EOF