views:

141

answers:

1

In vbscript, how do I run a batch file or command, in the current cmd prompt window,

without starting a new process.

For example. According to script56.chm (the vbscript help apparently) Windows Script Host
Run Method (Windows Script Host)
"Runs a program in a new process"

So if I have code that uses that e.g. a VBS file, and a BAT file. An environment variable g has the value abc g=abc from that command window, The VBS file calls the BAT file with windows scripting host Run. The bat process sets g=z. and finishes.. and the vbs process finishes. The environment variable is left untouched as g=abc.

I know CreateObject("Wscript.Shell").Run "c:\test.bat", 0 starts a new window as is clear when using 1 instead of 0. (since 0 hides the window)

How do I -run the bat file from the vbs, in the same cmd environment that the vbs was called in, so changes affect the cmd environment it was called in? -In the two windows case which this one is at the moment, how do I access the environment of the parent cmd window, from the batch file?

+1  A: 

how do I run a batch file or command, in the current cmd prompt window, without starting a new process?

I don't think you can; your vbscript runs under a script host engine (such as cscript.exe or wscript.exe), and batch files are interpreted by the command interpreter (typically cmd.exe). Both are separate executables and neither is, to my knowledge, available as an in-process library, so you cannot interpret .vbs and .cmd files within the same process. I also highly doubt that the script host engine that is running your VBScript also could run the batch file in its parent cmd.exe - I don't think you can 'inject' a new batch file into a running cmd.exe.

how do I access the environment of the parent cmd window, from the batch file?

Not just access, but change - MSDN's "Changing Environment Variables" is quite explicit on this: "Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process." You are trying to change the environment of the parent, not child, process. (I do wonder what 'directly' means in the context of this quote, though).

I would guess that the reason for this is security; imagine the havoc that could be wreaked if arbitrary processes could (maliciously or accidentally) change the PATH (or COMSPEC) environment variable of a running process, such as your vbscript host engine process - it could fail to launch your bat file entirely, breaking your program.

It would seem that you're out of luck - however, there are lots of other mechanisms for passing information between processes. Here are a couple of suggestions that are fairly simple to implement when talking between a batch file & vbscript, although it's by no means exhaustive:

  • Exit codes
  • Writing to & Parsing the consoleoutput (stdout) or a temp file

If you absolutely need to set environment variables in the parent cmd.exe (and also absolutely need the intermediate step of a vbscript), then you may have to write a wrapper batch file which runs the vbscript, consumes information produced by it and then sets environment variables; because the wrapper cmd is executing in the top-level cmd process, it will be able to change the env vars there.

Footnote: Note that you can change the permanent system/user environment variables (as opposed to process environment variables) from within a VBScript, but I wouldn't recommend this if you are trying to create a transient state; besides this won't affect already-running processes (like the parent cmd.exe) anyway.

bacar
[This solution](http://stackoverflow.com/questions/3737725/how-to-set-environment-variables-in-vbs-that-can-be-read-in-calling-batch-script/3737791#3737791) gives an example of how to write the suggested wrapper batch file.
bacar
I would add.. since it's a cmd window calling a vbs calling a bat(which runs in its own cmd window).. it's more a question of a child process and grandparent process communicating or setting each other's environment variables ;-) but I see.. looks like not possible. interesting to see the different approach though in that question/"solution" you link to.
barlop