user541686
user541686

Reputation: 210485

Running Javascript with CScript?

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?

Upvotes: 19

Views: 26490

Answers (7)

robert4
robert4

Reputation: 1102

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.

Upvotes: 12

Gank
Gank

Reputation: 4667

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

Upvotes: 1

PA.
PA.

Reputation: 29339

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.

Upvotes: 0

Steve Black
Steve Black

Reputation: 619

A very simple fix: use assoc.

c:\>assoc .js=JSFile

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

Upvotes: 23

rplantiko
rplantiko

Reputation: 2738

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

Upvotes: 25

user541686
user541686

Reputation: 210485

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

Upvotes: 5

Keltari
Keltari

Reputation: 475

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

regsvr32 %systemroot%\system32\vbscript.dll

Upvotes: -1

Related Questions