简体   繁体   English

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

[英]How to Run cmd.exe with parameters from javascript

I am trying to write javascript which should run cmd.exe with a specified command line in it like thisdocs.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.先感谢您。

Maybe you don't have this ActiveX-control installed (or registered) in your computer.也许您的计算机中没有安装(或注册)这个 ActiveX 控件。

WScript.Shell should 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] .让我们以[vDirectory] The documentation says: "The fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used."文档说:“包含由 sFile 指定的文件path of the directory的完全限定path of the directory 。如果未指定此参数,则使用当前工作目录。”

This means that you have an invalid path for this argument (having .cmd.exe at the end of it).这意味着此参数的路径无效(以.cmd.exe结尾)。 Also all examples for creating the ActiveX are like this:同样,创建 ActiveX 的所有示例都是这样的:

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

Notice the lowercase in "shell.application" .注意"shell.application"的小写。

And May12, thank's for asking this. 5 月 12 日,感谢您提出这个问题。 I didn't know about this ActiveX control before, it seems to be very useful to me.我以前不知道这个 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) 最后一个参数是数字类型,而不是字符串。

If I understood correctly, you are only interested in calling another file with parameters.如果我理解正确,您只对使用参数调用另一个文件感兴趣。 This is my example of calling another file from a shortcut or batch file这是我从快捷方式或批处理文件调用另一个文件的示例

If there are no spaces in the path如果路径中没有空格

    mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /c start /max C:\\Windows\\Notepad.exe',0,false);close()"

With spaces in the path.路径中有空格。 The double quote is replaced with #双引号替换为#

    mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /v /c set a=""&call set #=!a:~0,1!&start /max C:\\!#!Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe!#!',1,true);close()"
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可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM