Embedding JavaScript in a Batch File

Originally published on: Fri, 07 Aug 2009 12:05:51 +0000

The program below is both a batch file and a JavaScript WSH console file. The dual nature of the file is achieved via JavaScript block comments and the strategy by which the command interpreter searches for target labels in a batch file.

Save the file below as hybrid.bat.

rem ( /*
@echo off
cscript /nologo /e:javascript hybrid.bat
goto end
*/ )

// by Jim Lawless
// This program sample is in the public domain

function rem() {
   WScript.StdOut.WriteLine("Hello, world!");

:end */

The first line of code is benign for a batch file; the REM command is a single line remark, so it ignores all text after it.

The first line is constructed so that syntactically, it is also the beginning of a valid JavaScript function call for a function named rem().

Immediately after the first left-parenthesis in the call, a block-comment is used to mask the next few lines from the WSH JavaScript interpreter. Batch commands follow on the next three lines, ending with a goto.

The batch file itself invokes the WSH console interpreter cscript.exe passing in the name of the file ( hybrid.bat ) itself. After invocation of the script as a JavaScript file, the batch file portion attempts to transfer control to a label named end.

When the command processor attempts to transfer control to a label via the goto verb, each line of the batch file is examined sequentially until a matching label is found. Any syntax errors encountered ( the lines of JavaScript text ) are ignored.


About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged . Bookmark the permalink.

2 Responses to Embedding JavaScript in a Batch File

  1. Ankit says:

    Hello Jim, Please can you have a look at my below concern

    am traversing a website via batchfile to do a specific job. I have calculated the number of tabs required to reachout to specific element in website. But sometimes (due to latency) I find my batch file reaching the wrong element. My question is – Is there a way my batch file can read the onfocus element ?

    EDITING Question to add ssimple example code –

    @if (@CodeSection == @Batch) @then

    @echo off

    rem Use %SendKeys% to send keys to the keyboard buffer
    set SendKeys=CScript //nologo //E:JScript “%~F0”

    rem Start the other program in the same Window
    start /d “C:\Program Files\Internet Explorer” IEXPLORE.EXE http://www.google.com

    %SendKeys% “{F5}”
    timeout /t 04

    %SendKeys% “{TAB}”
    %SendKeys% “{TAB}”
    %SendKeys% “{TAB}”

    timeout /t 10
    goto :EOF


    // JScript section

    var WshShell = WScript.CreateObject(“WScript.Shell”);

    Above Example.bat file takes the control to “Advanced Search” button on google

    Now before i add further code on my batch file I want to validate the control of my batch file is actually on “Advanced Search” button during runtime . How can i moddify the code to read active element ?

    • Jim Lawless says:

      You might want to look into using SendKeys to place a bookmarklet into the address bar of your browser and then send and ENTER.

      A bookmarklet is a snippet of JavaScript code prefixed by the protocol “javascript:” that will execute JS code in the context of the document(s) that your browser is rendering. Try entering this in your address bar:


      It’s a little tricky to write, but you would then have the access to the page elements that you need from the JS code in the address bar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s