![]() |
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-09-15 12:21 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » How to write a script to automatically add system environment variables |
| Printable Version 9,106 / 19 |
| Floor1 ffo1984 | Posted 2006-10-24 22:21 |
| 新手上路 Posts 2 Credits 6 | |
|
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?
|
|
| Floor2 electronixtar | Posted 2006-10-24 22:38 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
Under 2003/Vista, there is a setx, but under XP, you can only change the registry through reg/regedit.
|
|
| Floor3 ffo1984 | Posted 2006-10-25 08:03 |
| 新手上路 Posts 2 Credits 6 | |
|
Oh, I see! Thanks! It seems that the guys have already enjoyed the new operating system!
|
|
| Floor4 lxmxn | Posted 2006-10-25 08:11 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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. |
|
| Floor5 vkill | Posted 2006-10-25 08:52 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
Too many? Are there many paths?
|
|
| Floor6 chenall | Posted 2006-10-25 09:26 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
Using VBS
Or using REG Just replace the content in <> with the name and value you need to define. |
|
| Floor7 alucifer | Posted 2006-12-27 08:25 |
| 新手上路 Posts 5 Credits 12 | |
|
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!
|
|
| Floor8 HUNRYBECKY | Posted 2006-12-27 21:24 |
| 银牌会员 Posts 442 Credits 1,179 | |
|
The path is represented as binary in the registry, which is not very easy to operate.
|
|
| Floor9 alucifer | Posted 2006-12-27 21:49 |
| 新手上路 Posts 5 Credits 12 | |
|
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... |
|
| Floor10 HUNRYBECKY | Posted 2006-12-27 22:16 |
| 银牌会员 Posts 442 Credits 1,179 | |
|
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. |
|
| Floor11 HUNRYBECKY | Posted 2006-12-27 22:17 |
| 银牌会员 Posts 442 Credits 1,179 | |
|
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.
|
|
| Floor12 alucifer | Posted 2006-12-27 22:23 |
| 新手上路 Posts 5 Credits 12 | |
|
Thanks to hunrybecky, but is it really impossible to add Path by modifying the registry? Looking forward to help...
|
|
| Floor13 HUNRYBECKY | Posted 2006-12-27 22:30 |
| 银牌会员 Posts 442 Credits 1,179 | |
|
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 |
|
| Floor14 zh159 | Posted 2006-12-27 22:30 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
XP should not execute AUTOEXEC.BAT, but will parse some variables in AUTOEXEC.BAT, and the path is one of them.
|
|
| Floor15 HUNRYBECKY | Posted 2006-12-27 22:33 |
| 银牌会员 Posts 442 Credits 1,179 | |
Originally posted by zh159 at 2006-12-27 22:30: 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. |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |