本文介绍了Win7上设置PATH环境变量在批处理文件只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户设置的路径和运行的Visual Studio IDE中构建步骤的一部分批处理文件。

I have batch file that sets user path and is run as part of Visual Studio IDE build step.

@ECHO OFF
@ECHO %PATH%
set COMSPEC = "%VCINSTALLDIR%\vcvarsall.bat" amd64
setx PATH "..\..\lib\libsndfile;..\..\lib\simulink" 
@ECHO %PATH%

当我生成项目,关闭VS,并重新打开它,并重建,我看
附加路径 PATH 变量的一部分。但是,我看到,在环境变量 PATH的视窗设置变量是在用户环境变量创建为

When I build the project, close VS, and reopen it, and rebuild, I seeappended path as part of PATH variable. However, I see that in Windows setting of environment variable PATH variable is created under user environment variables as

..\..\lib\libsndfile;..\..\lib\simulink

问1:

为什么这条道路也出现附加路径系统环境变量的一部分?

Why does this path also appear as appended path as part of system environment variable?

在执行回声%PATH%通过Visual Studio控制台(当我运行该项目第二次)打印系统变量路径和我创建的附加了新的路径。

On executing echo %PATH% through Visual Studio console (when I run the project second times) prints system variable path and the new path I created appended to it.

问题2:

我想修改我的批处理文件,以便它只有Visual Studio中建立的第一个运行过程中设置一次在用户设置PATH 环境变量。如果用户变量 PATH 在随后的运行中已经存在,它不应该再次执行设置命令,以避免在系统变量一再追加新的路径。

I want to modify my batch file so that it only sets once PATH environment variable in user settings during first run of Visual Studio build. If the user variable PATH already exists on subsequent runs, it should not execute set command again to avoid appending new path again and again in system variable.

任何想法如何实现这一目标?

Any ideas how to achieve this?

推荐答案

编辑:经过一番测试,看来我原来的答案并不完全适用于OP的问题。为了更直接地回答OP:

edit: After some testing, it appears that my original answer isn't entirely applicable to OP's questions. To answer OP more directly:


  1. %PATH%组合值 HKLM \\系统\\ CurrentControlSet \\控制\\会话管理\\环境\\路径 HKCU \\环境\\路径。当你 SETXDIR; DIR,什么你设置的是 HKEY_CURRENT_USER 路径值。该机宽 HKEY_LOCAL_MACHINE 路径值保持不变。这就是为什么你看到你的值作为附加,而不是作为替代品。你必须使用 SETX / M 来替换 HKLM 路径值。 但是,请不要除非你想创建您的操作系统安装严重的问题。

  1. %PATH% combines the values in HKLM\System\CurrentControlSet\Control\Session Manager\Environment\Path with HKCU\Environment\Path. When you setx "dir;dir", what you're setting is the HKEY_CURRENT_USER Path value. The machine-wide HKEY_LOCAL_MACHINE Path value remains untouched. That's why you see your values as appended, rather than as replacements. You'd have to use setx /m to replace the HKLM Path value. But please don't unless you want to create severe problems with your operating system installation.

如果你想测试在是否存在目录%PATH%,你可以 CD PUSHD 既要检查和%PATH%中的每个目录来统一各目录,确保所有相对路径,环境变量等被夷为平地。 每个集VAR =%CD%。然后如果/我!DIR1!==!DIR2!目录已经在某处存在%PATH%。还有的在这下面我原来的答复的例子。

If you want to test whether a directory exists in %PATH%, you could cd or pushd both to the directory you want to check and to each directory within %PATH% to unify each, making sure all relative paths, environment variables, etc. are flattened. set "var=%CD%" for each. Then if /I "!dir1!"=="!dir2!" the directory already exists somewhere in %PATH%. There's an example of this in my original answer below.

原因我原来的答案并不完全适用,因为 SETX 本身不具有破坏性,因为我曾经以为。危险的是,当用户希望将目录添加到它们的路径很多时候,他们就会 SETX /米PATH%PATH%;新目录;而的破坏性。因为%PATH% SETX 写入前值扩大,在路径中的所有目录pmaturely扩大$ P $。

The reason my original answer isn't entirely applicable is because setx itself isn't as destructive as I once thought. The danger is that often times when users want to append a directory to their path, they'll setx /m PATH "%PATH%;new dir"; and that is destructive. Because %PATH% is expanded before setx writes the value, all the directories in PATH are expanded prematurely.

下面的方法会更安全:

set "env=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"

for /f "tokens=2*" %%I in (
    'reg query "%env%" /v Path ^| findstr /i "\<Path\>"'
) do setx /m PATH "%%J;new directory"

但是,这是不是真的有什么OP问,我为下意识的回答道歉。

But that wasn't really what OP asked, and I apologize for the knee-jerk answer.

原来的答复: SETX 是破坏性的,不应该使用这种方式。当你 SETX PATH 你从REG_EXPAND_SZ转换注册表值的数据类型为REG_SZ。只要你做到这一点,所有存储在%PATH%动态环境变量地转化为平,绝对路径。使用路径命令添加目录到你的%PATH%暂时和 REG ADD 来这样做永久。 (作为一个方面说明,这里还有 dpath ,暂时增加了一个目录路径,但只能由键入命令。滚动2/3一路下跌更多信息有关 dpath

original answer: setx is destructive and shouldn't be used this way. When you setx PATH you're converting the registry value data type from REG_EXPAND_SZ to REG_SZ. As soon as you do this, all the dynamic environment variables stored in your %PATH% get converted to flat, absolute paths. Use the path command to append directories to your %PATH% temporarily, and reg add to do so permanently. (As a side note, there's also dpath, which temporarily adds a directory to your path, but can only be used by the type command. Scroll 2/3 the way down this page for more info on dpath.)

下面是一个实用的脚本我写信给目录的破坏性较小的方式添加到我的%PATH%。这也将避免加入同一个目录下%PATH%不止一次,无论它是如何被格式化(如尾部的反斜杠,相对路径,环境变量,或任何其他排列)

Here's a utility script I wrote to add directories to my %PATH% in a less destructive manner. It will also avoid adding the same directory to %PATH% more than once, regardless of how it's formatted (e.g. trailing backslash, relative paths, environment variables, or any other permutation).

@echo off
setlocal enabledelayedexpansion

if not exist "%~1" goto usage

for %%I in ("%~1") do pushd "%%~I" 2>NUL && (set "new=!CD!" && popd) || goto usage
for %%I in ("%PATH:;=";"%") do pushd "%%~I" 2>NUL && (
    rem // delaying expansion of !new! prevents parentheses from breaking things
    if /i "!new!"=="!CD!" (
        echo !new! already exists in %%PATH%%
        goto :EOF
    )
    popd
)

call :append_path "%new%"

goto :EOF

:usage
echo Usage: %~nx0 "dir"
goto :EOF

:append_path <val>
set "env=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
for /f "tokens=2*" %%I in ('reg query "%env%" /v Path ^| findstr /i "\<Path\>"') do (

    rem // make addition persistent through reboots
    reg add "%env%" /f /v Path /t REG_EXPAND_SZ /d "%%J;%~1"

    rem // apply change to the current process
    for %%a in ("%%J;%~1") do path %%~a
)

rem // use setx to set a temporary throwaway value to trigger a WM_SETTINGCHANGE
rem // applies change to new console windows without requiring a reboot
(setx /m foo bar & reg delete "%env%" /f /v foo) >NUL 2>NUL

color 4E
echo Warning: %%PATH%% has changed.  Reopen the console to inherit the changes.

goto :EOF

这篇关于Win7上设置PATH环境变量在批处理文件只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 23:53