Convert .bat to .exe without cheating

Last updated on September 2, 2024 by , Posted to developer tools

bat to exe converter

Why did I mention cheating in the title of the article? Because almost all converters do not do what you expect them to do. First of all, let's get this straight - who needs these batch files nowadays? This is the main question that many colleagues ask me. There was a much more advanced PowerShell a long time ago, so who needs these batch scripts? I was skeptical too, until I talked to our beta testers. It turns out that people are creating bat files thousands of lines of code and over the years administrators have accumulated hundreds of scripts that continue to work without significant modifications. Don't forget the main principle - don't touch what works well.

1 What's the point of this?

Batch files are simple text files containing a series of commands that can be executed at the Windows command line. Executable files are a set of commands and data in binary form that are executed directly by the CPU.

Compiling bat scripts into executable files allows you to create standalone executable programs that can be run without the need for a command line or batch interpreter. Why do they need to be compiled? Programmers will never have such a question, because they are used to distributing programs in binary format and not disclosing the source codes. In this respect, batch files have an advantage - they do not require a development environment, they do not depend on the Windows version or processor architecture.

But when it comes to administration, logins and passwords are stored in open form in bat files, and this is a serious security threat. This is the main reason for compilation. In addition to private data, the script author may seek to hide the work algorithm from prying eyes, or even hide the console window and perform everything in the background.

Side benefits of compilation will be an increase in the speed of script execution and the ability to set some parameters that are specific to binary files - icon, application version, administrator manifest, additional resources.

2 Compiler vs Converter

What difference does it make to me if I get an executable file anyway? Actually, it makes a huge difference. First of all, let's remember why we compile a batch file? The most common answer is to hide the source code. This is where the difference between these two approaches comes into play.

The compiler translates your source code into machine code. The compiled file is loaded into the computer's RAM and executed directly by the processor. It does not require a separate interpreter to execute, and reverse engineering is difficult.

The converter simply creates a self-extracting archive with your original script. Before running such an EXE file, it simply unzips the script into a temporary folder. From there it is run through the system interpreter cmd.exe. There is no protection of the source code - you can close the console window during execution and see the contents of the script in the temporary folder.

Thus we can distinguish two main differences - the compiler reliably protects your source code and speeds up its execution. A converter does neither neither. It simply hides the file in the hope that it will not be noticed.

3 How batch files are compiled

Actually bat script is not a compiled programming language, it is just a set of commands for the system interpreter. It does not have a strict syntax, but is executed sequentially until the end of the file or until an error occurs. This is what makes it so slow, especially when executing loops - the same block of text is read from disk each time, parsed into individual commands, and executed. If only you could parse it once and then change only variables.....

Bat to Exe Compiler

Quick Batch File Compiler Benefits

  • Reliable protection of your source codes
  • Acceleration of script execution
  • Embedded files and folders
  • Application icon, manifest and version information

This approach is called just-in-time(JIT) compilation or compilation to intermediate byte-code. This has long been implemented in popular programming languages like Java or C#, and now for bat files as well. Quick Batch File Compiler is the first, and so far the only, compiler for batch files. It supports both full-fledged JIT mode and conversion to a self-extracting archive. It is easy to see the difference in the implementation of these approaches.

4 Convert BAT to EXE step-by-step

Since we are talking about administration and compilation, I think it is inappropriate to describe downloading and installing software in detail. Let's go straight to the main window of the compiler.

Step 1: Open or create a batch file

There are no surprises here, you can simply use the editor to type the batch file commands or open an already prepared .cmd or .bat script. When typing manually, it is convenient to use context help - if you select a command and press F1, the documentation for that command will be displayed.

Step 2: Select compilation mode

This is the most important step where we specify the compilation mode. Open the project settings and select JIT mode, this is the mode responsible for the actual compilation. Next, you need to select the target architecture for the executable file - x86 or x64. For better compatibility, you may choose x86 mode, although there are very few such systems left today.

Step 3: Adding executable resources

Every executable file contains a resource section that stores its icon and information about its version and developer. The file's manifest specifies the necessary conditions for execution, such as compatibility mode or run as administrator. These parameters are desirable but not mandatory.


bat file resources
Step 4: Use embedded files (optional)

Since batch files do not have the ability to use images, sounds or other types of data, so-called embedded files are used instead. These files are placed inside an EXE file and can be used during script execution. The path to them is specified in the MYFILES environment variable. Use them at your discretion.

Step 5: Compiling bat into exe

Now it's time to run the compilation. At this time embedded files will be compressed, code sections will be added and resources will be assigned. If you are used to working from the command line, you can skip all the previous steps and start compiling the batch file from the console.

5 Conclusion

I'm sure you're aware that not all the programs live up to the hype in the ads. It's fair to say that almost all popular converters don't deliver on what they promise in their descriptions. In most cases, you can just close the console window and check the temporary folder for hidden files with .bat and .cmd extensions. A real alternative in the form of a full-featured compiler has been around for a long time and works exactly as promised, reliably protecting your batch files from browsing, and the speed increase is just a nice bonus.


Related Articles