China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 05:13
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] Setting global environment variables using scripts View 9,054 Replies 11
Original Poster Posted 2007-04-02 05:49 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
### 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 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2007-04-02 06:07 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Sofa!
Brother wil has finally posted a technical post again. Hehe
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-04-02 06:28 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
"Global environment variables" is it that a variable can be used in any CMD window???
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 4 Posted 2007-04-02 08:49 ·  中国 广东 广州 黄埔区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Floor 5 Posted 2007-04-02 09:03 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
First, admire willsort, then read the post
Floor 6 Posted 2007-04-02 09:08 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
My XP SP2 c:\autoexec.bat content is empty;

It seems that wmic is more convenient for setting global variables;

Because I just happened to be looking at this recently
Floor 7 Posted 2007-04-02 12:04 ·  中国 四川 成都 教育网
中级用户
★★
Credits 346
Posts 103
Joined 2004-04-06 00:00
22-year member
UID 21852
Gender Male
Status Offline
Willsort's post, arrived late, pay homage~~
Floor 8 Posted 2007-04-02 12:32 ·  中国 北京 朝阳区 联通
初级用户
Credits 83
Posts 34
Joined 2006-11-24 10:50
19-year member
UID 71574
Gender Male
Status Offline
The moderator's move is to come up with something practical right away..
Floor 9 Posted 2007-04-03 02:45 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 10 Posted 2007-04-09 00:31 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Flying knife, another flying knife! willsort has been in seclusion for many days. This time, coming back to the arena, he has brought many surprises to everyone again. First, I'll give a thumbs up.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2007-04-09 03:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

I'll also give it a bump.

But the Autoexec.bat of my XP sp2 can't run. The REG_SZ value of ParseAutoexec is also 1. I don't know why Autoexec.bat can't be executed. There are contents in my Autoexec.bat.
Floor 12 Posted 2008-01-25 12:31 ·  中国 信天游
初级用户
Credits 20
Posts 7
Joined 2008-01-22 19:44
18-year member
UID 109289
Gender Male
Status Offline
set sysenv=CreateObject("WScript.Shell").Environment("system") 'Array object for system environment variables
sysenv.Remove("ztest2") 'Delete variable
sysenv("ztest3")="test value" 'Add variable

This is borrowed. Thanks a lot
Forum Jump: