How can I convert a Windows batch script to a .exe?

24

12

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

Posted 2015-01-23T04:21:37.497

Reputation: 573

2Bash or Batch? They are quite differant, despite being so similar in purpose. – Frank Thomas – 2015-01-23T04:38:26.110

1You could also use AutoIt or AutoHotKey. They’re more powerful than Batch and both include compilers to generate standalone .exes. – Daniel B – 2016-01-17T17:10:18.077

Here's how this can be done without external tools – npocmaka – 2016-03-26T10:02:12.273

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

– Stevoisiak – 2019-08-16T14:11:44.173

Answers

22

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.

Chase Sandmann

Posted 2015-01-23T04:21:37.497

Reputation: 573

2Unfortunately, our enterprise AV noticed that the resulting executable was a "Cabinet Self-Extractor" and flagged it as malicious activity. – kmote – 2017-12-27T19:14:56.397

@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 – 2018-07-17T01:21:53.450

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, – Harsh Kanchina – 2020-02-28T09:32:17.377

13

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

rammi

Posted 2015-01-23T04:21:37.497

Reputation: 139

Bat2Exec allows you to add Administrator manifest which I liked. – pun – 2016-01-17T17:28:27.567

2Could you also give a quick rundown on how to accomplish the task with those programs? It never hurts to have extra detail in an answer :) – Ben N – 2016-01-17T21:39:52.040

Advanced Bat 2 Exe converter is shareware – djibe – 2019-09-17T20:02:22.307

http://web.archive.org/web/20150810162835id_/http://www.f2ko.de/en/b2e.php – Hüseyin Teoman Deniz – 2019-09-17T21:34:53.787

Does 2 - Bat 2 Exe support CLI? I cannot see any info on this. – Ste – 2020-02-04T23:20:41.623

Virus Detectors will mark the exe files from """1 - Bat To Exe Converter""" as potential threats – SoliQuiD – 2020-02-12T09:40:07.467

5

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

Posted 2015-01-23T04:21:37.497

Reputation: 1 364

4

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

Posted 2015-01-23T04:21:37.497

Reputation: 1 466

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 – 2019-10-14T17:09:03.643