失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > bat脚本执行exe文件_将批处理(BAT)脚本转换为可执行(EXE)文件

bat脚本执行exe文件_将批处理(BAT)脚本转换为可执行(EXE)文件

时间:2019-07-25 12:39:56

相关推荐

bat脚本执行exe文件_将批处理(BAT)脚本转换为可执行(EXE)文件

bat脚本执行exe文件

When you get down to it, batch files and executable files work pretty much the same way. Both are simply a set of instructions and logic for Windows execute. So why would you want to convert a batch file to an executable if they essentially work the same? Here are a few reasons:

当您了解它时,批处理文件和可执行文件的工作方式几乎相同。 两者都是Windows执行的一组指令和逻辑。 那么,如果批处理文件本质上相同,为什么还要将批处理文件转换为可执行文件呢? 原因如下:

Portability – you can include additional tools in your EXE file that the batch file depends on.可移植性–您可以在批处理文件所依赖的EXE文件中包括其他工具。 Protection – an EXE provides protection for your source script to ensure it is not inadvertently modified.保护-EXE为您的源脚本提供保护,以确保它不会被无意间修改。 Convenience – Executable files can be pinned to the Windows Start Menu and/or Windows 7 Task Bar.便利性–可执行文件可以固定到Windows“开始”菜单和/或Windows 7任务栏。

With the script below, you can easily build your own executable file from a batch file, complete with and required embedded tools.

使用下面的脚本,您可以轻松地从批处理文件构建自己的可执行文件,其中包括必需的嵌入式工具。

组态 (Configuration)

This script takes advantage of a 7-Zip advanced SFX (SelF eXtractor) to bundle and execute the batch file with any included tools. So you will need to download (links provided at the end)these and extract them to a single directory.

该脚本利用7压缩高级SFX(SelF eXtractor)来捆绑和执行包含任何工具的批处理文件。 因此,您需要下载(末尾提供的链接)并将它们解压缩到单个目录中。

Once you have everything downloaded, set the ‘PathTo7Zip’ variable in the script to the location where these files where downloaded.

下载完所有内容后,将脚本中的'PathTo7Zip'变量设置为这些文件的下载位置。

剧本 (The Script)

@ECHO OFFECHO Make EXE From BATECHO Written by: Jason FaulknerECHO ECHO.ECHO.REM Usage:REM MakeExeFromBat BatFileToConvert [IncludeFile1] [IncludeFile2] [...]REMREM Required Parameters:REM BatFileToConvertREMSource batch file to use to produce the output Exe file.REMREM Optional Parameters:REM IncludeFileREMAdditional files to include in the Exe file.REMYou can include external tools used by the batch file so they are available on the executing machine.SETLOCALREM Configuration (no quotes needed):SET PathTo7Zip=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 filesIF EXIST %OutputFile% DEL %OutputFile%REM Build source archiveECHO "%~dpnx1" > %SourceFiles%:AddIncludeIF {%2}=={} GOTO EndIncludeECHO "%~dpnx2" >> %SourceFiles%SHIFT /2GOTO AddInclude:EndInclude"%PathTo7Zip%7za.exe" a %Source7ZFile% @%SourceFiles%REM Build config fileECHO ;!@Install@!UTF-8! > %Config%ECHO RunProgram="%~nx1" >> %Config%ECHO ;!@InstallEnd@! >> %Config%REM Build EXECOPY /B "%PathTo7Zip%7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%REM Clean upIF EXIST %SourceFiles% DEL %SourceFiles%IF EXIST %Config% DEL %Config%IF EXIST %Source7ZFile% DEL %Source7ZFile%ENDLOCAL

结论 (Conclusion)

It is important to note that while the resulting file runs exactly the same as the source BAT file, this is not a true batch to executable conversion. The resulting file is an EXE, however it is intended to be used for self-extracting installers. When you execute the resulting EXE file, the process goes something like this:

重要的是要注意,尽管生成的文件与源BAT文件完全相同地运行,但这不是真正的可执行文件转换批处理。 生成的文件是EXE,但是应将其用于自解压安装程序。 当您执行生成的EXE文件时,该过程将如下所示:

The contents of the EXE file are extracted to the temp directory.EXE文件的内容被提取到temp目录。 The config file generated by the script is read.读取脚本生成的配置文件。 The batch file contained in the EXE file is executed in a new command window.EXE文件中包含的批处理文件在新的命令窗口中执行。 Once finished, the temp files are removed.完成后,将删除临时文件。

On Windows Vista and new OS’s, you may see the following message box after the script is run. After selecting ‘This program installed correctly’, the message box will not be displayed in the future for this file.

在Windows Vista和新操作系统上,运行脚本后,您可能会看到以下消息框。 选择“正确安装此程序”后,此文件以后将不再显示消息框。

Because the EXE file launches in a new window, the typical way of logging output (using the ‘>’ char) will not work as expected. In order to log the output, you would need to handle this natively in your source script.

由于EXE文件在新窗口中启动,因此记录输出(使用'>'char)的典型方式将无法按预期方式工作。 为了记录输出,您需要在源脚本中本地处理此输出。

Despite these minor inconveniences, being able to convert a batch file to an executable can really come in handy.

尽管存在这些微小的麻烦,但是将批处理文件转换为可执行文件确实非常有用。

链接 (Links)

Download Make EXE from BAT Script from Sysadmin Geek

从Sysadmin Geek的BAT脚本中下载Make EXE

Download 7-Zip Command Line Tool

下载7-Zip命令行工具

Download 7-Zip Advanced 7zSD SFX

下载7-Zip Advanced 7zSD SFX

翻译自: /50364/convert-a-batch-bat-script-to-an-executable-exe-file/

bat脚本执行exe文件

如果觉得《bat脚本执行exe文件_将批处理(BAT)脚本转换为可执行(EXE)文件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。