How does this Windows batch file run embedded javascript? - Stack Overflow

How does Windows know that this is JSCRIPT?@set @junk=1 *@echo offcscript nologo E:jscript %0 %*

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
        ------

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
        ------
Share Improve this question edited Jul 8, 2014 at 22:51 indiv 17.9k6 gold badges64 silver badges83 bronze badges asked Feb 15, 2011 at 2:31 Michael DillonMichael Dillon 32.4k7 gold badges75 silver badges107 bronze badges 6
  • 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 Commented Feb 15, 2011 at 2: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 Commented Feb 15, 2011 at 3:56
  • 1 This question is about writing code to acplish 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 Commented Feb 15, 2011 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 Commented Feb 19, 2011 at 18:23
  • thanks for posting! Just what I needed for my project. QUite amazing how obscure some bat file mand can be. This has been revealed :) JS is it for me! – The Coordinator Commented Nov 29, 2014 at 7:56
 |  Show 1 more ment

1 Answer 1

Reset to default 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 pile time variables, and the last two chars begin a Javascript ment 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745437418a4627672.html

相关推荐

  • How does this Windows batch file run embedded javascript? - Stack Overflow

    How does Windows know that this is JSCRIPT?@set @junk=1 *@echo offcscript nologo E:jscript %0 %*

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信