用 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
Running Javascript with CScript?
提问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
回答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 //E
for cscript which makes the job:
但是//E
cscript有一个命令行选项可以完成这项工作:
cscript //E:jscript hello.js
回答by Steve Black
回答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.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:
例如,如果您创建一个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
... @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.
@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.inf
windows脚本主机的安装文件%windir%\inf
2.- right-click on wsh.inf
and 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