运行7zip安装程序后,用于静默安装7zip的批处理脚本无法继续

时间:2018-07-20 21:52:01

标签: windows batch-file command-line

我正在尝试创建一个脚本,该脚本以静默方式运行多个setup .exe,无论出于何种原因,当我在批处理文件中以静默方式完成7zip的安装时,之后都不会再执行其他命令。

Kinda见识见识。我尝试过暂停,回显以及其他仅回显的子例程(返回后可正常工作并执行命令)。

但是无论出于何种原因,即使我从我安装的子例程返回后,执行7zip.exe / S后,它也只是退出执行命令。

@echo OFF


SET 7ZipName=7z*.exe

SET x86SetupFilePath=C:\PSDARS-Setup\x86\
SET x64SetupFilePath=C:\PSDARS-Setup\x64\

SET x647ZipPath=C:\Program Files\7-Zip\
SET x867ZipPath=C:\Program Files (x86)\7-Zip\

:: 1. Check for OS architecture
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

if %OS%==32BIT GOTO :32BIT

if %OS%==64BIT GOTO :64BIT

:32BIT

:: Check arguments 1 = Installation Directory Path. If not set use base above 
IF NOT %1.==. SET x86SetupFilePath=%~1

::Install 7zip
CALL :Install7Zip 

setx PATH "%PATH%;%x647ZipPath%" /m

GOTO :ProgramEnd


:64BIT

:: Check arguments 1 = Installation Directory Path 
IF NOT %1.==. SET x64SetupFilePath=%~1

::Install 7zip
CALL :Install7Zip 

CALL :Test

Echo 7zip Installation Complete

::setx PATH "%PATH%;%x647ZipPath%" /m

GOTO :ProgramEnd

:Test
Echo a
GOTO :EOF

:: Author: Jacob Howarth
:: Date: 07/20/18
:: Description: This procedure will install the correct version of
:: the 7zip installer silent based on the architecture of the OS
::
:Install7Zip
IF %OS%==32BIT cd "%x86SetupFilePath%"

IF %OS%==64BIT cd "%x64SetupFilePath%"

for %%f in (%7ZipName%) do SET File=%~f     

%File% /S

ECHO.%ERRORLEVEL%

GOTO :EOF
:: End of Install7Zip

echo 7zip Installation Complete

:ErrorInstall7Zip
echo Error installing 7zip for %OS% OS. x64 path = %x64SetupFilePath%, x86 path =%x86SetupFilePath%.

:ProgramEnd

1 个答案:

答案 0 :(得分:1)

iCalUId

重构代码以删除@echo off setlocal :: Check for OS architecture. set "SetupFilePath=%~1" if not defined SetupFilePath ( if defined ProgramFiles(x86^) ( set "SetupFilePath=C:\PSDARS-Setup\x86" ) else ( set "SetupFilePath=C:\PSDARS-Setup\x64" ) ) :: Change working directory. cd /d "%SetupFilePath%" || ( >&2 echo Failed to change directory. exit /b 1 ) :: Get setup file name. set "SevenZipName=7z*.exe" set "File=" for %%F in ("%SevenZipName%") do set "File=%%~F" if not defined File ( >&2 echo File not defined. exit /b 1 ) :: Install 7zip. "%File%" /S if errorlevel 1 ( >&2 echo 7zip errorlevel: %errorlevel% exit /b 1 ) :: Add to PATH environment variable. set "SevenZipPath=%ProgramFiles%\7-Zip" if exist "%SevenZipPath%" ( call :add_path "%SevenZipPath%" "%%ProgramFiles%%\7-Zip" ) echo 7zip Installation Complete exit /b 0 :add_path set "main_key=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" set "data=" :: Query the HKLM Path and get the key name, data type and data. for /f "tokens=1,2,*" %%A in ('reg query "%main_key%" /v Path') do ( if /i "%%~A" == "Path" ( set "key_name=%%~A" set "data_type=%%~B" set "data=%%~C" ) ) if not defined data ( >&2 echo Path data for write to registry is undefined. exit /b 1 ) :: Remove trailing semi-colon. if "%data:~-1%" == ";" set "data=%data:~,-1%" :: Check target is already in Path. for %%A in ("%data:;=" "%") do for %%B in ("%~1" "%~2") do ( if "%%~A" == "%%~B" ( echo Directory "%%~B" is in Path. exit /b 0 ) ) :: Set new_data to a value to suit data type. if /i "%data_type%" == "REG_EXPAND_SZ" ( set "new_data=%~2" ) else set "new_data=%~1" if not defined new_data ( >&2 echo Path new_data to write to registry is undefined. exit /b 1 ) :: Remove trailing backslash. if "%new_data:~-1%" == "\" set "new_data=%data:~,-1%" :: Backup and update Path. if not exist "%~dp0Environment_Backup.reg" ( echo Backing up key to Environment_Backup.reg. reg export "%main_key%" "%~dp0Environment_Backup.reg" ) echo Adding "%new_data%" to Path. >nul reg add "%main_key%" /v "Path" /t "%data_type%" /d "%data%;%new_data%" /f exit /b 0 流。

检查goto的操作系统体系结构。

对于简单变量,命令ProgramFiles(x86)可以使用 setx变量,需要更多代码来检查值 键并仅在需要时添加到现有值。 注意事项,例如reg值类型和可能的变量 该值可能需要检查。 reg导出文件是 在添加更改之前作为备份创建。

使用不以数字开头的用户变量名称。的 原因是诸如PATH之类的名称可能是 插入为%7ZipName%%7ZipName%是第一个有效 扩展是第七个论点。这可能会导致语法 错误。

在x64操作系统上测试。操作系统环境可能需要重新启动才能 更新更改或运行%7并单击 环境变量,然后两次 OK 更新。