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-25 06:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] How to modify the system path environment variable with batch processing View 21,855 Replies 31
Original Poster Posted 2006-11-25 09:59 ·  中国 湖南 怀化 电信
初级用户
Credits 170
Posts 25
Joined 2004-11-11 00:00
21-year member
UID 33534
Gender Male
Status Offline
Purpose: Modify or add the system path environment variable. If there is c:\123 in the environment variable, delete it. If there is no %System32%\qqq, add that path.

I used regedit /e or reg export in batch to export the HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment key in the registry, but the exported path is in the following hexadecimal number form and has multiple lines:
"ComSpec"=hex(2):25......
"Path"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,3b,00,25,00,\
53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,3b,00,25,\ .........
"FP_NO_HOST_CHECK"="NO"
"OS"="Windows_NT"
"PROCESSOR_ARCHITECTURE"="x86"
........

Asking experts:
1. How to only keep the path key in the exported registry key (keep multiple lines completely)
2. How to modify the existing path and add a new path

Note: The path obtained by set path in cmd is all absolute paths, not relative paths in the registry. I want to get relative paths, and absolute paths seem unable to be imported into the registry to change the path value.

[ Last edited by HAT on 2008-10-22 at 09:48 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
+1 2010-09-03 09:21
Floor 2 Posted 2006-11-25 11:54 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
The content queried by REG QUERY is not in hexadecimal.
You might as well use REG QUERY to query, and save the queried content into a file (redirect or use For),
Isn't this an existing ASCII code? Why must you have that hexadecimal encoded registry content? Haha...
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-25 13:13 ·  中国 湖南 怀化 电信
初级用户
Credits 170
Posts 25
Joined 2004-11-11 00:00
21-year member
UID 33534
Gender Male
Status Offline
Floor 4 Posted 2006-11-26 01:34 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
path=%path%;D:\My_Path

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 5 Posted 2006-11-26 01:42 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by electronixtar at 2006-11-26 01:34:
path=%path%;D:\My_Path

Hehe, I also add this to autoexec.bat
Floor 6 Posted 2006-11-26 04:40 ·  中国 湖南 怀化 电信
初级用户
Credits 170
Posts 25
Joined 2004-11-11 00:00
21-year member
UID 33534
Gender Male
Status Offline
Originally posted by electronixtar at 2006-11-26 01:34 AM:
path=%path%;D:\My_Path


Brothers on floor 4 and floor 5, you think too simply. After you run your batch file, take a look at the system's path environment variable. The added path is not saved at all, it's only effective within your batch file. Think it over carefully
Floor 7 Posted 2006-11-26 06:21 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Modifying environment variables generally requires making changes in the registry. Since I'm not familiar with this either, I can only get it to this point. Please, experts in the registry step in and offer strong assistance.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-11-26 06:33 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by mylovelyqq at 2006-11-26 04:40:


Brothers on the 4th and 5th floors, you think too simply. After you run your batch file, take a look at the system's path environment variable. The added path is not saved at all, it's only effective within your batch file...


>>c:\autoexec.bat echo path %path%;c:\tools

I use it like this all the time
Floor 9 Posted 2006-11-26 07:13 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
### Adding System Environment Variables:

The values of environment variables need to be entered manually. The ones imported into the registry should be permanent. I don't know how many characters the system environment variables can hold. If it can hold relatively few, it might not be imported successfully. Hehe, I don't know either.
```
@echo off&setlocal enabledelayedexpansion
:begin
cls
set/p path_=Please enter the path of the environment variable you want to add:
if not defined path_ goto error
for,/f,"skip=4 tokens=1,2,*",%%a,in,('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment" /v Path'),do,(
echo The current environment variable is:
echo %%c
echo;
set/p yesno=Do you confirm to add "%path_%" to the system environment variable? /
if /i "!yesno!"=="y" (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%%c;%path_%" /f
)
goto :eof
)
pause
goto :Eof
:error
echo Input error, please enter again
pause
goto begin
```
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +3 2006-11-29 02:54
+1 2010-09-03 09:22
Floor 10 Posted 2006-11-29 02:46 ·  中国 湖南 怀化 电信
初级用户
Credits 170
Posts 25
Joined 2004-11-11 00:00
21-year member
UID 33534
Gender Male
Status Offline
I'm not very familiar with the for command, but I used a small tool called strrpc 1.0 to perform text replacement on the value exported by reg query .../v Path, and it still achieved the goal.
Floor 11 Posted 2006-11-29 03:06 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
lxmxn +7 2006-11-29 07:54
tiancai4126 +1 2007-12-26 15:02
Attachments
setx.rar (25.38 KiB, Credits to download 1 pts, Downloads: 317)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 12 Posted 2006-11-29 03:19 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Brother redtek is always so enthusiastic, heh heh...
I'm here to post some water

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 13 Posted 2006-11-29 07:47 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Writing to autoexec.bat is completely okay~ At least my test in 03 was good
Floor 14 Posted 2006-11-29 08:18 ·  中国 湖南 怀化 电信
初级用户
Credits 170
Posts 25
Joined 2004-11-11 00:00
21-year member
UID 33534
Gender Male
Status Offline
Floor 15 Posted 2006-11-29 08:23 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by mylovelyqq at 2006-11-29 08:18:
Thanks to redtek for providing setx, which can run under XP. I haven't had time to use it much yet.
The small tool strrpc I use to replace text is also very useful. I'll upload it tomorrow.

Also re vkill
Modifying the system's autoexec.bat...

Brother often modifies the path??? I usually set two paths and never worry about it again. I think it's okay. Hehe~ Personal preference
Forum Jump: