如果可执行文件尚不存在,如何安装 32 位或 64 位可执行文件?



在检查正确的处理器类型后,我尝试了两种脚本变体来安装可执行文件。我相信可执行文件会运行,但由于某种原因,它无法检查该文件是否已存在。我将在这里发布两者。

有人可以帮忙吗?

@echo on
if /i "%processor_architecture%"=="x86" (
if exist "C:Program FilesCredential WizardCredentialWizard.exe" (
echo ***App is Installed Successfully***
) else (\srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepointnam-creds-provider-windows-x86-2.0.4.exe -q)
) else if /i "%processor_architecture%"=="X64" (
if exist "C:Program Files (x86)Credential WizardCredentialWizard.exe" (
echo ***App is Installed Successfully***
) else (\srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepointnam-creds-provider-windows-x64-2.0.4.exe -q)
)
exit

或者这个

@echo off
Set RegQry=HKLMHardwareDescriptionSystemCentralProcessor
REG.exe Query %RegQry%  | Find /i "x86" 
If %ERRORLEVEL% == 0 (
GOTO X86
) ELSE (
GOTO X64
)
:X86
IF NOT EXIST "C:Program FilesCredential WizardCredentialWizard.exe"(start \srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepointnam-creds-provider-windows-x86-2.0.4.exe -q)
GOTO END
:X64
IF NOT EXIST "C:Program Files (x86)Credential WizardCredentialWizard.exe"(start \srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepointnam-creds-provider-windows-x64-2.0.4.exe -q)
:End
exit

我建议阅读Microsoft文档页面

  • WOW64 实现详细信息
  • 文件系统重定向器
  • 受 WOW64 影响的注册表项

批处理文件可以通过在目录中cmd.exe在 64 位 Windows 上执行

  • %SystemRoot%System32(x64( 或
  • %SystemRoot%SysWOW64(x86(

使用哪种cmd.exe取决于调用批处理文件的应用程序的体系结构。运行批处理文件的 32 位安装程序可执行文件会导致批处理文件由 32 位 Windows 命令处理器解释,因此批处理文件在 32 位环境中运行,就像在 32 位 Windows 上一样,即使在 64 位 Windows 上执行也是如此。

因此,用于安装 32 位或 64 位应用程序的批处理文件需要始终首先找出它由哪个操作系统在哪个环境中执行。

此外,PC的CPU具有哪种架构并不重要。它可以是 x64 处理器,但安装的仍然是 32 位 Windows。在这种情况下,无法使用 64 位应用程序,尽管 CPU 会支持它们,因为已安装的 Windows 不支持它们。

还有其他一些事实必须考虑:

  1. 安装过程中是否创建了受 WOW64 影响的注册表项?
    在这种情况下,最好在 64 位 Windows 上的 32 位环境中执行当前执行的批处理文件在进行安装之前在 64 位环境中再次启动。

  2. 批处理文件是否由安装程序应用程序执行,该应用程序在完成执行批处理文件cmd.exe立即继续执行?
    在这种情况下,必须停止 32 位cmd.exe的批处理文件执行,直到 64 位cmd.exe在 64 位 Windows 上的 64 位环境中完成批处理文件的执行,然后在 32 位环境中退出而不执行任何操作。

我建议将此批处理文件用于您的任务:

@echo off
rem Is the batch file executed by 32-bit cmd.exe on 32-bit Windows?
if "%ProgramFiles(x86)%" == "" goto DoInstall
rem Is the batch file executed by 64-bit cmd.exe on 64-bit Windows?
if not exist "%SystemRoot%Sysnativecmd.exe" goto DoInstall
rem Run this batch file by 64-bit instead of 32-bit cmd.exe on 64-bit Windows.
rem This simple method works only if batch file is executed without arguments.
"%SystemRoot%Sysnativecmd.exe" /C "%~f0"
rem Exit batch file executed by 32-bit cmd.exe on 64-bit Windows
rem after 64-bit cmd.exe finished execution of the batch file.
goto EndBatch
:DoInstall
rem echo Processor architecture:  %PROCESSOR_ARCHITECTURE%
rem echo Program files directory: %ProgramFiles%
rem echo Common program files:    %CommonProgramFiles%
if exist "%ProgramFiles%Credential WizardCredentialWizard.exe" goto Installed
if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%Credential WizardCredentialWizard.exe" goto Installed
rem When \srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepoint
rem contains always just one installer executable for 32-bit and one for
rem for 64-bit, let the batch file use that one independent on its version
rem number in file name.
for %%I in ("\srvfs01.flymyrtlebeach.comdeployment$SoftwareNervepointnam-creds-provider-windows-%PROCESSOR_ARCHITECTURE%-*.exe") do (
copy /V "%%I" "%TEMP%%%~nxI"
"%TEMP%%%~nxI" -q
del "%TEMP%%%~nxI"
goto ReCheck
)
:ReCheck
if exist "%ProgramFiles%Credential WizardCredentialWizard.exe" goto Installed
if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%Credential WizardCredentialWizard.exe" goto Installed
echo === ERROR: App installation failed. ===
echo/
pause
goto EndBatch
:Installed
echo *** App is installed successfully. ***
:EndBatch

注意:我添加了一个FOR循环来运行nam-creds-provider-windows-x86-2.0.4.exenam-creds-provider-windows-x64-2.0.4.exe或任何其他nam-creds-provider-windows-x*-*.exe以防可执行文件的 2.0.4 版被更新的版本替换。

即使禁用了命令扩展,批处理文件也可以工作。

要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • cmd /?
  • echo /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?

PS:例如,双击此文件运行%SystemRoot%System32cmd.exe并运行set pro。让 64 位命令提示符窗口打开并从 Windows 资源管理器运行下一步,%SystemRoot%SysWOW64cmd.exe双击此文件并在 32 位命令提示符窗口中运行set pro。比较两个命令提示符窗口中的输出环境变量。

相关内容

最新更新