Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Note - jump to the</em> <strong><em>UPDATE 2014-04-27</em></strong> <em>section at the bottom of this answer for the best solution.</em></p> <p>I used to think the answer was no. But then DosTips user Liviu discovered that the <code>&lt;SUB&gt;</code> 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. <a href="http://www.dostips.com/forum/viewtopic.php?p=13160#p13160" rel="noreferrer">http://www.dostips.com/forum/viewtopic.php?p=13160#p13160</a></p> <p>This 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.</p> <p><strong><em>Note</em></strong> - <em>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 <code>&lt;SUB&gt;</code></em></p> <pre><code>::&lt;sub&gt;echo This will execute in batch, but it will fail as vbs. rem&lt;SUB&gt;echo This will execute in batch, and it is also a valid vbs comment ::'&lt;SUB&gt;echo This will execute in batch and it is also a valid vbs comment </code></pre> <p>That is the key to a successful batch/vbs hybrid. As long as each batch command is preceded by <code>rem&lt;SUB&gt;</code> or <code>::'&lt;SUB&gt;</code>, then the vbs engine won't see it, but the batch command will run. Just make sure you terminate the batch portion with an <code>EXIT</code> or <code>EXIT /B</code>. Then the remainder of the script can be normal looking vbs.</p> <p>You can even have a batch label if needed. <code>:'Label</code> is both a valid vbs comment and a valid batch label.</p> <p>Here is a trivial hybrid script. <em>(again with <code>&lt;SUB&gt;</code> in place of embedded Ctrl-Z char)</em></p> <pre><code>::'&lt;SUB&gt;@cscript //nologo //e:vbscript "%~f0" &amp; exit /b WScript.Echo "Example of a hybrid VBS / batch file" </code></pre> <p><strong><em>Update 2012-04-15</em></strong></p> <p>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.</p> <p>I have found a solution without CTRL-Z that eliminates the extraneous variables and is simpler to comprehend.</p> <p>Normally the special characters <code>&amp;</code>, <code>|</code>, <code>&lt;</code>, <code>&gt;</code> etc. don't work after a <code>REM</code> statement in batch. But the special characters do work after <code>REM.</code>. I found this nugget of information at <a href="http://www.dostips.com/forum/viewtopic.php?p=3500#p3500" rel="noreferrer">http://www.dostips.com/forum/viewtopic.php?p=3500#p3500</a>. A test shows that <code>REM.</code> is still a valid VBS comment. <strong><em>EDIT</em></strong> <em>- based on jeb's comment, it is safer to use <code>REM^</code> (there is a space after the caret).</em></p> <p>So here is a trivial VBS/batch hybrid using <code>REM^ &amp;</code>. The only drawback is it prints <code>REM &amp;</code> at the beginning, whereas jeb's solution prints <code>ECHO OFF</code>.</p> <pre><code>rem^ &amp;@cscript //nologo //e:vbscript "%~f0" &amp; exit /b WScript.Echo "Example of a hybrid VBS / batch file" </code></pre> <p>Here is another trivial example that demonstrates multiple batch commands, including a CALL to a labeled sub-routine.</p> <pre><code>::' VBS/Batch Hybrid ::' --- Batch portion --------- rem^ &amp;@echo off rem^ &amp;call :'sub rem^ &amp;exit /b :'sub rem^ &amp;echo begin batch rem^ &amp;cscript //nologo //e:vbscript "%~f0" rem^ &amp;echo end batch rem^ &amp;exit /b '----- VBS portion ------------ wscript.echo "begin VBS" wscript.echo "end VBS" 'wscript.quit(0) </code></pre> <p>I still like the CTRL-Z solution because it eliminates all extraneous output.</p> <p><strong><em>UPDATE 2012-12-17</em></strong></p> <p>Tom Lavedas posted a method to conveniently run dynamic VBS from a batch script over at Google Groups: <a href="https://groups.google.com/d/topic/alt.msdos.batch.nt/i1MbawOWFIo/discussion" rel="noreferrer">No file VBS hybrid scripting</a>. The method uses mshta.exe (Microsoft HTML Application Host).</p> <p>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.</p> <p>It is quite slow, but very convenient. It is restricted to executing a single line of VBS.</p> <p>The VBS is written normally, except all quotes must be doubled: A quote enclosing a string must be written as <code>""</code>, and quotes internal to a string must be written as <code>""""</code>. 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.</p> <p>It should work on any Windows OS from XP onward as long as IE is installed.</p> <pre><code>@echo off setlocal :: Define simple batch "macros" to implement VBS within batch set "vbsBegin=mshta vbscript:Execute("createobject(""scripting.filesystemobject"")" set "vbsBegin=%vbsBegin%.GetStandardStream(1).write(" set ^"vbsEnd=):close"^)" :: Get yesterday's date for /f %%Y in ('%vbsBegin% date-1 %vbsEnd%') do set Yesterday=%%Y set Yesterday pause echo( :: Get pi for /f %%P in ('%vbsBegin% 4*atn(1) %vbsEnd%') do set PI=%%P set PI pause echo( set "var=name=value" echo Before - %var% :: Replace = for /f "delims=" %%S in ( '%vbsBegin% replace(""%var%"",""="","": "") %vbsEnd%' ) do set "var=%%S" echo After - %var% pause echo( echo Extended ASCII: for /l %%N in (0,1,255) do ( %= Get extended ASCII char, except can't work for 0x00, 0x0A. =% %= Quotes are only needed for 0x0D =% %= Enclosing string quote must be coded as "" =% %= Internal string quote must be coded as """" =% for /f delims^=^ eol^= %%C in ( '%vbsBegin% """"""""+chr(%%N)+"""""""" %vbsEnd%' ) do set "char.%%N=%%~C" %= Display result =% if defined char.%%N ( setlocal enableDelayedExpansion echo( %%N: [ !char.%%N! ] endlocal ) else echo( %%N: Doesn't work :( ) pause echo( :: Executing the mini VBS script directly like the commented code below :: will not work because mshta fails unless stdout has been redirected :: or piped. :: :: %vbsBegin% ""Hello world"" %vbsEnd% :: :: But this works because output has been piped %vbsBegin% ""Hello world"" %vbsEnd% | findstr "^" pause </code></pre> <p><strong><em>UPDATE 2014-04-27</em></strong></p> <p>Over at DosTips there is a great compendium of <a href="http://www.dostips.com/forum/viewtopic.php?f=3&amp;t=5543" rel="noreferrer">js/vbs/html/hta hybrids and chimeras in cmd/bat</a>. Lots of good stuff from various people.</p> <p>Within that thread, DosTips user Liviu discovered a <a href="http://www.dostips.com/forum/viewtopic.php?p=33963#p33963" rel="noreferrer">beautiful VBS/batch hybrid</a> solution that uses WSF.</p> <pre><code>&lt;!-- : Begin batch script @echo off cscript //nologo "%~f0?.wsf" exit /b ----- Begin wsf script ---&gt; &lt;job&gt;&lt;script language="VBScript"&gt; WScript.Echo "VBScript output called by batch" &lt;/script&gt;&lt;/job&gt; </code></pre> <p>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 <code>--&gt;</code>.</p> <p>Similarly, the VBS code within WSF is absolutely normal. The only restriction is the VBS code cannot contain <code>&lt;/script&gt;</code>.</p> <p>The only risk is the undocumented use of <code>"%~f0?.wsf"</code> as the script to load. Somehow the parser properly finds and loads the running .BAT script <code>"%~f0"</code>, and the <code>?.wsf</code> suffix mysteriously instructs CSCRIPT to interpret the script as WSF. Hopefully MicroSoft will never disable that "feature".</p> <p>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.</p> <pre><code>&lt;!-- : Begin batch script @echo off echo batch output cscript //nologo "%~f0?.wsf" //job:JS cscript //nologo "%~f0?.wsf" //job:VBS exit /b ----- Begin wsf script ---&gt; &lt;package&gt; &lt;job id="JS"&gt; &lt;script language="VBScript"&gt; sub vbsEcho() WScript.Echo "VBScript output called by JScript called by batch" end sub &lt;/script&gt; &lt;script language="JScript"&gt; WScript.Echo("JScript output called by batch"); vbsEcho(); &lt;/script&gt; &lt;/job&gt; &lt;job id="VBS"&gt; &lt;script language="JScript"&gt; function jsEcho() { WScript.Echo("JScript output called by VBScript called by batch"); } &lt;/script&gt; &lt;script language="VBScript"&gt; WScript.Echo "VBScript output called by batch" call jsEcho &lt;/script&gt; &lt;/job&gt; &lt;/package&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload