使用批处理文件在文件夹中静默安装 exe 文件

Silent install exe files in a folder with a batch file

我正在尝试制作一个简单的批处理文件,用于在文件夹中找到一个 exe 文件并进行静默安装。到目前为止,我一直没有成功:

for /r "%CD%\folder1\folder2\" %%a in (*.exe /sPB) do start "" "%%~fa"

当然这给了我错误:

Windows cannot find 'E:\folder1\folder2\sPB'. Make sure you typed the name correctly, and then try again.

我知道静默安装的一般命令如下:

mysoftware.exe /sPB

那么我应该把 "silent install flags" 放在哪里?我意识到我不应该把它放在 *.exe.

之后

基于您应该知道安装程序的位置而无需在 folder1\folder2 下进行递归搜索这一事实,我建议:

@Echo Off
CD /D "folder1\folder2" 2>Nul || Exit /B
For %%A In (AcroRdr*.exe) Do Start "" "%%A" /sPB /rs /msi

尽管安装方法应该是相同的:

For /R "folder1\folder2" %%A In (*.exe) Do Start "" "%%A" /sPB

实际上,标志通常放在所有其他参数之后,如下例所示。

SET drive=%CD:~0,3%
%drive%InstallPackages\Notepad++\npp.7.3.1.Installer.x64.exe /S 
%drive%InstallPackages\Windows_SDK\sdksetup.exe /features+ /q /norestart /ceip off
start /wait msiexec /l*v perl-log.txt /I %drive%InstallPackages\strawberry-perl\strawberry-perl-5.24.4.1-64bit.msi TARGETDIR="c:\" PERL_PATH="Yes" /qb
msiexec /i %drive%InstallPackages\Putty\putty-64bit-2017-03-08-installer.msi /q
%drive%InstallPackages\Visual C++12\vcredist_x86.exe /install /quiet /norestart
%drive%InstallPackages\Win-OpenSSL\Win32OpenSSL-1_0_2k.exe /SP- /VERYSILENT /NORESTART /NOICONS

但如果它最后不起作用,我会尝试将它紧跟在可执行文件名称之后。从示例中您会注意到标志(开关)可能会有所不同,具体取决于用于创建安装程序的程序员和实用程序。但是您不必猜测它们是什么。如果您 运行 使用“/?”设置或安装(在 cmd.exe 或从 windows 快捷方式),通常会显示所有命令行选项(开关或标志)。或可执行文件名称后的“/H”或“/help”。大多数 setup/intallers 会显示类似这样的内容。

如您所见,"silent" 或 "quiet" 安装最常用的开关是“/s”和“/q”。我还发现 this site 非常有用。这里有一些其他的提示。

REM  --- How to ---  insert a comment: (start the line with REM)
REM This is a comment
REM  --- How to ---  display message to operator:
echo This is a message to operator
REM  --- How to ---  capture the local drive: 
SET drive=%CD:~0,3%
REM  --- How to ---  Display a variable:
ECHO %drive%
REM  --- How to ---  direct code:
if <statement> (do something) else (do something else)
REM  --- How to ---  insert a label:
:START
REM  --- How to ---  Get user input:
SET /P answer=Are you sure you want to exit?[Y/N]?
REM  --- How to ---  jump to label: (avoid jumping forward)
IF /I "%answer%" EQU N GOTO START
REM  --- How to ---  Check for an existing directory: (must end in "\")
if not exist C:\Strawberry\perl\bin\ (do something)
REM  --- How to ---  use directory names having spaces:
if not exist C:\"Program Files"\Notepad++\ (do something)
REM  --- How to ---  verify a file exists: (you must check for the directory first) 
if not exist C:\"Program Files"\CompanyName\ (
 if not exist C:\"Program Files"\CompanyName\AppName.exe (do something)
)
REM  --- How to ---  suppress overwriting propmts: (Use the /Y flag)
copy %drive%InstallPackages\Tftpd64\*.* /Y C:\"Program Files"\Tftpd64\
REM  --- How to ---  change drive or directory: (in .bat file)
cd /D C:
chdir /D \Apache24\bin
REM  --- How to ---  compare files: 
if fc C:\"Program Files"\CompanyName\AppName.ini %drive%InstallPackages\CompanyName\AppName.ini 
REM  --- How to ---  hide/unhide output from operator:
@Echo off
@Echo on