如何只获取用户路径变量? [英] How to get only the user path variable?

查看:62
本文介绍了如何只获取用户路径变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Echo %PATH%返回系统 PATH变量+ 用户 PATH变量.

Echo %PATH% returns system PATH variable + user PATH variable.

如何仅获取用户 PATH变量?

How to get only the user PATH variable?

推荐答案

系统 PATH变量存储在Windows注册表中,类型为REG_EXPAND_SZ的值Path包含对环境变量的引用像%SystemRoot%一样,或者如果REG_SZ类型的应用程序对其进行了正确的修改,而没有在键下引用环境变量

The system PATH variable is stored in Windows registry with value Path of type REG_EXPAND_SZ containing references to environment variables like %SystemRoot% or if not correct modified by an application of type REG_SZ without environment variable references under the key

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment

默认情况下,用户 PATH变量在Windows注册表中不存在,但是存储在Windows注册表中,一旦由应用程序创建,其值Path的类型为REG_EXPAND_SZREG_SZ或由用户在键下手动输入

The user PATH variable does by default not exist in Windows registry, but is stored in Windows registry with value Path of type REG_EXPAND_SZ or REG_SZ once created by an application or manually by the user under key

HKEY_CURRENT_USER\Environment

带有命令QUERY的外部命令 REG 可用于读取 系统用户 PATH变量直接从Windows注册表中获得标准用户权限.

The external command REG with command QUERY can be used to read the value of the system or the user PATH variable directly from Windows registry with standard user permissions.

仅需一个命令行即可从Windows注册表中读取 user PATH变量并将其值分配给环境变量UserPath,并在目录路径中扩展所有环境变量列表.

It is possible with just a single command line to read the user PATH variable from Windows registry and assign its value to an environment variable UserPath with additionally expanding all environment variables in directory paths list.

@echo off

for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe query "HKCU\Environment" /v "Path" 2^>nul') do if /I "%%N" == "Path" call set "UserPath=%%P" & goto UserPathRead

echo There is no user PATH defined.
echo/
pause
goto :EOF

:UserPathRead
echo The user PATH is: %UserPath%
echo/
set "UserPath="
pause

reg query的输出格式取决于Windows的版本,这使附加的不区分大小写的 IF 条件成为必要.有关reg query输出格式的详细信息,请参见例如Rob编写的使用REG查询读取NT的注册表范德·伍德.

The output format of reg query depends on version of Windows which makes the additional case-insensitive IF condition necessary. For details on reg query output format see for example Reading NT's Registry with REG Query written by Rob van der Woude.

在Windows XP上reg query HKCU\Environment /v Path 的示例输出:

Example output of reg query HKCU\Environment /v Path on Windows XP:

 
! REG.EXE VERSION 3.0

HKEY_CURRENT_USER\Environment
    Path        REG_SZ  C:\BatUtils
 

在输出的开头有一个空行,在输出的末尾还有一个空行,并且在带有注册表项的行的上方有一个标题行和一个空行.

There is an empty line at beginning and one more at end of the output as well as a header line and one more empty line above the line with the registry key.

包含感兴趣数据的行以4个空格字符开头,并且接下来是不区分大小写的环境变量Path的名称.然后,将单个水平制表符字符输出到注册表值类型REG_SZ的左侧.最后,在输出Path的字符串值之前还有一个水平制表符字符,该字符串值也可以包含1个或多个空格字符.

The line with the data of interest starts with 4 space characters and has next the name of the case-insensitive environment variable Path. Then a single horizontal tab character is output left to the registry value type REG_SZ. Last there is one more horizontal tab character before the string value of Path is output which could contain also 1 or more space characters.

在Windows 7上reg query HKCU\Environment /v Path 的示例输出:

Example output of reg query HKCU\Environment /v Path on Windows 7:

 
HKEY_CURRENT_USER\Environment
    Path    REG_SZ    C:\BatUtils
 

在输出的开头还有一个空行,在输出的末尾还有一个空行.但是没有头.第二行已经包含注册表项.

There is also an empty line at beginning and one more at end of the output. But there is no header. The second line contains already the registry key.

包含感兴趣数据的行以4个空格字符开头,然后具有区分大小写的环境变量Path的名称.然后,将 4个空格而不是制表符输出到注册表值类型REG_SZ.最后,在输出Path的字符串值之前,它又有 4个空格而不是一个制表符,该字符串值也可能包含1个或多个空格字符.

The line with the data of interest starts with 4 space characters and has next the name of the case-insenstive environment variable Path. Then 4 spaces instead of a tab are output left to the registry value type REG_SZ. Last there are again 4 spaces instead of a tab before the string value of Path is output which could contain also 1 or more space characters.

REG 命令是由 FOR 在后台的单独命令过程中执行的.通过将 REG 重定向到设备 NUL,可以抑制 REG 输出的错误消息,以在注册表项HKCU\Environment或值Path不存在的情况下处理 STDERR . 2>nul.重定向操作符>必须在此处转义,并且使用脱字符号^才能在Windows命令解释器解析 FOR 命令行时解释为原义字符,但在以后执行时作为重定向操作符REG 命令行在单独的后台命令过程中.

The command REG is executed in a separate command process in background by FOR. The error message output by REG to handle STDERR in case of registry key HKCU\Environment or value Path do not exist is suppressed by redirecting it to device NUL with 2>nul. The redirection operator > must be escaped here with caret character ^ to be interpreted as literal character on parsing FOR command line by Windows command interpreter, but as redirection operator on later execution of REG command line in separate background command process.

"skip=2 tokens=1,2*"定义的 FOR 选项导致跳过 REG 输出的前两行,并使用默认设置将其他行分成3个子字符串(令牌)分隔符空格和制表符.

The FOR options defined with "skip=2 tokens=1,2*" result in skipping the first 2 lines of REG output and splitting the other lines up into 3 substrings (tokens) using the default delimiters space and tab.

第一个用空格/制表符分隔的字符串被分配给第一个循环变量N,这是到达感兴趣数据行时的变量名称.

The first space/tab delimited string is assigned to first loop variable N which is the name of the variable on reaching the line with the data of interest.

将第二个子字符串作为注册表值类型,分配给循环变量O,循环变量O ASCII表.这是 FOR 循环变量区分大小写的原因.

The second substring being the registry value type is assigned to loop variable O being the next character in ASCII table. This behavior is the reason why FOR loop variables are case-sensitive.

第二个子字符串之后的空格/制表符后面的所有内容都分配给循环变量P,而不会由于tokens=1,2之后的选项字符串中的*而将其余行分开.

Everything after spaces/tabs after second substring is assigned to loop variable P without splitting up the rest of the line because of * in options string after tokens=1,2.

因此,在Windows Vista和更高版本的Windows上,命令 FOR 作为 REG 输出的第一行与Path一起处理,而在Windows XP上,第一行处理是带有注册表项的行,这是需要 IF 条件的原因.

So on Windows Vista and later Windows versions the command FOR processes as first line from output of REG the line with Path while on Windows XP the first line processed is the line with the registry key which is the reason why the IF condition is necessary.

命令 GOTO 用于在成功从Windows注册表中读取 user PATH变量后退出 FOR 循环并跳转到在读取现有的用户 PATH之后,该命令块将作进一步处理.但是,如果 user PATH变量在Windows注册表中根本不存在,则会到达 FOR 循环下面的命令块.

The command GOTO is used to exit FOR loop on having successfully read the user PATH variable from Windows registry and to jump to the command block for further processing after existing user PATH being read. But the command block below the FOR loop is reached if the user PATH variable does not exist at all in Windows registry.

为完整起见,批处理代码为

For completeness a batch code to

    从Windows注册表中
  • 读取系统用户 PATH
  • 在当前环境中的当前命令过程中,通过覆盖 local PATH将两个PATH变量连接到PATH
  • 将环境变量扩展为PATH值,
  • PATH值中将所有;;替换为;,并且
  • 如果有一个
  • PATH值的末尾删除;.
  • read system AND user PATH from Windows registry,
  • concatenate both PATH variables to PATH with overwriting local PATH in current command process in current environment,
  • expand the environment variables in PATH value,
  • replace all ;; by ; in PATH value and
  • remove ; from end of PATH value if there is one.

如果用户 PATH包含1个或多个在系统 PATH中已经存在的目录路径,则该批处理代码不会像Windows那样进行检查.因此,目录路径可能位于 local PATH中,最终会出现多次.

This batch code does not check like Windows if user PATH contains 1 or more directory paths being already present in system PATH before concatenating them. So it is possible that a directory path is in local PATH finally present more than once.

@echo off
rem Get directly from Windows registry the system PATH variable value.
set "SystemPath="
for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^>nul') do (
    if /I "%%N" == "Path" (
        set "SystemPath=%%P"
        goto GetUserPath
    )
)

rem Get directly from Windows registry the user PATH variable value.
:GetUserPath
set "UserPath="
for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe query "HKCU\Environment" /v "Path" 2^>nul') do (
    if /I "%%N" == "Path" (
        set "UserPath=%%P"
        goto SetPath
    )
)

rem Concatenate the two PATH values to a single value and expand variables.
rem Delete the two environment variables not further needed.
rem Next replace all two consecutive semicolons by a single semicolon.
rem Last remove semicolon from end of directory list if there is one.

:SetPath
call set "PATH=%SystemPath%;%UserPath%"
set "SystemPath="
set "UserPath="
set "PATH=%PATH:;;=;%"
if "%PATH:~-1%" == ";" set "PATH=%PATH:~0,-1%"

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /?
  • reg query /?
  • rem /?
  • set /?
  • call /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /?
  • reg query /?
  • rem /?
  • set /?

另请阅读有关使用命令重定向操作符的Microsoft文章. 2>nul的说明以及有关堆栈溢出主题使用Windows批处理文件的多行单行命令 &在命令行上.

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and the answer on Stack Overflow topic Single line with multiple commands using Windows batch file for meaning of operator & on a command line.

这篇关于如何只获取用户路径变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆