Embedding and Executing JScript JavaScript in a Batch File

Although Batch and JScript JavaScript are developed and released by Microsoft, but they are two different concepts. This free tutorial guides you embed and execute JScript function in a Batch file. Please go to the detailed post for full instructions and codes.


Sampled by © JavaScriptBank.com

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.

  1. rem ( /* 
  2. @echo off 
  3. cscript /nologo /e:javascript hybrid.bat 
  4. goto end 
  5. */ )  
  6.   
  7. // by Jim Lawless  
  8. // This program sample is in the public domain  
  9.   
  10. function rem() {  
  11.    WScript.StdOut.WriteLine("Hello, world!");  
  12. }  
  13.   
  14. /* 
  15. :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.

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com