I have been experimenting with making a batch-file installer program. by embedding the file in the batch file and using a more +... line to extract it.

Here is my code:

@echo off
set self=%~df0
>%userprofile%\desktop\file.exe more +8 "%self%"
echo done!
REM beginning of exe file...

€  ~  * 0      ~  *  €  *’s6  (e  
  €  s.  
€  *   (f  
*^(  og  
(9  oh  
*0 K     ~  ->~  (i  
~  -(  þ7  sk  
€  Þ(m  
Ü~  * 


The problem:

I am attempting to extract a GUI exe, however, once it is extracted, when I try to run it, Windows treats it like it is a 16-bit program... Why does this happen? And how can I fix it?



  • 2
    Its not the exe that is running in 16-bit mode, but the batch file (or more precisely the interpreter executing it; the vdm). As for your general idea, you are probably not going to be able to effectively encode the binary as ascii, and then just write it out again as binary data. The error you are seeing is from the CPU itself, telling you that it does not understand the machine code being submitted to it (an invalid CPU instruction) so it is clear that the extracted file is no longer a valid executable. – Frank Thomas Dec 15 '16 at 16:46
  • @FrankThomas Is there anything I could try instead of this? – cascading-style Dec 15 '16 at 16:48
  • 2
    you just can't store a binary file inside a text document without some deep and extensive knowledge of data encoding and some rather sophisticated code. sorry, but you probably want to take a different tact. – Frank Thomas Dec 15 '16 at 16:56

As pointed out in the comments, stuffing an executable program inside a text file will butcher the binary file. Instead, you can store an encoded version and then decode it. To accomplish that easily, you can use PowerShell. Start with this batch file:

@echo off
powershell -command "[IO.File]::WriteAllBytes('extracted.exe', [Convert]::FromBase64String((gc '%0')[-1]))"
del extracted.exe

REM Base64-encoded program will be inserted below

Add an extra blank line at the end.

The interesting part is the PowerShell invocation. That command reads in the contents of the batch file, takes the very last line, decodes it from Base64, and writes those bytes to a file called extracted.exe. Then the batch file just runs that EXE, and once that's done it cleans up by deleting the temporary executable.

To make the last line have the necessary information, fire up PowerShell, cd into the directory with the batch file, then run this (with actual file names and paths put in):

[Convert]::ToBase64String([IO.File]::ReadAllBytes('C:\fullPathTo\file.exe')) | Out-File 'batchFile.bat' -Append -Encoding ASCII

Once that's done, your batch file will be able to extract that program and run it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.