54

I have a fairly simple batch script that I would like to execute using a macro on my fancy gaming keyboard. However, SteelSeries Engine only supports opening a .exe file with the macro buttons. Is there any way to convert the script into a simple executable?

Chase Sandmann
  • 1,143
  • 1
  • 9
  • 13
  • 3
    Bash or Batch? They are quite differant, despite being so similar in purpose. – Frank Thomas Jan 23 '15 at 04:38
  • 2
    You could also use AutoIt or AutoHotKey. They’re more powerful than Batch and both include compilers to generate standalone `.exe`s. – Daniel B Jan 17 '16 at 17:10
  • [**Here's how**](http://stackoverflow.com/questions/28174386/how-can-a-bat-file-be-converted-to-exe-without-third-party-tools) this can be done without external tools – npocmaka Mar 26 '16 at 10:02
  • Related: [*Converting .bat to .exe with no additional external software (Create SFX)*](https://stackoverflow.com/q/51098378/3357935) – Stevoisiak Aug 16 '19 at 14:11
  • Use this tool: https://www.portablefreeware.com/index.php?id=1660 – Eyal Sooliman Dec 08 '20 at 10:25

6 Answers6

49

Yes, actually. It's not pretty, but it's clean (nothing to clean up afterwards) and it's actually built-in to your system!

In your C:\Windows\System32\ folder, there is a file called iexpress.exe.

  • Right-click it an Run as administrator.
  • Create a new SED and select "Extract files and run an installation command."
  • Add the script you want, and make sure that on the next screen, you set the install program to cmd /c [your_script.bat] where [your_script.bat] is the script file you want to execute. If you don't do this, windows will try to use Command.com (the old version of Command Prompt) which hasn't been in use for quite a while.
  • Select preferences (you might need to select "Store files using Long File Name inside Package), set an output path (to the .exe file you want to create), and select "No restart".
  • Click next and you should have your .exe!

Just a note, this file actually only acts as a wrapper for your script, and the script itself actually gets executed in a temp folder created on execution (and deleted afterwards), so make sure you don't use any relative paths.

Welz
  • 207
  • 1
  • 4
  • 17
Chase Sandmann
  • 1,143
  • 1
  • 9
  • 13
  • 3
    Unfortunately, our enterprise AV noticed that the resulting executable was a "Cabinet Self-Extractor" and flagged it as malicious activity. – kmote Dec 27 '17 at 19:14
  • @kmote me too. they asked me about it once before because it probably came up in a report and I explained what it is, this time around they just deleted it without asking. – Zero Jul 17 '18 at 01:21
  • 1
    Niice. Thanks man.This helped me create an EXE from a BAT script that I was unable to do earlier using various tools available online such as Bat2Exe etc, – anotherDev Feb 28 '20 at 09:32
  • 2
    +1 for built in tools – Zimba Apr 04 '20 at 17:37
  • +1 for the iexpress tool, i've never known about it before :) – Dee May 24 '20 at 04:41
  • Is there a tool without the limitation you mentioned? Namely it will act as the batch file is run form the `exe` path. – Royi Jun 16 '22 at 20:15
17

Here are 2 free programs that I highly recommend for creating EXE's out of batch files

1 - Bat To Exe Converter

2 - Bat 2 Exe

You can use both programs with simple GUI.

Bat To Exe Converter supports also CLI commands (\? flag for help). Basic example from documentation:

Bat_To_Exe_Converter.exe -bat mybatfile.bat -save myprogram.exe -icon myicon
pkowalczyk
  • 105
  • 3
rammi
  • 187
  • 1
  • 2
6

I found this article which shows you how to convert a .bat to .exe file using a batch-scipt:

@ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

REM Usage:
REM MakeExeFromBat BatFileToConvert [IncludeFile1] [IncludeFile2] [...]
REM
REM Required Parameters:
REM  BatFileToConvert
REM      Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
REM  IncludeFile
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=


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

Important downloads:

Stackcraft_noob
  • 1,820
  • 12
  • 15
  • Is it possible to make the EXE echo to the same command line it was run from? (As opposed to opening a new window) – Stevoisiak Oct 14 '19 at 17:09
6

If your keyboard software supports the passing of arguments to the executable (which is not improbable) you don't have to.

cmd.exe /c <path to batchfile>

would run the batch file, and give you a valid executable to name for the keyboard software. No conversion needed means you can always easily make changes to your bat without additional steps required.

Syberdoor
  • 1,772
  • 13
  • 11
  • 1
    This is nice but how do i turn this into `executor.exe`? – Ramin Melikov May 21 '20 at 17:10
  • 1
    If you mean how you convert this into one single exe file, that is not the point of this answer. The point of this answer is to show that sometimes this is not even necessary because many programs do take command line arguments along with the executable. If you need to convert a script + cmd to a single exe this is covered by some of the other answers (especially the two most top voted ones) – Syberdoor May 22 '20 at 05:33
  • This is not even necessary at all. Because if you are using an EXE then you are also using a MS Machine that runs EXE files and also BAT files. So, merely double clicking a batch file will execute the batch file, or even just opening a terminal in the folder and typing in the batch file 'file name' will do the same thing. I convert batch files to EXE all of the time because not everyone loves running commands through a terminal or even knows how. – ejbytes Nov 14 '20 at 19:19
  • The original question included the info "SteelSeries Engine only supports opening a .exe". While I cannot confirm whether this is true for the specific program it is certainly possible. A program can use ShellExecute which is the same thing that double clicking does and letting windows decide but it is absolutely possible to not do that (sometimes even necessary because of strange decisions by ms) and directly execute a binary. In this case a bat would not work but going through cmd.exe might. – Syberdoor Nov 15 '20 at 20:55
2

Different versions of Windows has different effects for same batch file commands, and some commands are limited to some Windows systems eg. findstr and shutdown.
BTW, Win 10 CMD doesn't allow changes to SETLOCAL on command line. OK for batch files.

See this link for different commands for restarting different versions of windows: https://www.computerhope.com/issues/ch000321.htm

So if you were to compile a script on Win 98, and run on Win 8.1, you'd get unexpected results or scripts may not even work. See list of commands here: https://www.ionos.com/digitalguide/server/know-how/windows-cmd-commands/

For this reason, one would need a different compiler on each version of Windows, preferably which would spit out binary code (generic) that can be run on as many CPU chips as possible, with same instruction sets. A workaround offered by most programs is to wrap the script in an exe file that would unwrap and execute the script when opened/run eg. Bat_To_Exe_Converter, Bat2Exe, BatchCompiler or Winzip: https://support.winzip.com/hc/en-us/articles/115011794948-What-is-a-Self-Extracting-Zip-File-

To solve this issue of portability, virtual machines have become more popular and hence the rise of Java & related scripts.

This however, would still be intepreted code, and not as fast as compiled code. Even byte code (intermediate code) from virtual machines still need to be compiled, even if it's (JIT): https://aboullaite.me/understanding-jit-compiler-just-in-time-compiler/

In short, you can get an exe file which would contain a script that would be intepreted by the command processor, but it won't be a native executable file, meaning it won't run without a host by the Windows operating system.

Zimba
  • 1,051
  • 11
  • 15
1

This is a simple way to convert a bat or cmd file to an exe.

I was having problems running batch files so I decided to rename the file .cmd and see if it would execute. I added the required "setlocal EnableDelayedExpansion" to the top of the new .cmd file but it displayed a "privileges" error.

I have copies of numerous programs that convert BAT to EXE, but most require multiple inputs and options to generate an EXE file. For something simple, I wanted a way to convert a simple BAT to EXE without having to select multiple options and configurations.

I did some research and located on OLD 7zip utility that provided a clean, simple and quick method of converting a simple .bat or .cmd file to an EXE. The old, outdated and unsupported utility is called "7z SFX CREATOR". I discovered that the utility only requires THREE files and two of the outdated 7zip program files that it includes (7z.dll and 7zG.exe) can be easily updated to the latest 7zip versions.

This little utility creates good EXE files from .bat or .cmd and requires only THREE options: the SOURCE file, the TARGET file and the TYPE of output: installer(run a file) or archive (dont run a file).

Since EXE files automatically run with privileges-the EXE I created worked perfectly.

Clean, simple, uncomplicated and efficient. That's all I wanted in a BAT to EXE utility.

jpeni
  • 65
  • 4