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

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

问题描述

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

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"');
}

真的很喜欢它。

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)\wkhtmltopdf\wkhtmltopdf.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 param,你可能会更好地摆脱额外的引号包装整个命令和参数:

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)\wkhtmltopdf\wkhtmltopdf.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天全站免登陆