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

2

25

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.

I have the following TEST.bat:

ECHO This is a Test bat to exe
pause

and the config.txt:

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

then I call the following command line (in another .bat):

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

%PathTo7Zip% is the directory to 7zCon.sfx %Config% is the config.txt file above %Source7ZFile% is my .7z archive and %OutputFile% is my output TEST.exe file 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.

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:

  1. The contents of the EXE file are extracted to the temp directory.
  2. The config file generated by the script is read.
  3. The batch file contained in the EXE file is executed in a new command window.
  4. Once finished, the temp files are removed.
Crocidolite answered 29/6, 2018 at 9:19 Comment(7)
What is the question? And may be you'll want to check this - #28174886Joint
@Joint the question is why isn't the bat executing? Why is step 3 missing? I tried to change the bat file because maybe pause is not working and I cannot see the output. So I added mkdir not_a_real_dir to my bat file and even then no new folder has been added. The bat file is simply not executing and maybe someone knows why.Crocidolite
Your question does not match your requirements! The idea seems to use external software, a 7zip self extraction module.Deflexed
@Deflexed because in my company we already have 7zip on the computers, however I cannot force the program to work using a software I download and other machines may not have. The software has to be in the shared server otherwise I cannot use it. 7zip is.Crocidolite
Well you should make that clear in your question, i.e. clarify that all machines already carry 7zip and the required sfx module, but no other additional external programs can be downloaded or used.Deflexed
@Joint I reviewed your link and it seems there is another way to do this without 7zip thank you for your help. previously I was working with reference to this page How can I convert a Windows batch script to a .exe? which did not have the solution listed in your link.Crocidolite
Possible duplicate of How can a .bat file be 'converted' to .exe without third party tools?Singlehanded
M
65

Refer to this How can a .bat file be 'converted' to .exe without third party tools?

The original script accepts two arguments - the .bat file you want to convert and the target executable.

I made a little modification to accept one argument : Just the .bat file you want to convert.

So in this case, you can drag and drop your batch file over this script bat2exeIEXP.bat and it will be converted to exe file with the same name as the batch file.

enter image description here

;@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%\2exe.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=
Murray answered 29/6, 2018 at 14:47 Comment(8)
How to set custom icon to this output setup.exe ?Soutine
@Philip: Icons in shortcuts can be changed from the File Properties dialog. Icons in the target executable (.exe file) can be changed by Binary File Editors like Resource Hacker or GConvert. Some others are listed at: listoffreeware.com/free-software-change-program-iconMonatomic
How to handle relative paths in bat script then?Lyonnaise
how to make application start silently in the background? cause it still shows the black cmd boxElul
@Elul change ShowInstallProgramWindow=1Aguiar
Does the resulting exe work with command line arguments? I tried "compiling" ECHO you said %1, which works in the bash script, but with the generated exe I get a syntax errorJonijonie
No more unsafe .EXE installs!Mudlark
Nice solution! Is it possible to add version and publisher to the exe info? And i need to include a folder with some files. The converted exe cant find them. Thanks!Moidore
M
2

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".

Guide on C# compiler:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

Guide on Visual Basic compiler:
https://learn.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/index
Sample commands:
https://learn.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/sample-compilation-command-lines

A Jscripting guide:
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?

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

Monatomic answered 10/11, 2019 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.