batch-file Batch and VBS hybrids Embed vbscript code into batch file without using temporary files


Here's an example with the technique(hack) invented by the dostips forums' user Liviu:

@echo off
echo Printed by CMD.EXE
cscript //nologo "%~f0?.wsf" //job:JS //job:VBS

exit /b %errorlevel%

  <job id="JS"> 
    <script language="VBScript">
        WScript.Echo("Printed by VBScript"):
  <job id="VBS"> 
    <script language="JScript">
        WScript.Echo("Printed by JScript");

As running wsf file with windows script host is extension sensitive you can run a file with any extension by adding ?.wsf at the end of the file (which is the core of the hack). While the Liviu's example is probably more robust the above code is more simplified version. As wsh does not care much about the things outside the <package> node you are not obligated to put everything in xml comments. Though it's to be careful with redirection symbols (< and >)