【问题标题】:How to install 32-bit or 64-bit executable if executable does not already exist?如果可执行文件不存在,如何安装 32 位或 64 位可执行文件?
【发布时间】:2019-03-31 09:02:26
【问题描述】:

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

有人可以帮忙吗?

@echo on
if /i "%processor_architecture%"=="x86" (
    if exist "C:\Program Files\Credential Wizard\CredentialWizard.exe" (
        echo ***App is Installed Successfully***
    ) else (\\srvfs01.flymyrtlebeach.com\deployment$\Software\Nervepoint\nam-creds-provider-windows-x86-2.0.4.exe -q)
) else if /i "%processor_architecture%"=="X64" (
    if exist "C:\Program Files (x86)\Credential Wizard\CredentialWizard.exe" (
        echo ***App is Installed Successfully***
    ) else (\\srvfs01.flymyrtlebeach.com\deployment$\Software\Nervepoint\nam-creds-provider-windows-x64-2.0.4.exe -q)
)
exit

或者这个

@echo off

Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry%  | Find /i "x86" 
If %ERRORLEVEL% == 0 (
    GOTO X86
) ELSE (
    GOTO X64
)

:X86
IF NOT EXIST "C:\Program Files\Credential Wizard\CredentialWizard.exe"(start \\srvfs01.flymyrtlebeach.com\deployment$\Software\Nervepoint\nam-creds-provider-windows-x86-2.0.4.exe -q)
GOTO END

:X64
IF NOT EXIST "C:\Program Files (x86)\Credential Wizard\CredentialWizard.exe"(start \\srvfs01.flymyrtlebeach.com\deployment$\Software\Nervepoint\nam-creds-provider-windows-x64-2.0.4.exe -q)
:End
exit

【问题讨论】:

  • 你的想法是错误的,在64位操作系统上,64位程序会被安装到%ProgramFiles%,在32位操作系统上,32位程序会被安装到%ProgramFiles%%ProgamFiles(x86)%,应该是 64 位操作系统上的 32 位版本,如果您的系统使用正确的操作系统位软件,您只需要检查 %ProgramFiles%
  • 您是否调试过您的脚本以确定它是否正确 无法查看文件与不正确 是否无法查看文件?换句话说,你是说文件在那里而这段代码看不到它吗?或者您是说该文件实际上不是您的脚本期望找到的位置?

标签: batch-file installation file-exists not-exists


【解决方案1】:

我建议阅读 Microsoft 文档页面

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

  • %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 -bit 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%\Sysnative\cmd.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%\Sysnative\cmd.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 Wizard\CredentialWizard.exe" goto Installed
if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%\Credential Wizard\CredentialWizard.exe" goto Installed

rem When \\srvfs01.flymyrtlebeach.com\deployment$\Software\Nervepoint\
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.com\deployment$\Software\Nervepoint\nam-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 Wizard\CredentialWizard.exe" goto Installed
if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%\Credential Wizard\CredentialWizard.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%\System32\cmd.exe,例如双击此文件并运行set pro。让 64 位命令提示符窗口打开并从 Windows 资源管理器%SystemRoot%\SysWOW64\cmd.exe 运行下一步,也可以双击此文件并在 32 位命令提示符窗口中运行 set pro。比较两个命令提示窗口中的输出环境变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 2010-12-28
    • 1970-01-01
    • 2016-12-10
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    相关资源
    最近更新 更多