如何在vbs中设置可以在调用批处理脚本时读取的环境变量 [英] How to set environment variables in vbs that can be read in calling batch script

查看:53
本文介绍了如何在vbs中设置可以在调用批处理脚本时读取的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用 vbscript 文件的批处理文件.我试图让 vbscript 文件更改稍后在调用 vbscript 文件的批处理文件中使用的环境变量.

I have a batch file that calls a vbscript file. I am trying to have the vbscript file change an environment variable that is later used in the batch file that calls the vbscript file.

这里是文件的片段.

父.bat

Set Value="Initial Value"
cscript Child.vbs
ECHO Value = %VALUE%

Child.vbs

Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "Process" )
wshSystemEnv("VALUE") = "New Value"

推荐答案

你不能.一个进程可以将环境变量传递给子进程,但不能传递给它的父进程——在这种情况下,父进程是 cmd.exe,它运行着你的 Parent.bat 文件.

You can't. A process can pass environment variables to child processes, but not to its parent - and in this case the parent is cmd.exe, which is running your Parent.bat file.

当然还有其他方式将信息传递回父批处理文件 - 输出到标准输出或文件是一种显而易见的方式,例如

There are of course other ways to communicate information back to the parent batch file - outputting to stdout or a file is an obvious way, e.g.

== Child.vbs ===
WScript.echo "New Value"

== Parent.cmd ===
for /f "tokens=*" %%i in ('cscript //nologo child.vbs') do set Value=%%i
echo %Value%

这篇关于如何在vbs中设置可以在调用批处理脚本时读取的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆