用 CScript 运行 Javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7189269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:15:54  来源:igfitidea点击:

Running Javascript with CScript?

windows-7windowsjavascriptwsh

提问by user541686

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

我正在尝试学习如何使用 CScript.exe 运行 Javascript(而不是 VBScript),但是当我尝试时,出现错误:

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

I thoughtthis is possible -- any idea what's wrong?

认为这是可能的 - 知道有什么问题吗?

采纳答案by user541686

It turns outthat the .jsextension wasn't associated with JSFilein the registry. Doing so fixed the problem.

原来.js扩展名是没有关联JSFile在注册表中。这样做解决了问题。

回答by rplantiko

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.

使用 regsvr32 设置注册表对我不起作用。另外,我不想要它,因为我希望我的 .js 与纯文本编辑器链接。

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

但是//Ecscript有一个命令行选项可以完成这项工作:

cscript //E:jscript hello.js

回答by Steve Black

A very simple fix: use assoc.

一个非常简单的修复:使用assoc

c:\>assoc .js=JSFile

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

(我的已经在某个时候与文本编辑器相关联。)

回答by robert4

It's worth to mention that rplantiko's solutionworks 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).

值得一提的是,即使文件名的扩展名不是 .js ,rplantiko 的解决方案也能正常工作。这允许将 .js 代码放入 .cmd 文件并作为批处理运行,形成一个完全可移植的单文件解决方案,无需准备步骤(如 assoc)。

For example, if you create a test.cmdfile 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:

例如,如果您创建一个test.cmd包含以下内容的文件,您只需在资源管理器中单击它,或将另一个文件拖放到其图标上即可运行它:

@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... @endare batch commands interpreted by cmd.exe. The last command is goto :eofto skip the rest of the file. The lines after @endare interpreted by cscript.exe.

@then...之间的行@end是由 cmd.exe 解释的批处理命令。最后一个命令是goto :eof跳过文件的其余部分。后面的行@end由 cscript.exe 解释。

回答by Gank

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

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

回答by PA.

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

也有这个问题,我解决了...

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

1.- 找到wsh.infwindows脚本主机的安装文件%windir%\inf

2.- right-click on wsh.infand choose install.

2.- 右键单击wsh.inf并选择install

回答by Keltari

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

您应该能够运行此命令来修复错误:

regsvr32 %systemroot%\system32\vbscript.dll

regsvr32 %systemroot%\system32\vbscript.dll