1

I came across a way to convert my .bat with dependencies on tool to an .exe file. However when I try using the script and run the .exe created, I always getting an error. Seems I modified the script incorrectly.

Anyone can help, please?

Here's the code with my modifications:

@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
faital
  • 19
  • 2
  • 7

3 Answers3

1

This doesn't really convert a bat file to an exe. It just creates a selfextracting archive (exe) which contains the bat file. On execution it extracts the file to a temporary folder and runs it from there. You can even extract the bat from the exe just by using 7zip/rar/winzip or any other archiver.

If you want to convert a bat to an exe for real you should use one of the tools from the web (like this one: http://www.f2ko.de/index.php?lang=en) or concider using a simple script language like AutoIt.

If you pick the second, you can simply execute your bat code with Run("put your bat code in here") and you can compile your script to a "real" exe file.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Well, this is actually a problem. In this case the only way I can think of is to pack your program and the external exe into an SFX as it's done in your example. However, if you don't wan to mess around with the code up there, you can just do it by hand. Put your bat and the external exe files into an 7zip archive, write a config file that executes your bat on extraction and use binary copy to put the sfx-binary file together with your 7zip archive into one single exe file. If you need help with that, just ask me. – MichaelS Jan 15 '15 at 11:32
  • Take a look at this page: http://www.7zsfx.info/en/ There is an example how to use the 7zip SFX and even how to put a simple gui upon your exe. – MichaelS Jan 15 '15 at 11:38
  • ;!@Install@!UTF-8! RunProgram="yourbat.bat" GUIMode="2" ;!@InstallEnd@! This will extract BOTH (your bat and your exe) to a temp folder without showing a GUI (GUIMOde="2") and run your bat file. It should automatically find the used external exe because both of them are in the same temp folder. This also might help: http://stackoverflow.com/questions/2568924/why-does-7zip-ignore-my-installpath-when-making-a-sfx-installer – MichaelS Jan 15 '15 at 12:10
  • So you have the sfx file (7zsd.sfx), your archive (arch.7z) and your config file (config.txt), you are combining them with `copy /b 7zsd.sfx + config.txt + arch.7z outputfile.exe` and it doesn't work? Are you sure all files are being found? Maybe a path problem? – MichaelS Jan 15 '15 at 12:27
  • Woooooooooh, it's working now man. This is missing in the config. InstallPath="%TEMP%" – faital Jan 15 '15 at 13:05
  • @faital The [referenced site](http://www.f2ko.de/index.php?lang=en) seems to still work, but contains no downloads. The [subsite "Programs"](https://f2ko.de/programme/) is empty, as are [the archive versions of that site that I found](http://web.archive.org/web/20200701000000*/https://f2ko.de/programme/). The whole page is in german, so a auto-translated version could be offered, `?lang=en` seems to yield no effect. However, you can find a download to the Converter [here, even in a portable version.](https://www.softpedia.com/get/System/File-Management/Batch-To-Exe-Converter.shtml#download) – Cadoiz Oct 12 '21 at 15:11
  • [Here is an archive version to 7zsfx.info/en](http://web.archive.org/web/20120201020827/http://7zsfx.info/en/). [@Starwarswii](https://stackoverflow.com/users/3249197/starwarswii) mentioned [his answer providing the newest modified sfx files found, as well as some more information](https://stackoverflow.com/a/11481752/3249197). – Cadoiz Oct 12 '21 at 15:17
0

For an alternative approach, you can basically do the same thing as described in the accepted answer (making a 7z-SFX) with WinRAR. That way, you can also do it with a GUI, and I will try to add some more useful information.

Actually, you can also use the latter approach to generate portable applications and it also works with "converting" every runnable (or openable) file into an .exe.


If you need that "portability hack", you should unpack your .exe or .msi installer with Universal Extractor. Details can be found in this Article, Step 1 to 4. Newer Versions of 7zip or WinRAR also come with comparable features.

  • Now you add all needed files to the archive. In the easiest case, this is just your .bat script or whatever file you want to "convert" into an .exe applivation. (Step 5 here)

  • Steps 6 and 7 are just some Settings for the SFX-Archive, 8 is the interesting one, as you select what you actually want to run there. Input the name of your (.bat-)file.

  • Step 9 lets you select where to unpack to - you do this setting manually and programmatically in the MakeExeFromBat.bat-script.

  • After this process you created a Portable App in SFX archiever form, enjoy


The word "converting" was put into quotation marks, because running that .exe actually works like this:

  1. The contents of the (SFX-)EXE file are extracted from the "archive part" to a directory as the specified temp directory.

  2. ( The config file generated by the script is read. )

  3. The file, that was previously contained in the EXE file and then extracted, is now executed in a new window.

    a) This file could besides a .bat be anything - as e.g. an image, a MP3 or a video

    b) or also a Python Script (of course your OS needs to know how to deal with that file.

  4. Once finished, the temp files are removed.

You can also derive some limitations from that. If you have a .bat that needs the content of the working directory, you will have a problem. (Say, a batch that renames all files in the current dir from 1 to n.) In some cases that can be dealt with by adding all needed files to the archive too. On Windows Vista and all newer OSes, you might encounter a 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. 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.


All references were already linked, but once again: Big credit goes to Jason Faulkner for providing the Article and 7zip-Approach, binbert for the WinRAR-SFX Solution (which is as hinted much more versatile -> portability) and some credit to creative8 for finding the two and the article comparing them.

Cadoiz
  • 1,446
  • 21
  • 31
0

Actually, I was develping another solution using AutoHotkey. In my case, I just want to be able to add my .bat to the windows start menu - but the options are not limited to that.

The script itself is just a oneliner and .AHK is easily converted to .exe (I used v1.1.33.09):

run % SubStr(A_ScriptName, 1, -4) ;// run also has the option to run your file minimized or hidden, see the source 2 below

Source 2

What it does is taking its own name, removing the .ahk or .exe respectively (the last 4 characters, hence -4) and running excactly that. Usage could not be easier: you have a runme.bat, so you rename the program I provide to runme.bat.exe. Say you want the .exe to open an image.png - guess what, rename it to image.png.exe. You get the gist - that's it. It dynamically checks its name to find what to run. In my opinion, this is not much less mighty than "unpacking the .bat and then run it", but (again imho) it is much more elegant.

Use it as you wish, I should probably start a public github page or so.

Cadoiz
  • 1,446
  • 21
  • 31