windows 无需额外的外部软件即可将 .bat 转换为 .exe(创建 SFX)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51098378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting .bat to .exe with no additional external software (Create SFX)
提问by Hadi Farah
Following the same steps as this guide. I am trying to convert from bat to exe without installing any new software such as Bat to Exe Converter. The reason I am using this method is because all machines in my workplace already have 7zip installed and can use it, however I am not allowed to make the script work using external software not present on the main server to be compatible on any machine in the company.
遵循与本指南相同的步骤。我正在尝试从 bat 转换为 exe 而不安装任何新软件,例如Bat to Exe Converter。我使用这种方法的原因是因对我来说有效场所的所有机器都已经安装了 7zip 并且可以使用它,但是我不允许使用主服务器上不存在的外部软件使脚本工作,以便在任何机器上兼容公司。
I have the following TEST.bat:
我有以下TEST.bat:
ECHO This is a Test bat to exe
pause
and the config.txt:
和config.txt:
;!@Install@!UTF-8!
RunProgram="TEST.bat"
;!@InstallEnd@!
then I call the following command line (in another .bat):
然后我调用以下命令行(在另一个 .bat 中):
COPY /B "%PathTo7Zip%zCon.sfx" + %Config% + %Source7ZFile% %OutputFile%
%PathTo7Zip%
is the directory to 7zCon.sfx%Config%
is the config.txtfile above
%Source7ZFile%
is my .7z archive
and %OutputFile%
is my output TEST.exefile which should supposedly run the bat file when I call it according to the author of the guide.
However calling TEST.exe
triggers unzipping the .7z archive (which is expected) and then exits without running TEST.bat.
%PathTo7Zip%
是7zCon.sfx的目录%Config%
是上面的config.txt文件
%Source7ZFile%
是我的 .7z 存档,%OutputFile%
是我的输出TEST.exe文件,当我根据指南的作者调用它时,它应该运行 bat 文件。然而,调用TEST.exe
会触发解压缩 .7z 存档(这是预期的),然后退出而不运行TEST.bat。
However the author explains:
然而作者解释说:
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.
- The config file generated by the script is read.
- The batch file contained in the EXE file is executed in a new command window.
- Once finished, the temp files are removed.
- EXE 文件的内容被提取到临时目录。
- 读取脚本生成的配置文件。
- EXE 文件中包含的批处理文件在新的命令窗口中执行。
- 完成后,临时文件将被删除。
回答by Hackoo
Refer to this How can a .bat file be 'converted' to .exe without third party tools?
请参阅此如何在没有第三方工具的情况下将 .bat 文件“转换”为 .exe?
The original script accepts two arguments - the .bat file you want to convert and the target executable.
原始脚本接受两个参数 - 要转换的 .bat 文件和目标可执行文件。
I made a little modification to accept one argument : Just the .bat file you want to convert.
我做了一点修改以接受一个参数:只是您要转换的 .bat 文件。
So in this case, you can drag and drop your batch file over this script bat2exeIEXP.batand it will be converted to exe file with the same name as the batch file.
因此,在这种情况下,您可以将批处理文件拖放到该脚本bat2exeIEXP.bat 上,它将被转换为与批处理文件同名的 exe 文件。
;@echo off
;Title Converting batch scripts to file.exe with iexpress
;Mode 75,3 & color 0A
;Rem Original Script https://github.com/npocmaka/batch.scripts/edit/master/hybrids/iexpress/bat2exeIEXP.bat
;echo(
;if "%~1" equ "" (
;echo Usage : Drag and Drop your batch file over this script:"%~nx0"
;Timeout /T 5 /nobreak>nul & Exit
;)
;set "target.exe=%__cd__%%~n1.exe"
;set "batch_file=%~f1"
;set "bat_name=%~nx1"
;set "bat_dir=%~dp1"
;Set "sed=%temp%exe.sed"
;echo Please wait a while ... Creating "%~n1.exe" ...
;copy /y "%~f0" "%sed%" >nul
;(
;(echo()
;(echo(AppLaunched=cmd /c "%bat_name%")
;(echo(TargetName=%target.exe%)
;(echo(FILE0="%bat_name%")
;(echo([SourceFiles])
;(echo(SourceFiles0=%bat_dir%)
;(echo([SourceFiles0])
;(echo(%%FILE0%%=)
;)>>"%sed%"
;iexpress /n /q /m %sed%
;del /q /f "%sed%"
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=1
UseLongFileName=1
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=
DisplayLicense=
FinishMessage=
FriendlyName=-
PostInstallCmd=<None>
AdminQuietInstCmd=
回答by Zimba
All windows machines with .NET installed come with files called vbc.exe, csc.exe & jsc.exe, at %windir%\Microsoft.NET\Framework (for 32bits) or Framework64 (for 64 bits), in a folder name starting with "v".
所有安装了 .NET 的 Windows 机器都带有名为 vbc.exe、csc.exe 和 jsc.exe 的文件,位于 %windir%\Microsoft.NET\Framework(对于 32 位)或 Framework64(对于 64 位),文件夹名称以用“v”。
Guide on C# compiler:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe
C# 编译器指南:https:
//docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe
Guide on Visual Basic compiler:
https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/index
Sample commands:
https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/sample-compilation-command-lines
Visual Basic 编译器指南:
https: //docs.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/index
示例命令:https:
//docs.microsoft.com/en-我们/dotnet/visual-basic/reference/command-line-compiler/sample-compilation-command-lines
A Jscripting guide:
http://www.phpied.com/make-your-javascript-a-windows-exe/
Jscripting 指南:http://www.phpied.com/make-your-javascript-a-windows-exe/
A simple walkthrough from @Itchy to bundle a batch file to exe file with csc.exe:
How can a .bat file be 'converted' to .exe without third party tools?
从@Itchy 将批处理文件捆绑到带有 csc.exe 的 exe 文件的简单演练:
如何在没有第三方工具的情况下将 .bat 文件“转换”为 .exe?
In the same folder, a file called ngen.exe may also be found, which "generates native code so the application does not need to go through the just-in-time compilation process at runtime".
See:
https://books.google.com.au/books?id=iZM1jyqiyakC&pg=PA453&lpg=PA453&dq=guide++vbc.exe&source=bl&ots=UB041mSfni&sig=ACfU3U0xtmS8X9p0eDKD-u6bt-WvOVCbmQ&hl=en&sa=X&ved=2ahUKEwjshc6589_lAhXXXSsKHcTaBlUQ6AEwD3oECAYQAQ#v=onepage&q=guide%20%20vbc.exe&f=false
在同一个文件夹中,还可以找到一个名为 ngen.exe 的文件,它“生成本机代码,因此应用程序在运行时不需要经过即时编译过程”。
请参阅:
https://books.google.com.au/books?id=iZM1jyqiyakC&pg=PA453&lpg=PA453&dq=guide++vbc.exe&source=bl&ots=UB041mSfni&sig=ACfU3U0xtmS8X9p0eDKD-u6bt-WvOVCbmQ&hl=en&sa=X&ved=2ahUKEwjshc6589_lAhXXXSsKHcTaBlUQ6AEwD3oECAYQAQ#v=onepage&q =guide%20%20vbc.exe&f=false