Running Javascript with CScript? Running Javascript with CScript? windows windows

Running Javascript with CScript?


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


A very simple fix: use assoc.

c:\>assoc .js=JSFile

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


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

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@endWScript.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.