Running Javascript with CScript?
Asked Answered
I

7

18

I'm trying to learn how to run Javascript (instead of VBScript) using CScript.exe, but when I try, I get an error:

cscript temp.js
Input Error: There is no script engine for file extension ".js".

I thought this is possible -- any idea what's wrong?

Idoux answered 25/8, 2011 at 9:33 Comment(0)
I
5

It turns out that the .js extension wasn't associated with JSFile in the registry. Doing so fixed the problem.

Idoux answered 28/8, 2011 at 1:53 Comment(0)
B
25

Setting the registry with regsvr32 did not work for me. Also, I don't want it, since I want my .js to be linked with a plain text editor.

But there is a command line option //E for cscript which makes the job:

cscript //E:jscript hello.js
Bordure answered 22/6, 2012 at 13:0 Comment(1)
This answer is best if you don't want to replace your file association.Hoard
T
23

A very simple fix: use assoc.

c:\>assoc .js=JSFile

(Mine had become associated with a text editor at some point.)

Tuneberg answered 17/7, 2012 at 3:41 Comment(1)
I'd associated .js with text editor on purpose, its very easy to confuse this error with something else.Turk
S
11

It's worth to mention that rplantiko's solution works even if the extension of the filename is not .js. This allows for putting .js code into a .cmd file and running as a batch, forming a single-file solution that is fully portable without preliminary steps (like assoc).

For example, if you create a test.cmd file with the following content, you'll be able to run it by simply clicking on it in Explorer, or by drag&drop another file over its icon:

@if (@CodeSection == @Batch) @then
  @cscript //Nologo //E:jscript "%~f0" "test arg" %* & pause & goto :eof
@end
WScript.Echo("hello world");
for (var i = 0, n = WScript.Arguments.Length, args = []; i < n; ++i)
    args.push(WScript.Arguments(i));
WScript.Echo("arguments: " + args.join(","));

The lines between @then ... @end are batch commands interpreted by cmd.exe. The last command is goto :eof to skip the rest of the file. The lines after @end are interpreted by cscript.exe.

Salmagundi answered 17/4, 2015 at 11:7 Comment(2)
Very nice! :) Probably 99% people only know and use the very basics of cmd scripts[1], so this adds a massive amount of power for very little fuss. Definitely one for the snippets collection, thanks. [1] Ok, don't flame me, that's off the top of my head.Crist
@Salmagundi This is very nice and should get more attention, even today when PowerShell is dominating batch. I know batch extremely well but did not know this trick and still having some trouble understanding precisely how it works for both cmd and cscript. Will it get by VBScript? (I'm going to test) Well done. If there a web site or some place with more info or tricks like this?Unconventionality
I
5

It turns out that the .js extension wasn't associated with JSFile in the registry. Doing so fixed the problem.

Idoux answered 28/8, 2011 at 1:53 Comment(0)
S
1

assoc .js=JSfile ftype jsfile=C:\Windows\System32\Cscript.exe "%1" %*

Spectroscope answered 16/12, 2013 at 20:43 Comment(0)
C
0

Had this problem, too, and I solved ...

1.- locate wsh.inf, the installation file for windows scripting host in %windir%\inf

2.- right-click on wsh.inf and choose install.

Claireclairobscure answered 7/1, 2013 at 15:26 Comment(0)
S
-1

You should be able to run this command to fix the error:

regsvr32 %systemroot%\system32\vbscript.dll

Splanchnic answered 25/8, 2011 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.