6

How does Windows know that this is JSCRIPT?

@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/
x = WScript.Arguments
Yr = x(0) ; Mo = x(1)

YS = "JanFebMarAprMayJunJulAugSepOctNovDec"
MN = Mo<1 || Mo>12 ? Mo : YS.substr(3*Mo-3, 3) // Month Name
WScript.echo(" ", Yr, "         ", MN)
WScript.echo(" Mo Tu We Th Fr Sa Su")
WD = new Date(Yr, Mo-1, 1).getDay() ;
if (WD==0) WD = 7 // Week Day Number of 1st
LD = new Date(Yr, Mo, 0).getDate() // Last Day of month
Wk = "" ; for (D=1 ; D < WD ; D++) Wk += "   "

for (D=1 ; D<=LD ; D++) {
  Wk = Wk + " " + (D<10 ? "0"+D : D) ; WD++
  if ((WD==8) || (D==LD)) { WScript.echo(Wk) ; WD = WD-7 ; Wk = "" }
  }

WScript.echo("        ------       ")

Sample usage:

C:\batch>calendar.cmd 2014 7
  2014           Jul
 Mo Tu We Th Fr Sa Su
    01 02 03 04 05 06
 07 08 09 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31
        ------
indiv
  • 17,306
  • 6
  • 61
  • 82
Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
  • 1
    I posted this a) because it is not well known among Windows programmers, and b) for a beginner it is better to learn Javascript than to wrestle with either obscure batch scripts or the syntactically atrocious Powershell. – Michael Dillon Feb 15 '11 at 02:36
  • I'm not usually anal about things like this, but I don't think this is an appropriate "question" for StackOverflow (at least according to my interpretation of the ground rules). – Luke Feb 15 '11 at 03:56
  • 1
    This question is about writing code to accomplish something specific and that is what programming is all about. Not every question will have useful information for every reader which is why we have tags. – Michael Dillon Feb 15 '11 at 22:00
  • I meant it wasn't appropriate in that it is not a real question. Stack Overflow is about asking for and receiving help, not posting tips and tricks (unless the question calls for that). While perhaps useful information, I think it would have been better suited to a different forum. – Luke Feb 19 '11 at 18:23
  • thanks for posting! Just what I needed for my project. QUite amazing how obscure some bat file command can be. This has been revealed :) JS is it for me! – The Coordinator Nov 29 '14 at 07:56
  • @Luke no, the purpose of SO is not as narrow as you make it. Users are encouraged to answer their own questions in order to spread information. – reformed Sep 27 '21 at 16:25

1 Answers1

7

It is quite simple really. The first line is valid batch file language for setting a shell variable and everything after the last space is ignored. It is also valid JSCRIPT for setting compile time variables, and the last two chars begin a Javascript comment which causes the rest of the batch file language lines to be ignored.

The cscript line causes the same file %0 to be executed by JSCRIPT with the same arguments %*. Then the batch goto statement uses :eof which is a builtin label that represents the end of file.

If you are a beginner, and you spend your time learning how to apply JSCRIPT to the problems of Windows shell scripting, you can reapply your Javascript knowledge in the browser with web applications, with Windows HTML apps (.HTA), and even in shell scripting on Unix platforms that have Rhino or node.js installed.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
  • Just to be clear: `@set @junk=1 /*` In batch script, this assigns all four characters to the variable @junk [one, space, front-slash, asterisk]. (Not that it really matters to this question.) Also, this explains how that line is handled from a JScript perspective: https://msdn.microsoft.com/en-us/library/thak6fez(v=vs.84).aspx – Patrick May 18 '15 at 20:17