使用 WScript.shell activeX 执行命令行 [英] Using a WScript.shell activeX to execute a command line

查看:67
本文介绍了使用 WScript.shell activeX 执行命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WScript.shell activeX 调用 .exe 文件.该文件是 wkhtmltopdf.exe,它用于将 HTML 页面转换为 .pdf.当我只是在代码中调用 C:wkhtmltopdf.exe 时,一切都运行良好.它运行然后正确关闭.但我的问题是您需要从 cmd 运行它,程序名称然后是您正在阅读的 HTML 文件名,然后是您希望将其创建为的 .pdf 名称.

I am working on calling a .exe file with a WScript.shell activeX. The file is wkhtmltopdf.exe and it is used to convert a HTML page to a .pdf. Everything is working well when I am just calling C:wkhtmltopdf.exe in the code. It runs and then closes correctly. But my issue is you need to run it from cmd with the program name then the HTML file name you are reading followed by the .pdf name you want it to be created as.

例如:

c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf

这将调用 wkhtmltopdf.exe,读取 c:PDFTestPage.html,然后创建 c:TEST.pdf.当我在 cmd 中输入它时工作正常.

This will call wkhtmltopdf.exe, read c:PDFTestPage.html, then create c:TEST.pdf. Works fine when I type it into cmd.

有谁知道一个 ActiveX,它不仅会运行和 .exe,还会实际执行命令行?

Does anyone know an activeX that will not just run and .exe but actually execute a command line?

这是我目前使用的代码.

Here is my code that I am currently using.

function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:wkhtmltopdf.exe"');
}

真的很喜欢下面的内容.

Would really like it to be the following.

function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf"');
}

另附注.出于某种原因,我无法从绝对路径启动 .exe.我必须移动到该目录,然后输入 wkhtmltopdf.exe.填充路径为:

Also side note. For some reason I cant launch the .exe from an absolute path. I have to move to the directory and then just type in wkhtmltopdf.exe. The fill path is:

C:Program Files (x86)wkhtmltopdfwkhtmltopdf.exe

我真的只使用 UNIX,所以我不确定路径中的空格.我可以用空格做一个 chdir,但在执行它时我不能使用填充路径.任何信息都有帮助.先感谢您.

I really only work with UNIX so I'm not sure about spaces in the path. I can do a chdir with the spaces but I cant use the fill path when executing it. Any information would be helpful. Thank you in advance.

推荐答案

根据以下内容:

http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx

您应该能够将命令作为 strCommand 参数的一部分直接传递,最好去掉包裹整个命令和参数的额外引号:

You should be able to pass the commands directly as part of the strCommand param, you'd probably be better off getting rid of the extra quotes wrapping the entire command and arguments:

function callShellApplication(){
  var objShell = new ActiveXObject("WScript.shell");
  objShell.run('c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf');
}

此外,您应该能够通过将每个项目括在引号中来处理路径中的空格,如下所示:

Also you should be able to handle spaces in paths by wrapping each item in quotes, like so:

function callShellApplication(){
  var objShell = new ActiveXObject("WScript.shell");
  objShell.run('"C:Program Files (x86)wkhtmltopdfwkhtmltopdf.exe" "c:PDFTestPage.html" "c:TEST.pdf"');
}

您还应该记住是否要bWaitOnReturn,以及您需要哪种intWindowStyle(某些可执行文件可能受益于特定样式).

You should also keep in mind whether you want to bWaitOnReturn or not, and which intWindowStyle you need (some executables may benefit from a particular style).

也只是作为一个警示说明—我已经有一段时间没有使用 WScript.shell —但是您可能需要在路径中转义反斜杠.因此,路径可能需要如下所示:

Also just as a cautionary note — it's been a while since I've used WScript.shell — but you may need to escape your backslashes in your paths. So a path may need to look like the following:

objShell.run('"C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe"');

这篇关于使用 WScript.shell activeX 执行命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆