Store a file inside of a batch file?

Hi im trying to store a binary file inside of a basic batch script that ive written. Basically i want the script to be able to output this prebuilt file at some point instead of creating it from scratch.

If this is not possible then i would have to include this file separately with the batch file which would then move it into the necessary location, but I'd rather have this file invisible to the user so that it seems that the file is being generated from within the batch.

So is this possible and if so how? Thanks in advance.


You could simply append the binary part to your batch file with COPY.

copy /a batchBin.bat + /b myBinaryFile.bin /b combined.bat

batchBin.bat (The last line with exit /b should end with a newline)

    ;;;===,,,@echo off
    ;;;===,,,echo line2
    ;;;===,,,findstr /v "^;;;===,,," "%~f0" > output.bin
    ;;;===,,,exit /b

The key is the findstr command, it outputs all lines not beginning with ;;;===,,,.
And as each of them are standard batch delimiters, they can be prefix any command in a batch file in any combination.


If the target machine is Vista and higher then you can use certutil.exe and create a base64 encoded text, which you can embed within the batch file.

This example has a base64 encoded file that is just a single space, but the technique is the same with larger binaries.

This batch file uses certutil.exe to decode the certificate and data and create the file with a single space in it, no carriage return or line feed.

@echo off
(
echo -----BEGIN CERTIFICATE-----
echo IA==
echo -----END CERTIFICATE-----
)>file.tmp
certutil -decode file.tmp "file with a single space.txt" >nul
del file.tmp

To encode a program file for placing inside the batch file you use a command line like this, replacing myprogram.exe with your program name:

certutil -encode -f "myprogram.exe" file.tmp

and then place the contents of file.tmp inside the batch file:

@echo off
(
echo -----BEGIN CERTIFICATE-----
echo place the data from file.tmp here
echo as it is listed inside the file
echo -----END CERTIFICATE-----
)>file.tmp
certutil -decode file.tmp "myprogram.exe" >nul
del file.tmp

It's fairly easy to add the echo to the front of each line and then use the data from file2.tmp:

@echo off
for /f "delims=" %%a in (file.tmp) do >>file2.tmp (echo(echo %%a)

Read about HERE documents. Msdos batch does not offer them, but perl and ruby are both available and do.

http://en.wikipedia.org/wiki/Here_document