![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-08 22:15 |
47,811 topics / 349,895 posts / today 0 new / 48,253 members |
| DOS批处理 & 脚本技术(批处理室) » [Discussion] Setting global environment variables using scripts |
| Printable Version 9,201 / 11 |
| Floor1 willsort | Posted 2007-04-02 05:49 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
### 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 ] |
|
| Floor2 ccwan | Posted 2007-04-02 06:07 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Sofa!
Brother wil has finally posted a technical post again. Hehe |
|
| Floor3 huzixuan | Posted 2007-04-02 06:28 |
| 高级用户 Posts 219 Credits 537 From 芜湖 | |
|
"Global environment variables" is it that a variable can be used in any CMD window???
|
|
| Floor4 everest79 | Posted 2007-04-02 08:49 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
Big beard, I also wrote one
http://www.cn-dos.net/forum/viewthread.php?tid=28204&fpage=1&highlight=%2Beverest79 |
|
| Floor5 bjsh | Posted 2007-04-02 09:03 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
First, admire willsort, then read the post
|
|
| Floor6 bjsh | Posted 2007-04-02 09:08 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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 |
|
| Floor7 estar | Posted 2007-04-02 12:04 |
| 中级用户 Posts 103 Credits 346 | |
|
Willsort's post, arrived late, pay homage~~
|
|
| Floor8 20080610 | Posted 2007-04-02 12:32 |
| 初级用户 Posts 34 Credits 83 | |
|
The moderator's move is to come up with something practical right away..
|
|
| Floor9 vkill | Posted 2007-04-03 02:45 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
| Floor10 namejm | Posted 2007-04-09 00:31 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor11 lxmxn | Posted 2007-04-09 03:25 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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. |
|
| Floor12 popfrog | Posted 2008-01-25 12:31 |
| 初级用户 Posts 7 Credits 20 | |
|
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 |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |