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-07-31 23:33
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to write a script to automatically add system environment variables View 8,735 Replies 19
Original Poster Posted 2006-10-24 22:21 ·  中国 江苏 无锡 联通
新手上路
Credits 6
Posts 2
Joined 2006-10-24 02:51
19-year member
UID 67951
Gender Male
Status Offline
Our company has a software that needs to add environment variables to run after installation. Because there are too many, it's a bit annoying. I want to write a script to automatically complete the addition of system environment variables. Does anyone know how?
Floor 2 Posted 2006-10-24 22:38 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Under 2003/Vista, there is a setx, but under XP, you can only change the registry through reg/regedit.

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 3 Posted 2006-10-25 08:03 ·  中国 江苏 无锡 联通
新手上路
Credits 6
Posts 2
Joined 2006-10-24 02:51
19-year member
UID 67951
Gender Male
Status Offline
Oh, I see! Thanks! It seems that the guys have already enjoyed the new operating system!
Floor 4 Posted 2006-10-25 08:11 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

set path="c:\windows\help";%path%

Among them, "c:\windows\help" is just an example. You can add many paths to it, but don't forget to add ";" in the middle to separate each path. This can't set the environment variable permanently, it can only be used temporarily. To make the set environment variable effective for a long time, you can only manually modify the system environment variables or modify the registry.
Floor 5 Posted 2006-10-25 08:52 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Too many? Are there many paths?
Floor 6 Posted 2006-10-25 09:26 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Using VBS

dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
wsh.Environment("system").Item("<Variable Name>")="<Value>"

Or using REG

set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
::System environment variables
::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
::User environment variables
::HKEY_CURRENT_USER\Environment
reg add "%regpath%" /v "<name>" /d "<data>"

Just replace the content in <> with the name and value you need to define.
QQ:366840202
http://chenall.net
Floor 7 Posted 2006-12-27 08:25 ·  中国 北京 光环新网
新手上路
Credits 12
Posts 5
Joined 2006-12-27 08:04
19-year member
UID 74686
Gender Male
Status Offline
If I want to set the value of path, how to use reg add to do it? That is, without changing the original value of path, add the value I need. Can you write down specifically how to do it? Thanks a lot!
Floor 8 Posted 2006-12-27 21:24 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
The path is represented as binary in the registry, which is not very easy to operate.
Floor 9 Posted 2006-12-27 21:49 ·  中国 北京 光环新网
新手上路
Credits 12
Posts 5
Joined 2006-12-27 08:04
19-year member
UID 74686
Gender Male
Status Offline
Is it that troublesome? Just as the 6th floor said, use:
set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
reg add "%regpath%" /v "Path" /d "<data>"

But this will overwrite the original value of Path.
I tried writing it like this:
set  regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set  Path=<data>;%Path%
reg add "%regpath%" /v "Path" /d "%Path%"

But there was an error: too many command-line .......

My idea is to export the original value, modify it and then import it back, but I'm not familiar with DOS commands...
Floor 10 Posted 2006-12-27 22:16 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
I found a very good method. That is to use SET PATH in AUTOEXEC.BAT. I don't know the principle very well, but it has been tested successfully, and I also feel very strange. It is said that WINNT doesn't use AUTOEXEC.BAT anymore, but the fact is not like that. Everyone do a small experiment to see if it can succeed on your computer.

Appending PATH paths without overwriting the original paths: If your system disk doesn't have AUTOEXEC.BAT, then create one, and append the content SET PATH=D:\;E:\;F:\;G:\; in it.

Restart the computer, and the miracle happens. Use set path to check your PATH variable, and it turns out that the variable set in AUTOEXEC.BAT is added to the PATH variable. It's really strange.
Floor 11 Posted 2006-12-27 22:17 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Posts where the test is successful. The path above can be changed to any path you need, but you must pay attention that each path must be separated by a semicolon.
Floor 12 Posted 2006-12-27 22:23 ·  中国 北京 光环新网
新手上路
Credits 12
Posts 5
Joined 2006-12-27 08:04
19-year member
UID 74686
Gender Male
Status Offline
Thanks to hunrybecky, but is it really impossible to add Path by modifying the registry? Looking forward to help...
Floor 13 Posted 2006-12-27 22:30 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Yes, I also thought of adding it through the registry at first, but then I thought that because it's using expandable characters (I said binary at first, but later found out it's expandable string, sorry), the exported registry is a long string of numbers, so I gave up, and accidentally discovered the method of using AUTOEXEC.BAT just now. Now the method using the registry has also been successful. Actually, expandable characters don't affect, you can directly use the general REG_SZ. The method is as follows:

@ECHO OFF
SET SE=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
REG ADD "%SE%" /V Path /t REG_EXPAND_SZ /d %PATH%G:\ACCESS; /f
@ECHO ON

Note that %SPATH%G:\ACCESS; Note that there is no ; between the original variable and the added one
Floor 14 Posted 2006-12-27 22:30 ·  中国 河南 许昌 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
XP should not execute AUTOEXEC.BAT, but will parse some variables in AUTOEXEC.BAT, and the path is one of them.
Floor 15 Posted 2006-12-27 22:33 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Originally posted by zh159 at 2006-12-27 22:30:
XP should not execute AUTOEXEC.BAT, but will parse some variables in AUTOEXEC.BAT, and the path is one of them.



Yes, from this example, it can illustrate your point, and what's more interesting is that the variables set in AUTOEXEC.BAT are added to the existing variables instead of replacing them, so using this thing, we should be able to do many interesting things.
Forum Jump: