Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

windows - execute WshShell command from a batch script

I've got a simple question:

What's the best way to execute a single WshShell command from a Windows batch (.bat) script?

(hopefully it's not creating a new file with VB code)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can access WshShell via VBScript or Jscript. Both can be embedded within a batch file, but JScript is much cleaner.

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.

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 */

Explanation:

The key to the technique is the first line. It must be a line that has valid syntax for both JScript and batch.

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 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...