Embed a batch file in Visual Basic 6

I have a VB program which runs a batch file. How can I embed this batch file into VB itself, so no one can steal/look at the code?

Thanks again
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

off the top of my head...

store the contents of batch file as a constant string.
at runtime, create a file with the content of that string.
run the file (using Shell(...))
delete the file after complete

need to know how?
If you look at this request ...


One of the solutions was to build and execute a BAT file.  Sounds like what you want.
Shank1Author Commented:
bobbit31, that would be great. It's alot of code, maybe 2 pages worth, so  otherwise I could use Roguesolutions's solution, but thats alot of code to copy over into VB and add quotes into etc..  unless thats the only way to do it

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.


What I do for transferring SQL strings into VB so I can build them into a string (similar I think) is to copy the text into Excel so that each line of text is in a row.

Then build formulae in the column to the right that adds the necessary characters to create a string, so ...

If A1 is ECHO My Batch File
and A2 is ECHO Line two of my file

Then B1 is ="    strBat = strBat & """ & A1 & """ & vbCrLf "
and B2 is ="    strBat = strBat & """ & A2 & """ & vbCrLf "

With a bit of filling down and copying you can get the VB code mostly defined for you.
Add the file to Resources...

hold on, i'm making an example now

Shank1Author Commented:
Rougesolutions : So each line of my batch file needs to be declared?

Not sure what you mean by declared.  Here's a fuller example that might help.

Here's a chunk of batch file from my PC, no idea what it does just found it.

@echo off

set VC_PATH=%1
if "%VC_PATH%"=="NONE" goto noVC
call %VC_PATH%\bin\vcvars32.bat
goto msEnv

rem echo Visual C++ product directory could not be found.
set LIB=

set MS_DISK=%2
set MS_MSTN=%3


Here's the code to build and run it.

Private Sub WriteMe()
   Open App.Path + "\Delself.bat" For Append As #1
   Print #1, strGetBatchContent()
   Shell App.Path + "\Delself.bat", vbHide
End Sub

Private Function strGetBatchContent() As String

    Dim strBat  As String
    strBat = ""
    strBat = strBat & "@ECHO OFF" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & "set CLASSPATH0=%CLASSPATH%" & vbCrLf
    strBat = strBat & "set VC_PATH=%1" & vbCrLf
    strBat = strBat & "if ""%VC_PATH%""==""NONE"" goto noVC" & vbCrLf
    strBat = strBat & "set NTTOOLS=%VC_PATH%" & vbCrLf
    strBat = strBat & "call %VC_PATH%\bin\vcvars32.bat" & vbCrLf
    strBat = strBat & "GoTo msEnv" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & ": noVC" & vbCrLf
    strBat = strBat & "Rem echo Visual C++ product directory could not be found." & vbCrLf
    strBat = strBat & "set NTTOOLS=" & vbCrLf
    strBat = strBat & "set LIB=" & vbCrLf
    strBat = strBat & "set INCLUDE=" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & ": msEnv" & vbCrLf
    strBat = strBat & "set MS_DISK=%2" & vbCrLf
    strBat = strBat & "set MS_MSTN=%3" & vbCrLf
    strBat = strBat & "set MS=%MS_DISK%%MS_MSTN%" & vbCrLf
    strBat = strBat & vbCrLf

    strGetBatchContent = strBat
End Function

Note the vbCrLf's that are on each line and between the 'blocks' of the batch file to ensure the gaps are maintained (just for checking purposes really)

Also any " that were in the original file are repeated to ensure the string builds properly.  You could do this as part of the Excel stage by using the SUBSTITUTE worksheet function, e.g.
="    strBat = strBat & """ & SUBSTITUTE(A1,CHAR(34), CHAR(34) & CHAR(34)) & """ & vbCrLf "

It will be fiddly but secure, no text file exists as part of your install.  You canget the Batch file to delete itself as it's last command.

Perhaps the Resource method from Bobbit will suit your needs better.
simply click tools->Resource Editor (if you don't see it... add it via Add-Ins->VB6 Resource Editor)

click "add custom" and select your batch file.
The file will now be stored along w/ the compiled .exe (you don't have to have the actual .bat anywhere on system)

To get the contents of the file from resources:

Dim strFile As String
strFile = StrConv(LoadResData(101, "CUSTOM"), vbUnicode) '' strFile now has contents of batch file (assuming you left the default properties for the resource)

now all you have to do is:

Open App.Path + "\RunMe.bat" For Output As #1
Print #1, strFile
Close (1)
Shell App.Path + "\RunMe.bat", vbHide

and then delete the file on unload.


I realise I could go read the help ;) but doesn't a resource file 'travel' as a .res when you do your installation?  I remember something about using a Resource file to create language changes (i.e. you substitute the .res of the language the user wants).
afaik, no that's not how it works.

The resource file is used when compiling the exe and the contents of the resource file are included in the build.

Try it out and test... do the above steps, make the .exe, delete the .res and run the .exe.  The info will still be there.

Fair enough, I've never used one (and no VB on this PC to play with just at the mo).  I expect Shank will feedback, it's definitely less painful than my approach!

Why not encrypt the batch.  Decypt it, run it, deletete he decrypted file.

here's a link to an encryption routine.
I seem to remember (back in the MSDOS days) there was a utility which converted batch files into executables.  I think it was called bat2exe.  Perhaps if you can compile the .bat file into a .com or .exe file, it would be unreadable.

But is there any reason you could not just convert the functionality of the batch file into real VB code and just run the VB version?
Shank1Author Commented:
bobbit31: How do I delete the batch file after the exe has been run?
Private Sub Form_Unload(Cancel as Integer)
   Kill <path to file>
End Sub

You can also have a DEL command in the batch file as the last line and it will delete itself (based on the solution I pasted a link to from another question)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shank1Author Commented:
That worked great. Thanks. but the problem is if the program closes unexpectedly, such as you do a shutdown, then the unload call doesn't execute right? If that is the case, I put the kill command as the last command in the form load procedure, right after the shell App.path _ " " code. And if you close the program then, it gives you an error file not found too. can we prevent that from happening?
remove the kill statement from form unload?
or put del command as last line in batch file as roguesolutions suggested
Shank1Author Commented:
Perfect, works like a charm. Thanks guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.