【问题标题】:Batch file behaves differently when ran from a SFX从 SFX 运行时,批处理文件的行为不同
【发布时间】:2023-03-20 02:59:01
【问题描述】:

所以我编写了一个批处理文件来将客户端转换为云服务,但我发现它出现了一些奇怪的行为。

所以这基本上是寻找一个特定的文件夹,无论它是否存在,它都使用 GOTO 继续前进。当我使用 WinRAR 将其压缩为 SFX 并指示它运行批处理文件时,它永远不会检测到该文件夹​​,但是,当我运行批处理文件本身时,它总是会检测到该文件夹​​,无论它是否存在。这几天我一直在尝试解决这个问题,但我只是不明白为什么会这样。

@ECHO Off
CD %~dp0
Goto DisableLocal


:DisableLocal
 IF EXIST "%ProgramFiles%\Server\" (
   GOTO Server
) ELSE (
GOTO Config
)

【问题讨论】:

  • 如何使用 GUI 或 winrar 的命令行创建 SFX 文件?你是否写了 SFX 选项?
  • 我使用了 gui。发现它在做什么,但我仍然不知道为什么。当我启动 SFX 并运行它时,它会将 %ProgramFiles% 视为 32 位,因此它引用 /Program Files (x86)/,而当我只运行批处理文件时,它是 64 位的。

标签: windows batch-file sfx


【解决方案1】:

对于在 64 位 Windows 上执行的 32 位应用程序,环境变量 ProgramFiles 由 Windows 设置为环境变量 ProgramFiles(x86) 的值,正如 Microsoft 在 MSDN 中解释的那样文章WOW64 Implementation Details

WinRAR SFX 存档显然是使用 x86 SFX 模块创建的。 SFX 存档也可以使用 x64 SFX 模块创建,但是这个 SFX 存档只能在 Windows x64 上执行。

如果在创建存档时使用 x86 SFX 模块,则批处理文件在 32 位环境中使用 32 位 cmd.exe 执行。

所以更好的是调整批处理代码并在 64 位 Windows 上添加对 32 位执行的检测。

@ECHO OFF
CD /D "%~dp0"
GOTO DisableLocal

:DisableLocal
SET "ServerPath=%ProgramFiles%\Server\"
IF EXIST "%ServerPath%" GOTO Server

REM Is batch file processed in 32-bit environment on 64-bit Windows?
REM This is not the case if there is no variable ProgramFiles(x86)
REM because variable ProgramFiles(x86) exists only on 64-bit Windows.
IF "%ProgramFiles(x86)%" == "" GOTO Config

REM On 64-bit Windows 7 and later 64-bit Windows there is the variable
REM ProgramW6432 with folder path of 64-bit program files folder.
IF NOT "%ProgramW6432%" == "" (
    SET "ServerPath=%ProgramW6432%\Server\"
    IF EXIST "%ProgramW6432%\Server\" GOTO Server
)

REM For Windows x64 prior Windows 7 x64 and Windows Server 2008 R2 x64
REM get 64-bit program files folder from 32-bit program files folder
REM with removing the last 6 characters from folder path, i.e. " (x86)".
SET "ServerPath=%ProgramFiles:~0,-6%\Server\"
IF EXIST "%ServerPath%" GOTO Server

:Config
ECHO Need configuration.
GOTO :EOF

:Server
ECHO Server path is: %ServerPath%

【讨论】:

    【解决方案2】:

    我有同样的问题,我正在尝试这种方式,但不确定是否可以在 Windows 32 位上运行。

    @ECHO Off
    IF DEFINED ProgramW6432 (
        SET "ServerPath=%ProgramW6432%\Server\"
    ) ELSE (
        SET "ServerPath=%ProgramFiles%\Server\"
    )
    
    CD %~dp0
    Goto DisableLocal
    
    :DisableLocal
     IF EXIST "%ServerPath%" (
       GOTO Server
    ) ELSE (
    GOTO Config
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2011-10-29
      • 2012-08-07
      相关资源
      最近更新 更多