在命令提示符上重新加载Windows环境变量而不关闭它

时间:2015-02-24 14:11:45

标签: windows cmd command prompt

如何在不关闭CMD的情况下在CMD上重新加载Windows环境变量?

1 个答案:

答案 0 :(得分:2)

setlocal
... do part  of the script
endlocal
setlocal
... do part 2 of the script
endlocal
setlocal
... do part 3 of the script
endlocal

可以根据需要添加任意数量的部件。

缺点是您要保存的环境更改将在endlocal命令中丢失。

围绕这个

有两种方法
setlocal
... do part n of the script
endlocal&set keepme=%keepme%&set keepme2=%keepme2%...

setlocal
... do part n of the script
set keepme>>keepmefile.txt
endlocal
for /f "delims=" %%a in (keepmefile.txt) do set %%a

将值keepme的值保存在文件中,然后在执行endlocal后重新加载它们。 (我倾向于在我想为此目的保留的变量名上保留$的前缀)