2016-06-30 61 views
2

因此,我编写了一个批处理文件将客户端转换为云服务,并且我看到了一些奇怪的行为。从SFX运行批处理文件时的行为不同

所以这基本上寻找一个特定的文件夹,它是否存在它使用GOTO继续前进。当我使用WinRAR将其压缩到SFX并指示它运行批处理文件时,它从不检测文件夹,但是,当我运行批处理文件本身时,它总是检测文件夹,无论它是否存在。我一直试图弄清楚这几天,我只是不明白为什么会发生这种情况。

@ECHO Off 
CD %~dp0 
Goto DisableLocal 


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

如何用GUI或命令行创建SFX文件?你有没有使用SFX选项? – Hackoo

+0

我用了gui。发现它在做什么,但我仍然不知道为什么。当我启动SFX并运行它时,它将%ProgramFiles%视为32位,所以它引用/ Program Files(x86)/,当我运行批处理文件时,它的64位。 – ENorum

回答

0

对于64位Windows环境变量PROGRAMFILES执行32位应用程序是由Windows作为微软在MSDN文章WOW64 Implementation Details解释设置为环境变量PROGRAMFILES(x86)的值

The 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%