|
mylovelyqq
初级用户
 
积分 170
发帖 25
注册 2004-11-11
状态 离线
|
『楼 主』:
[已结]如何用批处理修改系统path环境变量
使用 LLM 解释/回答一下
目的: 修改或添加系统path环境变量,如果环境变量中有c:\123,则删除之,如果没有%System32%\qqq则添加该路径.
我在批处理中用regedit /e 或 reg export 导出注册表的HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment 项,但导出的path是下列方式的16进制数,且有多行:
"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"
........
请教高手:
1.对导出的注册表项如何只保留其中的path项(多行完整保留)
2.如何修改已有路径和添加新的路径
说明:在cmd中用set path方式得到的路径都绝对路径,而不是注册表中的相对路径.我希望得到相对路径,而且绝对路径好象无法导入注册表中更改path值.
Last edited by HAT on 2008-10-22 at 09:48 ]
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 ]
此帖被 +1 点积分 点击查看详情 评分人:【 】 | 分数: +1 | 时间:2010-9-3 09:21 |
|
|
|
2006-11-25 09:59 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
REG QUERY 查出上面的注册表内容是非16进制的,
你还不如用 REG QUERY 查询,查询的内容存到一个文件内(重定向或是For都行),
这不是现成的ASCII码吗?干吗非得要那个16进制的编过码的注册表内容啊?哈哈……
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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-25 11:54 |
|
|
mylovelyqq
初级用户
 
积分 170
发帖 25
注册 2004-11-11
状态 离线
|
|
2006-11-25 13:13 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
path=%path%;D:\My_Path
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'>" |
|
2006-11-26 01:34 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by electronixtar at 2006-11-26 01:34:
path=%path%;D:\My_Path
呵呵,我也是把这个加到autoexec.bat
Originally posted by electronixtar at 2006-11-26 01:34:
path=%path%;D:\My_Path
Hehe, I also add this to autoexec.bat
|
|
2006-11-26 01:42 |
|
|
mylovelyqq
初级用户
 
积分 170
发帖 25
注册 2004-11-11
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by electronixtar at 2006-11-26 01:34 AM:
path=%path%;D:\My_Path
第4楼和第5楼的兄弟,你想得也太简单了吧,你运行完你的批处理后看看系统的path环境变量,添加的路径根本就保存不了,只是在你的批处理里有效而已,好好想想吧
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
|
|
2006-11-26 04:40 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
修改环境变量一般要到注册表里去动手脚,因我对这个也不熟悉,只能点到这个份上了,请注册表方面的高手出马鼎力相助。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-26 06:21 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by mylovelyqq at 2006-11-26 04:40:
第4楼和第5楼的兄弟,你想得也太简单了吧,你运行完你的批处理后看看系统的path环境变量,添加的路径根本就保存不了,只是在你的批处理里有效而已, ...
>>c:\autoexec.bat echo path %path%;c:\tools
我这样用的啊~一直是
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
|
|
2006-11-26 06:33 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
添加系统环境变量:
环境变量值需要手工输入,导入注册表的,应该是永久的。不知道系统环境变量可容纳的字符是多少,如果容纳的比较小,可能导入不成功,呵呵,我也不知道。
@echo off&setlocal enabledelayedexpansion
:begin
cls
set/p path_=请输入你要添加的环境变量的路径:
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 当前的环境变量为:
echo %%c
echo;
set/p yesno=是否确认将"%path_%"添加到系统环境变量中去?/
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 输入错误,请重新输入
pause
goto begin
### 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
```
此帖被 +4 点积分 点击查看详情 评分人:【 redtek 】 | 分数: +3 | 时间:2006-11-29 02:54 | 评分人:【 】 | 分数: +1 | 时间:2010-9-3 09:22 |
|
|
|
2006-11-26 07:13 |
|
|
mylovelyqq
初级用户
 
积分 170
发帖 25
注册 2004-11-11
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
我对 for 命令实在是不熟,不过对 reg query .../v Path导出的值用了一个叫strrpc 1.0的小工具进行了文本替换,还是达到了目的
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.
|
|
2006-11-29 02:46 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
  『第 11 楼』:
使用 LLM 解释/回答一下
Windows Server 2003 自带一个修改用户、系统环境变量并永久生效的工具: SETX.EXE
这个工具已经强大到你可以指定存在于一个文本文件内的(x,y)座标来直接取文本值并设置到环境变量内,并永久生效。
(备注: 下面 SETX 工具的使用说明
SetX 有三种使用方式:
语法 1:
SETX user ]]] var value
语法 2:
SETX user ]]] var /K regpath
语法 3:
SETX user ]]]
/F file {var {/A x,y | /R x,y string} | /X}
描述:
在用户或系统环境创建或修改环境变量。能基于参数、注册表项或文件输
入设置变量。
参数列表:
/S system 指定要连接的远程系统。
/U user 指定命令在其下执行的用户上下文。
/P 指定给定用户上下文的密码。如果省略则提示输入。
var 指定要设置的环境变量。
value 指定分配给环境变量的值。
/K regpath 指定变量是基于注册表项的信息而设置的。
路径的格式应该是 hive\key\...\value。例如
HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Control\TimeZoneInformation\StandardName。
/F file 指定要使用的文本文件的文件名。
/A x,y 指定绝对文件坐标(线 X,项目 Y)作为在此文件
里搜索的参数。
/R x,y string 指定有关“字符串”作为搜索参数的相对文件坐标。
/M 指定应该在系统 (HKEY_LOCAL_MACHINE) 环境中设
置此变量。在 HKEY_CURRENT_USER 环境下,默认
将设置此变量。
/X 用 x,y 坐标显示文件内容。
/D delimiters 指定其他限定符,如 "," 或 "\"。
内置分隔符是空格、制表符、回车和换行符。所有
ASCII 字符都可作为限定符。限定符的最大数量,
包括内置分隔符,是 15。
/? 显示帮助消息。
注意: 1) SETX 在注册表中将变量写入主机环境。
2) 在本地系统,用此工具创建或修改的变量将在以后的命令窗口可用,但
在当前的 CMD.exe 命令窗口。
3) 在远程系统,用此工具创建或修改的变量在下次登录会话可用。
4) 有效的注册表项数据类型是 REG_DWORD,REG_EXPAND_SZ,REG_SZ
和 REG_MULTI_SZ。
5) 受支持的配置单元: HKEY_LOCAL_MACHINE (HKLM),
HKEY_CURRENT_USER (HKCU)。
6) 限定符区分大小写。
7) REG_DWORD 的值是从注册表里以十进制格式提取出来的。
示例:
SETX MACHINE COMPAQ
SETX MACHINE "COMPAQ COMPUTER" /M
SETX MYPATH "%PATH%"
SETX MYPATH ~PATH~
SETX /S system /U user /P password MACHINE COMPAQ
SETX /S system /U user /P password MYPATH ^%PATH^%
SETX TZONE /K HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Control\TimeZoneInformation\StandardName
SETX BUILD /K "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
NT\CurrentVersion\CurrentBuildNumber" /M
SETX /S system /U user /P password TZONE /K HKEY_LOCAL_MACHINE\
System\CurrentControlSet\Control\TimeZoneInformation\
StandardName
SETX /S system /U user /P password BUILD /K
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\
CurrentVersion\CurrentBuildNumber" /M
SETX /F ipconfig.out /X
SETX IPADDR /F ipconfig.out /A 5,11
SETX OCTET1 /F ipconfig.out /A 5,3 /D "
SETX IPGATEWAY /F ipconfig.out /R 0,7 Gateway
SETX /S system /U user /P password /F c:\ipconfig.out /X
下面是这个 SETX.EXE Windows Server 2003自带的环境变量操纵工具的中文使用技巧~:)
(备注: 下面使用技巧来自互联网,因COPY时未见标有原作者的记录数据,故原作者名未在下文标注出来。下面文章版权归原作者所有)
这个命令行工具提供了一种批量修改环境变量的方法,同时不需要使用任何编程方法或者是脚本。此外,除了可以获得变量和值之外还可以跟注册表的值挂上关系。在2000中除了setx.exe之外,其他没有任何一种命令行工具可以直接设置系统变量值。只有通过控制面板和注册表编辑器才能够设置。如果有人说可以用set命令的话, 实际上这个CMD的内部命令只能够设置当前控制台窗口的用户环境变量。 setx允许你通过Command Line Mode,Registry Mode,File Mode,这三种模式来设置环境变量。
a. Command-Line
setx variable value -m
variable
要设置的环境变量的名字。
value
要设置的值。
-m
设置计算机环境,默认设置是用户环境。
b. Registry Mode
setx variable -k hive\key\……\value -m
variable
要设置的环境变量的名字。
-k
指定变量设置会基于注册表中的信息。
hive\key\……\value
注册表路径,如:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
-m
同命令行模式。
c. File Mode
setx variable -f filename -a x,y │ -r x,y "string" -d d -x -m
-f filename
指定使用的文件名。
-a x,y
指定要搜索的绝对坐标或者是偏移量。
-r x,y "string"
指定从"string"的相对坐标或者是偏移。
-d d
指定附加的分隔符,后一个d表示,可以是",", "\"等等。系统有四个内建的分隔符是:space, tab,
carriage return, linefeed。附加的可以是任意的ASCII字符。
-x
显示文件坐标,这个时候-a, -r, -d会变忽略。
-m
在同上。 setx将变量写入到在注册表的主环境中去。用setx设置的变量将会在下一次启动的时候生效,这是2000的局限。当访问REG_MULTI_SZ时,只能访问到第一个项目。
目前setx只可以支持HKCU和HKLM两个hives。用Setx增加的值不可以用setx去掉,可以在控制面板里面改,也可以在注册表里面改,这种时候也可以用set把它置空。
但是还是来几个例子吧!
a.Command Line
Setx MACHINE COMPAQ
在用户环境中设置MICHINE为COMPAQ。
setx MYPATH %PATH%
设置MYPATH的值为当前PATH变量的值。
setx MYPATH ~PATH~
设置MYPATH总是和PATH环境的值保持一致。
b. Registry
Setx TZONE -k
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
设置TZONE的值为上面的键。比如:"Central Standard Time"
Setx BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
NT\CurrentVersion\CurrentBuildNumber"
设置BUILD的值为当前的Windows NT版本号,如:1314。
c. File
在进行这部分之前先运行ipconfig > ipconfig.out,弄一个ipconfig的文本出来。然后就可以进行*作了
Setx var -f ipconfig.out -x
这个命令会显示这个文件的内容的坐标。如:
C:\>SETX VAR -f ipconfig.out -x
(1,0 Windows) (1,1 2000) (1,2 IP) (1,3 Configuration)
(3,0 Ethernet) (3,1 adapter) (3,2 Local) (3,3 Area) (3,4 Connection
(5,0 Connection-specific) (5,1 DNS) (5,2 Suffix) (5,3 .) (5,4
(6,0 IP) (6,1 Address.) (6,2 .) (6,3 .) (6,4 .) (6,5 .) (6,6 .) (6,7 .) (6,8 .)
(6,9 .) (6,10 .) (6,11 .) (6,12 .) (6,13 (6,14 192.168.1.1)
(7,0 Subnet) (7,1 Mask) (7,2 .) (7,3 .) (7,4 .) (7,5 .) (7,6 .) (7,7 .) (7,8 .)
(7,9 .) (7,10 .) (7,11 .) (7,12 .) (7,13 (7,14 255.255.255.0)
(8,0 Default) (8,1 Gateway) (8,2 .) (8,3 .) (8,4 .) (8,5 .) (8,6 .) (8,7 .) (8,8
.) (8,9 .) (8,10 .) (8,11
setx ipaddr -f ipconfig.out -a 5,11
这个命令会寻找绝对偏移为(5,11)的内容,然后设置ipaddr为这个值。如:
C:\>setx ipaddr -f ipconfig.out -a 6,14
Extracted value is: 192.168.1.1
Setx ipgateway -f ipconfig.out -r 0,11 "Gateway"
查找Gateway这个字符串偏移0,11的东东,然后写到ipgateway里面去。
C:\>Setx ipgateway -f ipconfig.out -r 0,11 "Gateway"
Extracted value is: 192.168.1.244
以上所有的修改都会在重起以后看到,可以用set或者是控制面板中的System properties看。
(注:工具在附件内,请使用前用杀毒软件扫描一遍以确保安全应用)
附件
1: setx.rar (2006-11-29 03:06, 25.38 KiB, 下载附件所需积分 1 点
,下载次数: 317)
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-29 03:06 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
redtek兄总是这么热情,呵呵……
偶来灌水

Brother redtek is always so enthusiastic, heh heh...
I'm here to post some water

|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-11-29 03:19 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
写入autoexec.bat 完全可以啊~至少我03测试是好的
Writing to autoexec.bat is completely okay~ At least my test in 03 was good
|
|
2006-11-29 07:47 |
|
|
mylovelyqq
初级用户
 
积分 170
发帖 25
注册 2004-11-11
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
感谢 redtek 提供的 setx,在XP下可以运行,目前还没时间多用.
我用的替换文本的小工具strrpc也特别好用,明儿再上传吧.
另外re vkill
改系统的autoexec.bat可是可以,但我不建议这么做,一是现在很少有人在windows操作系统下去改autoexec.bat,除非是做DOS启动盘,可能很多人都忘了或删除了这个文件.
二是尽量只在一个地方设置系统环境变量,如果在自动批处理和系统的环境变量区都设置了环境变量,以后要添加或修改系统环境变量时就要检查或修改二个地方,给自己的操作带来麻烦,如果有太多重复的路径,说不定系统找个文件还要找半天,嘿嘿,它可不知道哪些路径是相同的.
|
|
2006-11-29 08:18 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Originally posted by mylovelyqq at 2006-11-29 08:18:
感谢 redtek 提供的 setx,在XP下可以运行,目前还没时间多用.
我用的替换文本的小工具strrpc也特别好用,明儿再上传吧.
另外re vkill
改系统的autoexec.bat叠...
兄常改path ???我一般设置两个路径,以后再也不管了,觉得还好了,呵呵~个人爱好
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
|
|
2006-11-29 08:23 |
|
|