简体   繁体   English

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

[英]How to install 32-bit or 64-bit executable if executable does not already exist?

I've tried two variations of scripts to install an executable file after checking for the correct processor type.在检查了正确的处理器类型后,我尝试了两种脚本变体来安装可执行文件。 I believe the executable runs, but for some reason its failing to check if the file already exists.我相信可执行文件会运行,但由于某种原因,它无法检查文件是否已经存在。 I will post both here.我会在这里发布两者。

Can some one please help?有人可以帮忙吗?

@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

Or this one或者这个

@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

I suggest to read the Microsoft documentation pages我建议阅读 Microsoft 文档页面

A batch file can be executed on 64-bit Windows by cmd.exe in directory批处理文件可以在 64 位 Windows 上通过目录中的cmd.exe执行

  • %SystemRoot%\\System32 (x64) or %SystemRoot%\\System32 (x64) 或
  • %SystemRoot%\\SysWOW64 (x86) %SystemRoot%\\SysWOW64 (x86)

Which cmd.exe is used depends on the architecture of the application calling the batch file.使用哪个cmd.exe取决于调用批处理文件的应用程序的体系结构。 A 32-bit installer executable running a batch file results in getting the batch file interpreted by 32-bit Windows command processor and so the batch file runs in 32-bit environment like on 32-bit Windows even on being executed on 64-bit Windows.运行批处理文件的 32 位安装程序可执行文件导致批处理文件由 32 位 Windows 命令处理器解释,因此批处理文件在 32 位环境中运行,就像在 32 位 Windows 上一样,即使在 64 位 Windows 上执行.

For that reason a batch file used to install either a 32- or a 64-bit application needs to always find out first in which environment it is executed by which operating system.出于这个原因,用于安装 32 位或 64 位应用程序的批处理文件需要始终首先找出它由哪个操作系统在哪个环境中执行。

Further it does not matter which architecture the CPU of the PC has.此外,PC 的 CPU 具有哪种架构并不重要。 It can be an x64 processor, but installed is nevertheless 32-bit Windows.它可以是 x64 处理器,但安装的是 32 位 Windows。 In this case it is not possible to use 64-bit applications, although the CPU would support them because of installed Windows does not support them.在这种情况下,不可能使用 64 位应用程序,尽管 CPU 会支持它们,因为安装的 Windows 不支持它们。

There are some other facts which must be taken into account:还有一些其他事实必须考虑:

  1. Are there registry keys created during the installation which are affected by WOW64?安装过程中是否创建了受 WOW64 影响的注册表项?
    In this case it would be better on batch file being currently executed in 32-bit environment on 64-bit Windows to be started again in 64-bit environment before doing the install.在这种情况下,最好在 64 位 Windows 上的 32 位环境中当前正在执行的批处理文件在执行安装之前在 64 位环境中再次启动。

  2. Is the batch file executed by an installer application which continues immediately on cmd.exe finished executing the batch file?是否由安装程序执行的批处理文件立即在cmd.exe上继续执行完成批处理文件?
    In this case it is necessary that the batch file execution by 32-bit cmd.exe is halted until 64-bit cmd.exe finished execution of the batch file in 64-bit environment on 64-bit Windows and then exits without doing anything in 32-bit environment.在这种情况下,有必要暂停 32 位cmd.exe的批处理文件执行,直到 64 位cmd.exe在 64 位 Windows 上的 64 位环境中完成批处理文件的执行,然后退出而不做任何事情32位环境。

I suggest to use this batch file for your task:我建议将此批处理文件用于您的任务:

@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

Note: I added a FOR loop to run either nam-creds-provider-windows-x86-2.0.4.exe or nam-creds-provider-windows-x64-2.0.4.exe or any other nam-creds-provider-windows-x*-*.exe in case of version 2.0.4 of the executable is ever replaced by a newer version.注意:我添加了一个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 的情况下会被更新的版本替换。

The batch file works even with disabled command extensions.批处理文件即使在禁用命令扩展的情况下也能工作。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

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

PS: Run %SystemRoot%\\System32\\cmd.exe for example with a double click on this file and run set pro . PS:运行%SystemRoot%\\System32\\cmd.exe例如双击这个文件并运行set pro Let the 64-bit command prompt window open and run next from Windows Explorer %SystemRoot%\\SysWOW64\\cmd.exe also with a double click on this file and run set pro in 32-bit command prompt window.打开 64 位命令提示符窗口,然后从 Windows 资源管理器%SystemRoot%\\SysWOW64\\cmd.exe同时双击该文件,然后在 32 位命令提示符窗口中运行set pro Compare the output environment variables in both command prompt windows.比较两个命令提示符窗口中的输出环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM