无需额外的外部软件即可将 .bat 转换为 .exe(创建 SFX)

Converting .bat to .exe with no additional external software (Create SFX)

按照与此相同的步骤进行操作 guide. I am trying to convert from bat to exe without installing any new software such as Bat to Exe Converter。我使用这种方法的原因是因为我工作场所的所有机器都已经安装了 7zip 并且可以使用它,但是我不允许使用主服务器上不存在的外部软件使脚本工作,以便在任何机器上兼容公司。

我有以下 TEST.bat:

ECHO This is a Test bat to exe
pause

config.txt:

;!@Install@!UTF-8! 
RunProgram="TEST.bat" 
;!@InstallEnd@! 

然后我调用以下命令行(在另一个 .bat 中):

COPY /B "%PathTo7Zip%zCon.sfx" + %Config% + %Source7ZFile% %OutputFile%

%PathTo7Zip%7zCon.sfx 的目录 %Config%就是上面的config.txt文件 %Source7ZFile% 是我的 .7z 存档 %OutputFile% 是我的输出 TEST.exe 文件,根据指南的作者,当我调用它时应该是 运行 bat 文件。 但是,调用 TEST.exe 会触发解压缩 .7z 存档(这是预期的),然后在没有 运行ning TEST.bat.

的情况下退出

然而作者解释:

结论:

请务必注意,虽然生成的文件 运行 与源 BAT 文件完全相同,但这并不是真正的批处理到可执行文件的转换。生成的文件是一个 EXE,但它旨在用于自解压安装程序。当您执行生成的 EXE 文件时,过程如下所示:

  1. EXE文件内容解压到临时目录
  2. 读取脚本生成的配置文件
  3. EXE文件中包含的批处理文件在新文件中执行 命令 window.
  4. 完成后,临时文件将被删除。

参考这个

原始脚本接受两个参数 - 您要转换的 .bat 文件和目标可执行文件。

我做了一点修改以接受一个参数:只是您要转换的 .bat 文件。

所以在这种情况下,您可以将批处理文件拖放到此脚本 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=

所有安装了 .NET 的 windows 机器都带有名为 vbc.exe、csc.exe 和 jsc.exe 的文件,位于 %windir%\Microsoft。NET\Framework (对于 32 位)或 Framework64(对于 64 位),在以 "v" 开头的文件夹名称中。

C# 编译器指南:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

Visual Basic 编译器指南:
https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/index
示例命令:
https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/sample-compilation-command-lines

Jscripting 指南:
http://www.phpied.com/make-your-javascript-a-windows-exe/

来自@Itchy 的简单演练,使用 csc.exe 将批处理文件捆绑到 exe 文件:

在同一文件夹中,可能还会找到一个名为 ngen.exe 的文件,该文件 "generates native code so the application does not need to go through the just-in-time compilation process at runtime".
参见:
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