【问题标题】:batch file to run office build in uninstaller在卸载程序中运行 Office 构建的批处理文件
【发布时间】:2015-04-16 22:43:50
【问题描述】:

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

问题出在第三部分。我得到管理员的权利。我可以找到安装了什么版本的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

因此,您可以发送给我的任何帮助都将是很大的帮助。谢谢。

【问题讨论】:

    标签: batch-file command line


    【解决方案1】:

    问题似乎出在您的if 语句上:

    If %office_version%== office12 goto 2007
    

    == 后面的空格是问题所在。您正在检查%office_version% 输出的字符串是否在字面上等于“office12”,包括空格字符。

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

    如果您的 if 条件都不满足,您可能还应该在 if 语句之后添加一些内容。比如:

    echo Unable to identify Office version
    goto :EOF
    

    【讨论】:

    • 感谢您对字符串的洞察
    猜你喜欢
    • 2019-06-14
    • 2014-01-18
    • 2016-06-21
    • 2014-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多