0

Sometimes in my batch scripts I usually merge batch code with JavaScript code (.js file), like this:

@if(@X)==(@Y) @end /*          ← Hybrid line. Starts a Javascript comment block.
::Batch code
@echo off
set code=65
cscript //nologo /E:JScript "%~f0" %code%      ← Calls itself using cscript.exe.
pause>nul
exit/b
*/                        ← Ends the comment block, now we can write JavaSCript.
//JavaScript code
WScript.echo(String.fromCharCode(WScript.Arguments.Unnamed(0)));

Its output is A (65 ASCII).

Ok, JavaSCript works fine. My question is: How do I do that with VBScript? I am not an expert in VBScript, but I know there's not any way to make comment blocks in my code, hence I can't use the same technique as I used with JavaScript.

Supposing I have a code that does the same thing (converts ASCII code passed on first parameter to a char), but now in VBScript:

WScript.echo chr(WScript.Arguments.Unnamed(0))

How can I merge this VBScript code with my batch code as I did with JavaScript?

Rafael
  • 3,042
  • 3
  • 20
  • 36

1 Answers1

4

What i have used. To be saved as .cmd file

<?xml : version="1.0" encoding="UTF-8" ?> ^<!-- This is the cmd zone ---------

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "code=65"
    cscript //nologo "%~f0?.wsf" //job:mainJob %code%
    exit /b

------------------------------------------------ This is the vbs zone ------->
<package>
  <job id="mainJob">
    <script language="VBScript"><![CDATA[

        If WScript.Arguments.Unnamed.Count > 0 Then 
            WScript.StdOut.WriteLine Chr(WScript.Arguments.Unnamed(0))
        End If

    ]]></script>
  </job>
</package>

Two tricks are used in this case. The first one is hide the cmd part inside a xml comment inside a wsf file that will execute the vbscript part. The second trick is the call to cscript with the cmd file as the script to execute using "%~f0?.wsf", that will make cscript handle the file as a .wsf file while the real extension is .cmd

But you should read this for a exhaustive analysis of the problem.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126