从批处理脚本执行的WshShell命令 [英] execute WshShell command from a batch script
问题描述
我有一个简单的问题:
什么是从Windows批处理执行一个命令的WshShell的最佳方式(蝙蝠)脚本?
What's the best way to execute a single WshShell command from a Windows batch (.bat) script?
的(希望它不创造用VB code上新建一个文件)的
推荐答案
您可以通过VBScript或Jscript脚本访问的WshShell。既可以嵌入在批处理文件中,但JScript是干净多了。
You can access WshShell via VBScript or Jscript. Both can be embedded within a batch file, but JScript is much cleaner.
大多数人写的临时VBS文件的批处理内执行的VBScript。但它有可能做到这一点没有一个临时文件。请参见是否有可能嵌入和不使用临时文件的批处理文件中执行的VBScript?的各种选项。
Most people execute VBScript within batch by writing a temporary VBS file. But it is possible to do it without a temporary file. See Is it possible to embed and execute VBScript within a batch file without using a temporary file? for various options.
批次中嵌入JScript是很容易的。请参见 http://stackoverflow.com/a/5656250/1012053 。我使用该技术的一个非常微小的变化。
Embedding JScript within batch is quite easy. See http://stackoverflow.com/a/5656250/1012053. I use a very slight variation of that technique.
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
:: ******* Begin batch code *********
@echo off
:: Your batch logic goes here
:: At any point you can execute the following to access your JScript
cscript //E:JScript //nologo "%~f0" yourJscriptParametersGoHere
:: Be sure to terminate your script so that
:: it does not fall through into the JScript code
exit /b
********* Begin JScript code **********/
var WshShell=WScript.CreateObject("WScript.Shell")
/* do whatever with your WshShell object */
说明:
该技术的关键是第一行。它必须是一个具有有效的语法为JScript和批次线。
The key to the technique is the first line. It must be a line that has valid syntax for both JScript and batch.
批量看到第一行作为一个简单的IF命令总是计算为false,那么它永远不会执行不存在的@end命令,没有造成任何伤害。下面的几行都是正常批次code到达出口/ b,直到,在这一点批处理终止,其余行被忽略。
Batch sees the first line as a simple IF command that always evaluates to false, so it never executes the non-existent @end command, and no harm is done. The following lines are all normal batch code until exit /b is reached, at which point batch processing terminates and the remaining lines are ignored.
的JScript看到的第一行作为空条件编译块,随后是多行注释的开头。 JScript中忽略了下面的批处理code,因为它是注释的一部分。注释与 * /
,然后正常的JScript code如下:
JScript sees the first line as an empty conditional compilation block, followed by the beginning of a multi-line comment. JScript ignores the following batch code because it is all part of the comment. The comment ends with */
, and then normal JScript code follows.
这可能失败的唯一的事情是,如果您的批处理code必须在它 * /
,因为这会pmaturely终止JScript的评论$ P $。但是,这可以通过将之间的事情来解决 *
和 /
批量解析后消失。如果code未报价,那么你可以简单地逃脱斜线如下: * ^ /
。如果code被引用,那么你可以扩展一个未定义的变量: *%=%/
。命名的变量 =
保证不会被定义。
The only thing that could fail is if your batch code must have */
within it, because that would terminate the JScript comment prematurely. But that can be solved by putting something between the *
and /
that disappears after batch parsing. If the code is not quoted, then you can simply escape the slash as follows: *^/
. If the code is quoted, then you can expand an undefined variable: *%=%/
. A variable named =
is guaranteed not to be defined.
这篇关于从批处理脚本执行的WshShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!