使用脚本设置全局环境变量
最近发现有不少人在讨论使用脚本如何设置系统全局长期有效的环境变量的问题,正巧最近也用到了这方面的内容,所以将大家的讨论内容做了一些收集整理,遂成此文。
1、内部工具:regedit /s、echo >> & regedit /s、reg add
2、外部工具:setx、setntenvar、winset
3、其他脚本:vbs
4、自动批处理:autoexec.bat、autoexec.nt
===============================================
1、内部工具:regedit /s、echo >> & regedit /s、reg add
1.1 regedit /s - Windows自带工具
优点:在所有Windows系统下均可使用,可同时操作其他注册表项
缺点:不可动态定义变量,需要重启才能生效,需读取额外的注册表文件
:: dsc.reg 已经存在并可用
REGEDIT /S DSC.REG
1.2 echo >> & regedit /s - Windows自带命令和工具
优点:可动态定义变量,在所有Windows系统下均可使用,可同时操作其他注册表项
缺点:需要重启才能生效,需读写额外的注册表文件
:: dsc.reg 已经存在并可用,修改环境变量Driver为cd变量转化值
ECHO "Driver"="%cd:\=\\%\\sqora32.dll">>DSC.REG
REGEDIT /S DSC.REG
1.3 reg add - Windows 2K/XP/03自带工具
优点:可动态定义变量
缺点:需要重启才能生效,缺省不能在9X下使用
SET ENV_POOL=HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment
REG ADD "%ENV_POOL%" /V PATH /T REG_SZ /D "%cd%;%Path%" /F >nul
2、外部工具:setx、setntenvar、winset
2.1 setx - 来自 2K/XP/03 安装光盘的支持工具包
Version 1.0a (5/31/96)
Gary Milne - Microsoft MCS
优点:可动态定义变量,不需重启立即生效,可动态引用变量,可引用文本内容设置变量
缺点:只能在NTs下使用,不能删除变量而只能置为空值
:: 设置当前用户全局变量
SETX MACHINE COMPAQ
:: 设置系统全局变量
SETX MACHINE "COMPAQ COMPUTER" -m
:: 静态引用其他环境变量(仅引用一次当前会话环境变量)
SETX MYPATH %PATH%
:: 动态引用其他环境变量(总是引用全局环境变量)
SETX MYPATH ~PATH~
:: 使用注册表值设置环境变量
SETX BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber"
:: 使用文件中指定内容设置环境变量(不会立即生效)
SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
2.2 setntenvar - 来自无忧启动论坛(topic=4197) Lx1638(老九)
SetNTEnVar V06.04
优点:可动态定义变量,可定义光驱盘符为环境变量,不需重启立即生效
缺点:只能在NTs下使用
:: 修改变量
SETNTENVAR PATH=%PATH_BAK%
:: 删除变量
SETNTENVAR BAK_PATH=
:: 定义光驱盘符为环境变量CDROM\CDROM0\CDROM1等
SETNTENVAR /FindCDROM
2.3 winset - 来自9x安装盘支持工具包
优点:可动态定义变量,不需重启立即生效
缺点:只能在9X下使用,在NTs下运行出错
:: 修改变量
WINSET PATH=%PATH_BAK%
:: 删除变量
WINSET BAK_PATH=
3、其他脚本:vbs
3.1 vbs - 来自中国DOS联盟论坛(tid=27952) electronixtar
set sysenv=CreateObject("WScript.Shell").Environment("system") '系统环境变量的数组对象
sysenv.Remove("ztest2") '删除变量
sysenv("ztest3")="test value" '添加变量
优点:可动态定义变量,不需重启立即生效
缺点:需要修改系统配置文件,仅对使用command创建的命令行有效
4、自动批处理:autoexec.bat、autoexec.nt
4.1 autoexec.bat - 来自系统盘根目录
优点:可动态定义变量,可在DOS/Windows全系列系统下使用
缺点:需要重启才能生效,需要修改系统文件
echo set path=d:\batch;%path%>> c:\autoexec.bat
在2K/XP/03中是否解析autoexec.bat中的变量与以下注册表项相关
User Key:
Value Name: ParseAutoexec
Data Type: REG_SZ (String Value)
Value Data: (0 = disabled, 1 = enabled)
4.2 autoexec.nt - 来自2K/XP/03系统的%SystemRoot%\system32
优点:可动态定义变量,不需重启立即生效
缺点:需要修改系统文件,仅对2K/XP/03中使用autoexec.nt文件的命令行有效
echo set path=d:\batch;%path%>> %SystemRoot%\System32\autoexec.nt
===============================================
总体而言,系统环境变量在NT系列的注册表下有两个地方:
1、HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
代表系统环境变量空间的注册表项
2、HKEY_CURRENT_USER\Environment
代表当前用户环境变量空间的注册表项
这两个键支下的注册表值分别表示相应的环境变量,直接修改系统空间的变量需要重启才会再全局生效,在用户空间修改的只要注销就可以了。
另外需要注意的是,使用以上方法设置的系统变量,都是无法在批处理或命令行运行的当前会话(窗口)中生效的,所以如果在设置变量后即刻去查询,是不会发生变化的。
解决这个问题的最简单办法是设置系统级变量的同时,使用set设置会话级变量为相同的内容。另外,也可以使用reg query或regedit /e或者setx -k查询相应的注册表值。
===============================================
关于autoexec.nt文件的问题
配置文件是否生效与文件名无关,只有且只要它能被命令行程序正确引用时才生效。
引用配置文件的方法,新建快捷方式,在项目中填写command,按确定后生成程序信息文件(.pif)"MS-DOS 方式",在“属性->程序->高级”中定义初始化文件
但是因为command.com总是使用%SystemRoot%\_default.pif作为程序信息文件,而_default.pif定义了auotexec.nt和config.nt作为初始化配置文件,所以使用command.com打开命令行,总是能成功引用autoexec.nt中设置的环境变量。
============================
相关链接
1、 WinXP SP2支持工具包里提取的setx.exe
http://www.cn-dos.net/forum/viewthread.php?tid=28698
2、setx.vbs重启依然有效的系统环境变量
http://www.cn-dos.net/forum/viewthread.php?tid=27952
3、怎么写一个自动完成系统环境变量添加的脚本
http://www.cn-dos.net/forum/viewthread.php?tid=24114
4、如何用批处理修改系统path环境变量
http://www.cn-dos.net/forum/viewthread.php?tid=25057
5、怎么用批处理设置XP系统的环境变量
http://www.cn-dos.net/forum/viewthread.php?tid=23409
6、MSDOS7.10启动过程中各因素的相互影响
http://www.cn-dos.net/forum/viewthread.php?tid=17107
16:56 2007-3-24
14:42 2007-4-1
Last edited by willsort on 2007-4-2 at 06:04 AM ]
### Using Scripts to Set Global Environment Variables
Recently, I've noticed that many people are discussing how to use scripts to set system global environment variables that are long - term and effective. Coincidentally, I've also used this kind of content recently, so I've collected and sorted out everyone's discussion content, and thus this article is formed.
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
2. External tools: setx, setntenvar, winset
3. Other scripts: vbs
4. Automatic batch processing: autoexec.bat, autoexec.nt
===============================================
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
1.1 regedit /s - Windows built - in tool
Advantages: Can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Cannot dynamically define variables, needs to be restarted to take effect, and needs to read additional registry files
:: dsc.reg already exists and is available
REGEDIT /S DSC.REG
1.2 echo >> & regedit /s - Windows built - in commands and tools
Advantages: Can dynamically define variables, can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Needs to be restarted to take effect, and needs to read and write additional registry files
:: dsc.reg already exists and is available, modify the environment variable Driver to the converted value of the cd variable
ECHO "Driver"="%cd:\=\\%\\sqora32.dll">>DSC.REG
REGEDIT /S DSC.REG
1.3 reg add - Windows 2K/XP/03 built - in tool
Advantages: Can dynamically define variables
Disadvantages: Needs to be restarted to take effect, and cannot be used under 9X by default
SET ENV_POOL=HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment
REG ADD "%ENV_POOL%" /V PATH /T REG_SZ /D "%cd%;%Path%" /F >nul
2. External tools: setx, setntenvar, winset
2.1 setx - Support tool package extracted from 2K/XP/03 installation disc
Version 1.0a (5/31/96)
Gary Milne - Microsoft MCS
Advantages: Can dynamically define variables, takes effect immediately without restart, can dynamically reference variables, and can reference text content to set variables
Disadvantages: Can only be used under NTs, cannot delete variables but can only set them to empty values
:: Set current user global variable
SETX MACHINE COMPAQ
:: Set system global variable
SETX MACHINE "COMPAQ COMPUTER" -m
:: Static reference to other environment variables (only reference the current session environment variables once)
SETX MYPATH %PATH%
:: Dynamic reference to other environment variables (always reference the global environment variables)
SETX MYPATH ~PATH~
:: Use registry value to set environment variable
SETX BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber"
:: Use the content specified in the file to set environment variable (will not take effect immediately)
SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
2.2 setntenvar - From Wuyou Boot Forum (topic = 4197) Lx1638 (Lao Jiu)
SetNTEnVar V06.04
Advantages: Can dynamically define variables, can define the CD - ROM drive letter as an environment variable, takes effect immediately without restart
Disadvantages: Can only be used under NTs
:: Modify variable
SETNTENVAR PATH=%PATH_BAK%
:: Delete variable
SETNTENVAR BAK_PATH=
:: Define CD - ROM drive letter as environment variable CDROM\CDROM0\CDROM1, etc.
SETNTENVAR /FindCDROM
2.3 winset - From 9x installation disc support tool package
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Can only be used under 9X, and runs in error under NTs
:: Modify variable
WINSET PATH=%PATH_BAK%
:: Delete variable
WINSET BAK_PATH=
3. Other scripts: vbs
3.1 vbs - From China DOS Union Forum (tid = 27952) electronixtar
set sysenv=CreateObject("WScript.Shell").Environment("system") 'Array object of system environment variables
sysenv.Remove("ztest2") 'Delete variable
sysenv("ztest3")="test value" 'Add variable
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system configuration file, and is only valid for the command line created by command
4. Automatic batch processing: autoexec.bat, autoexec.nt
4.1 autoexec.bat - From the root directory of the system disk
Advantages: Can dynamically define variables, can be used under all DOS/Windows systems
Disadvantages: Needs to be restarted to take effect, and needs to modify the system file
echo set path=d:\batch;%path%>> c:\autoexec.bat
In 2K/XP/03, whether the variables in autoexec.bat are parsed is related to the following registry item
User Key:
Value Name: ParseAutoexec
Data Type: REG_SZ (String Value)
Value Data: (0 = disabled, 1 = enabled)
4.2 autoexec.nt - From %SystemRoot%\system32 of 2K/XP/03 system
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system file, and is only valid for the command line that uses the autoexec.nt file in 2K/XP/03
echo set path=d:\batch;%path%>> %SystemRoot%\System32\autoexec.nt
===============================================
Generally speaking, there are two places for system environment variables under the NT series registry:
1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Registry item representing the system environment variable space
2. HKEY_CURRENT_USER\Environment
Registry item representing the current user's environment variable space
The registry values under these two keys respectively represent the corresponding environment variables. Directly modifying the variables in the system space requires restarting to take effect globally, and for the variables modified in the user space, it is only necessary to log off.
In addition, it should be noted that the system variables set by the above methods cannot take effect in the current session (window) where the batch processing or command line is run. So if you query immediately after setting the variables, there will be no change.
The simplest way to solve this problem is to set the session - level variable to the same content as the system - level variable while setting the system - level variable. In addition, you can also use reg query or regedit /e or setx -k to query the corresponding registry value.
===============================================
About the issue of the autoexec.nt file
The effectiveness of the configuration file has nothing to do with the file name. It only takes effect when it can be correctly referenced by the command line program.
The method to reference the configuration file is to create a shortcut, fill in command in the item, click OK to generate the program information file (.pif) "MS - DOS mode", and define the initialization file in "Properties -> Program -> Advanced"
But because command.com always uses %SystemRoot%\_default.pif as the program information file, and _default.pif defines auotexec.nt and config.nt as the initialization configuration files, so when using command.com to open the command line, it can always successfully reference the environment variables set in autoexec.nt.
============================
Related links
1. setx.exe extracted from the support tool package of WinXP SP2
http://www.cn-dos.net/forum/viewthread.php?tid=28698
2. setx.vbs system environment variables that still take effect after restart
http://www.cn-dos.net/forum/viewthread.php?tid=27952
3. How to write a script to automatically add system environment variables
http://www.cn-dos.net/forum/viewthread.php?tid=24114
4. How to use batch processing to modify the system path environment variable
http://www.cn-dos.net/forum/viewthread.php?tid=25057
5. How to use batch processing to set the environment variables of the XP system
http://www.cn-dos.net/forum/viewthread.php?tid=23409
6. Mutual influence of various factors during the startup process of MSDOS7.10
http://www.cn-dos.net/forum/viewthread.php?tid=17107
16:56 2007 - 3 - 24
14:42 2007 - 4 - 1
Last edited by willsort on 2007 - 4 - 2 at 06:04 AM ]