解读“从BAT中制作EXE”剧本由Jason Faulker撰写

时间:2015-01-15 10:39:40

标签: batch-file

我遇到了一种将带有工具依赖项的.bat转换为.exe文件的方法。但是,当我尝试使用该脚本并运行.exe创建时,我总是收到错误。似乎我错误地修改了脚本。

有人可以帮忙吗?

以下是我修改过的代码:

@ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

REM Usage:
MakeExeFromBat BatFileToConvert -bat MyProgram.bat
REM
REM Required Parameters:
BatFileToConvert -save MyProgram
REM     Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
IncludeFile -include Tool.exe
REM     Additional files to include in the Exe file.
REM     You can include external tools used by the batch file so they are available on the executing machine.

SETLOCAL

REM Configuration (no quotes needed):
SET PathTo7Zip=C:\Desktop\


REM ---- Do not modify anything below this line ----

SET OutputFile="%~n1.exe"
SET SourceFiles="%TEMP%\MakeEXE_files.txt"
SET Config="%TEMP%\MakeEXE_config.txt"
SET Source7ZFile="%Temp%\MakeEXE.7z"

REM Remove existing files
IF EXIST %OutputFile% DEL %OutputFile%

REM Build source archive
ECHO "%~dpnx1" > %SourceFiles%
:AddInclude
IF {%2}=={} GOTO EndInclude
ECHO "%~dpnx2" >> %SourceFiles%
SHIFT /2
GOTO AddInclude
:EndInclude
"%PathTo7Zip%\7za.exe" a %Source7ZFile% @%SourceFiles%

REM Build config file
ECHO ;!@Install@!UTF-8! > %Config%
ECHO RunProgram="%~nx1" >> %Config%
ECHO ;!@InstallEnd@! >> %Config%

REM Build EXE
COPY /B "%PathTo7Zip%\7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%

REM Clean up
IF EXIST %SourceFiles% DEL %SourceFiles%
IF EXIST %Config% DEL %Config%
IF EXIST %Source7ZFile% DEL %Source7ZFile%

ENDLOCAL

1 个答案:

答案 0 :(得分:0)

这并不能真正将bat文件转换为exe文件。它只是创建一个包含bat文件的自动提取存档(exe)。在执行时,它将文件提取到临时文件夹并从那里运行它。您甚至可以使用7zip / rar / winzip或任何其他归档程序从exe中提取bat。

如果你想将蝙蝠转换成真实的exe,你应该使用网络中的一个工具(比如这个:http://www.f2ko.de/index.php?lang=en)或者使用像AutoIt这样的简单脚本语言来实现。

如果你选择第二个,你只需用Run("put your bat code in here")执行你的蝙蝠代码,你就可以将你的脚本编译成一个“真正的”exe文件。