This question already has an answer here:
这个问题已经有了答案:
- Setting a system environment variable from a Windows batch file? 6 answers
- 从Windows批处理文件中设置系统环境变量?6答案
How do I declare a global variable in a batch script?
如何在批处理脚本中声明全局变量?
Example:
例子:
Test1.bat:
Test1.bat:
set testvar=C:\Windows
echo %testvar%
Now I should be able to use this testvar in other batch script (test2.bat)
现在我应该可以在其他批处理脚本(test2.bat)中使用这个testvar了
Test2.bat:
Test2.bat:
echo %testvar%
Thanks and Regards!
感谢和问候!
4 个解决方案
#1
1
As far as I know the only way to use one variable in multiple batch scripts is to create custom system environment variables. You can find detailed walkthrough on how to do it here: global environment variables
就我所知,在多个批处理脚本中使用一个变量的唯一方法是创建自定义系统环境变量。您可以在这里找到详细的演练:全局环境变量
#2
1
See
看到
SETX /?
from the prompt - and observe the critical point that the change aplies to FUTURE instances of cmd.exe only - not existing
从提示-和观察临界点的变化适合于未来的cmd实例。只有exe -不存在
#3
1
By setting a system environment variable permanently. See here.
通过永久设置一个系统环境变量。在这里看到的。
Applies To: Windows Server 2008, Windows Vista
适用于:Windows Server 2008, Windows Vista
For Windows XP download and install Windows XP Service Pack 2 Support Tools
对于Windows XP下载和安装Windows XP Service Pack 2支持工具。
#4
0
If the 2 batch files are run in the same command process it will work just fine.
如果这两个批处理文件在同一个命令进程中运行,它将会正常工作。
This is test1.bat
这是test1.bat
set testvar="test123"
echo In test1.bat
echo %testvar%
This is test2.bat
这是test2.bat
echo In test2.bat
echo %testvar%
This is output from running test1.bat and then test2.bat
这是运行test1的输出。蝙蝠,然后test2.bat
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
This also works if you call one batch from another batch (because it is the same process)
如果您从另一个批处理调用一个批处理(因为它是相同的过程),这也可以工作。
Change test1.bat to be
test1变化。蝙蝠是
set testvar="test123"
echo In test1.bat
echo %testvar%
call test2.bat
Now output looks like this
输出是这样的
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>call test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
If you want it to be available to different command processes you'll need to look at setx or setting a system variable.
如果希望它对不同的命令进程可用,则需要查看setx或设置系统变量。
#1
1
As far as I know the only way to use one variable in multiple batch scripts is to create custom system environment variables. You can find detailed walkthrough on how to do it here: global environment variables
就我所知,在多个批处理脚本中使用一个变量的唯一方法是创建自定义系统环境变量。您可以在这里找到详细的演练:全局环境变量
#2
1
See
看到
SETX /?
from the prompt - and observe the critical point that the change aplies to FUTURE instances of cmd.exe only - not existing
从提示-和观察临界点的变化适合于未来的cmd实例。只有exe -不存在
#3
1
By setting a system environment variable permanently. See here.
通过永久设置一个系统环境变量。在这里看到的。
Applies To: Windows Server 2008, Windows Vista
适用于:Windows Server 2008, Windows Vista
For Windows XP download and install Windows XP Service Pack 2 Support Tools
对于Windows XP下载和安装Windows XP Service Pack 2支持工具。
#4
0
If the 2 batch files are run in the same command process it will work just fine.
如果这两个批处理文件在同一个命令进程中运行,它将会正常工作。
This is test1.bat
这是test1.bat
set testvar="test123"
echo In test1.bat
echo %testvar%
This is test2.bat
这是test2.bat
echo In test2.bat
echo %testvar%
This is output from running test1.bat and then test2.bat
这是运行test1的输出。蝙蝠,然后test2.bat
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
This also works if you call one batch from another batch (because it is the same process)
如果您从另一个批处理调用一个批处理(因为它是相同的过程),这也可以工作。
Change test1.bat to be
test1变化。蝙蝠是
set testvar="test123"
echo In test1.bat
echo %testvar%
call test2.bat
Now output looks like this
输出是这样的
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>call test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
If you want it to be available to different command processes you'll need to look at setx or setting a system variable.
如果希望它对不同的命令进程可用,则需要查看setx或设置系统变量。