从SFX运行时,批处理文件的行为有所不同

时间:2016-06-30 17:40:44

标签: windows batch-file sfx

所以我编写了一个批处理文件来将客户端转换为云服务,并且我看到了一些奇怪的行为。

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

@ECHO Off
CD %~dp0
Goto DisableLocal


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

2 个答案:

答案 0 :(得分:0)

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

显然使用x86 SFX模块创建了 WinRAR SFX存档。也可以使用x64 SFX模块创建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%

答案 1 :(得分:0)

我有同样的问题,我正在尝试这种方式,但不确定是否可以在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
)
相关问题