批处理文件到 运行 office build in uninstaller
batch file to run office build in uninstaller
我正在编写批处理脚本,但遇到了问题。
首先,它会检查管理员权限。
其次,它会检查系统上安装的 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
所以你能给我的任何帮助都将是一个很大的帮助。谢谢。
问题似乎出在您的 if
语句上:
If %office_version%== office12 goto 2007
==
后的space就是问题所在。您正在检查 %office_version%
输出的字符串是否字面上等于“office12”,包括 space 字符。
If "%office_version%"=="office12" goto :2007
您可能还应该在 if 语句之后添加一些内容,以防满足您的 if 条件的 none。类似于:
echo Unable to identify Office version
goto :EOF
我正在编写批处理脚本,但遇到了问题。
首先,它会检查管理员权限。
其次,它会检查系统上安装的 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
所以你能给我的任何帮助都将是一个很大的帮助。谢谢。
问题似乎出在您的 if
语句上:
If %office_version%== office12 goto 2007
==
后的space就是问题所在。您正在检查 %office_version%
输出的字符串是否字面上等于“office12”,包括 space 字符。
If "%office_version%"=="office12" goto :2007
您可能还应该在 if 语句之后添加一些内容,以防满足您的 if 条件的 none。类似于:
echo Unable to identify Office version
goto :EOF