Javascript 如何使用来自javascript的参数运行cmd.exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10186813/
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 00:13:57  来源:igfitidea点击:

How to Run cmd.exe with parameters from javascript

javascriptcommand-linecmdshellexecute

提问by May12

I am trying to write javascript which should run cmd.exe with a specified command line in it like this docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/edit:

我正在尝试编写 javascript ,它应该像这样docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/edit一样运行带有指定命令行的 cmd.exe :

I prepare a code after reading shellexecute method on microsoft site:

我在微软网站上阅读shellexecute方法后准备了一个代码:

var objShell = new ActiveXObject("Shell.Application");
        objShell.ShellExecute("cmd.exe", "C: cd C:\pr main.exe blablafile.txt auto", "C:\WINDOWS\system32", "open", "1");

but it does not insert command line in cmd.exe.

但它不会在 cmd.exe 中插入命令行。

Could anybody help me? Thank you in advance.

有人可以帮我吗?先感谢您。

采纳答案by Teemu

Maybe you don't have this ActiveX-control installed (or registered) in your computer.

也许您的计算机中没有安装(或注册)这个 ActiveX 控件。

WScript.Shellshould be found in every Windows:

WScript.Shell应该在每个 Windows 中找到:

var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");

If there are spaces in commands to run, you need to use double quotes.

如果 中有空格commands to run,则需要使用双引号。

Edit

编辑

The content below is mainly from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx

以下内容主要来自MSDN:http: //msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v= vs.85).aspx

iRetVal = Shell.ShellExecute(
  sFile,
  [ vArguments ],
  [ vDirectory ],
  [ vOperation ],
  [ vShow ]
)

Let's take [vDirectory]. The documentation says: "The fully qualified path of the directorythat contains the file specified by sFile. If this parameter is not specified, the current working directory is used."

让我们拿[vDirectory]。文档说:“path of the directory包含由 sFile 指定的文件的完全限定。如果未指定此参数,则使用当前工作目录。”

This means that you have an invalid path for this argument (having .cmd.exeat the end of it). Also all examples for creating the ActiveX are like this:

这意味着您对此参数的路径无效(.cmd.exe在它的末尾)。同样,创建 ActiveX 的所有示例都是这样的:

var objShell = new ActiveXObject("shell.application");

Notice the lowercase in "shell.application".

注意 中的小写字母"shell.application"

And May12, thank's for asking this. I didn't know about this ActiveX control before, it seems to be very useful to me.

5 月 12 日,感谢您提出这个问题。我以前不知道这个 ActiveX 控件,它似乎对我很有用。

EDIT II

编辑二

But have you understood it? Your example works perfect in my app:

但是你明白了吗?您的示例在我的应用程序中完美运行:

objShell.ShellExecute("cmd.exe", "cd C: C:\cd c:\ext_file main.exe test.txt", "C:\WINDOWS\system32", "open", 1);

With three exceptions:

除了三个例外:

1) The one I mentioned early in this answer about the path

1)我在这个关于路径的答案中早些时候提到的那个

2) Escaped \used also in arguments.

2) 转义\也用于参数。

3) The last argument is type of number, not a string.

3) 最后一个参数是数字类型,而不是字符串。

回答by Editor

var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\pr main.exe blablafile.txt auto", "C:\WINDOWS\system32", "open", "1"); 

is usable

可用