问题描述
我有一个简单的问题:
从 Windows 批处理 (.bat) 脚本执行单个 WshShell 命令的最佳方法是什么?
What's the best way to execute a single WshShell command from a Windows batch (.bat) script?
(希望它不是用 VB 代码创建新文件)
推荐答案
您可以通过 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 非常容易.请参阅https://stackoverflow.com/a/5656250/1012053.我使用了该技术的一个非常细微的变化.
Embedding JScript within batch is quite easy. See https://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.
Batch 将第一行视为一个简单的 IF 命令,该命令的计算结果始终为 false,因此它永远不会执行不存在的 @end 命令,并且不会造成任何伤害.以下几行都是正常的批处理代码,直到 exit/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 会忽略以下批处理代码,因为它都是注释的一部分.注释以 */
结尾,然后是普通的 JScript 代码.
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.
唯一可能失败的是,如果您的批处理代码中必须包含 */
,因为这会过早终止 JScript 注释.但这可以通过在 *
和 /
之间放置一些在批量解析后消失的东西来解决.如果代码没有被引用,那么您可以简单地转义斜杠,如下所示:*^/
.如果代码被引用,那么你可以扩展一个未定义的变量:*%=%/
.保证不会定义名为 =
的变量.
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 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!