People have been embedding and executing VBScript within batch files for a long time. But all the published solutions that I have seen (at the time this question was originally posed) involve writing a temporary VBS file. For example: Embed VBScript inside Windows batch file.
Is it possible to execute embedded VBScript within batch without writing a temporary file?
Best Answer
Note - jump to the UPDATE 2014-04-27 section at the bottom of this answer for the best solution.
I used to think the answer was no. But then DosTips user Liviu discovered that the
<SUB>
character (Ctrl-Z, 0x1A, decimal 26) has bizare effects when embedded within a batch file. If functions somewhat like a line terminator such that it is possible for batch commands that follow a REM (or a :: remark hack) to execute if they are preceded by Ctrl-Z. http://www.dostips.com/forum/viewtopic.php?p=13160#p13160This has been confirmed on XP Home Edition sp3, Vista Home Premium sp2 64 bit, and Vista Enterprise sp2 32 bit. I'm assuming it works on other Windows versions.
Note - the code below is supposed to have embedded Ctrl-Z characters. I swear I used to see them on this site when viewed with IE8. But they seem to have been lost from this post somehow and I cannot figure out how to post them anymore. I've replaced the characters with the string
<SUB>
That is the key to a successful batch/vbs hybrid. As long as each batch command is preceded by
rem<SUB>
or::'<SUB>
, then the vbs engine won't see it, but the batch command will run. Just make sure you terminate the batch portion with anEXIT
orEXIT /B
. Then the remainder of the script can be normal looking vbs.You can even have a batch label if needed.
:'Label
is both a valid vbs comment and a valid batch label.Here is a trivial hybrid script. (again with
<SUB>
in place of embedded Ctrl-Z char)Update 2012-04-15
jeb found a solution that avoids the awkward CTRL-Z, but it prints out ECHO OFF at the start and also sets some extraneous variables.
I have found a solution without CTRL-Z that eliminates the extraneous variables and is simpler to comprehend.
Normally the special characters
&
,|
,<
,>
etc. don't work after aREM
statement in batch. But the special characters do work afterREM.
. I found this nugget of information at http://www.dostips.com/forum/viewtopic.php?p=3500#p3500. A test shows thatREM.
is still a valid VBS comment. EDIT - based on jeb's comment, it is safer to useREM^
(there is a space after the caret).So here is a trivial VBS/batch hybrid using
REM^ &
. The only drawback is it printsREM &
at the beginning, whereas jeb's solution printsECHO OFF
.Here is another trivial example that demonstrates multiple batch commands, including a CALL to a labeled sub-routine.
I still like the CTRL-Z solution because it eliminates all extraneous output.
UPDATE 2012-12-17
Tom Lavedas posted a method to conveniently run dynamic VBS from a batch script over at Google Groups: No file VBS hybrid scripting. The method uses mshta.exe (Microsoft HTML Application Host).
His original batch solution relied on an external small VBS.BAT script to execute the VBS within a FOR /F. I modified the syntax slightly to make it convenient to embed directly within any given batch script.
It is quite slow, but very convenient. It is restricted to executing a single line of VBS.
The VBS is written normally, except all quotes must be doubled: A quote enclosing a string must be written as
""
, and quotes internal to a string must be written as""""
. Normally the mini script is executed within the IN() clause of a FOR /F. It can be executed directly, but only if stdout has been redirected or piped.It should work on any Windows OS from XP onward as long as IE is installed.
UPDATE 2014-04-27
Over at DosTips there is a great compendium of js/vbs/html/hta hybrids and chimeras in cmd/bat. Lots of good stuff from various people.
Within that thread, DosTips user Liviu discovered a beautiful VBS/batch hybrid solution that uses WSF.
I think this solution is fantastic. The batch and WSF sections are clearly separated by nice headers. The batch code is absolutely normal, without any odd syntax. The only restriction is the batch code cannot contain
-->
.Similarly, the VBS code within WSF is absolutely normal. The only restriction is the VBS code cannot contain
</script>
.The only risk is the undocumented use of
"%~f0?.wsf"
as the script to load. Somehow the parser properly finds and loads the running .BAT script"%~f0"
, and the?.wsf
suffix mysteriously instructs CSCRIPT to interpret the script as WSF. Hopefully MicroSoft will never disable that "feature".Since the solution uses WSF, the batch script can contain any number of independent VBS, JScript, or other jobs that can be selectively called. Each job can even utilize multiple languages.