为什么其他的文件夹路径也加入到系统路径以SETX不仅指定的文件夹路径? [英] Why are other folder paths also added to system PATH with SetX and not only the specified folder path?

查看:160
本文介绍了为什么其他的文件夹路径也加入到系统路径以SETX不仅指定的文件夹路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从C ++调用使用一个批处理文件系统(name.bat)。在该批处理文件,我想读一个注册表项的值。从调用C ++批处理文件导致设置KEY_NAME = HKEY_LOCAL_MACHINE \\东西失败。

I have a batch file which I am calling from C++ using system("name.bat"). In that batch file I am trying to read the value of a registry key. Calling the batch file from C++ causes set KEY_NAME=HKEY_LOCAL_MACHINE\stuff to fail.

然而,当我直接运行该批处理文件(双击它),它运行良好。不知道我做错了。

However, when I directly run the batch file (double clicking it), it runs fine. Not sure what I am doing wrong.

批处理文件:

set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Ansoft\Designer\2014.0\Desktop
set VALUE_NAME=InstallationDirectory
REG QUERY %KEY_NAME% /v %VALUE_NAME%

C ++文件:

C++ file:

int main(void)
{
    system("CALL C:\\HFSS\\setup_vars.bat");
    return 0;
}


更新1:


UPDATE 1:

我发现关键实际上是在64位注册表,而我建设我的C ++溶液作为32位。一旦我固定的,它找到注册表项罚款。

I found out that the key is actually in the 64-bit registry, and I was building my C++ solution as a 32-bit. Once I fixed that, it found the registry key fine.

现在我有并称路径我的 PATH 变量的问题。而不是创建一个系统变量,它是创建用户变量 PATH 并添加它。

Now I am having an issue adding that path to my PATH variable. Instead of creating a system variable, it is creating a user variable PATH and adding it there.

在命令行中运行工作。

code:

set KEY_NAME=HKLM\SOFTWARE\Ansoft\Designer\2014.0\Desktop\
set VALUE_NAME=InstallationDirectory

FOR /F "usebackq skip=1 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME%`) DO (
   set ValueName=%%A
   set ValueType=%%B
   set ValueValue=%%C
)

if defined ValueName (
   @echo Value Value = %ValueValue%
) else (
   @echo %KEY_NAME%\%VALUE_NAME% not found.
)

:: Set PATH Variable
set path_str=%PATH%
set addPath=%ValueValue%;

echo %addPath%
echo %ValueValue%

echo %PATH%| find /i "%addPath%">NUL

if NOT ERRORLEVEL 1 (
   SETX PATH "%PATH%
) else (
   SETX PATH "%PATH%;%addPath%;" /M
)


更新2:


UPDATE 2:

我感动选项/ M的位置和它现在添加到右的 PATH 变量。

I moved the placement of the option /M and it is now adding to right PATH variable.

然而,当我这样做,它是添加在 PATH 超过一次(3次),然后将其还加入到Visual Studio AMD64文件夹的路径。

However, when I am doing this, it is adding the PATH more than once (3 times) and then it is also adding a path to visual studio amd64 folder.

我MOT知道为什么就是发生了。

I'm mot sure why that is happening.

推荐答案

Windows创建启动新进程的新工艺过程的整个环境表的副本。因此,在你的C ++应用程序的开始,你的应用程序从父进程,Windows资源管理器或在您的案件的Visual Studio环境表包括 PATH 。而这个 PATH 被复制为的cmd.exe 的批处理文件的开头。

Windows creates a copy of the entire environment table of the process starting a new process for the new process. Therefore on start of your C++ application, your application gets the environment table including PATH from parent process, Windows Explorer or in your case Visual Studio. And this PATH is copied for cmd.exe on start of the batch file.

拍摄的整个过程棵树考虑从Windows桌面到批处理文件,已经出现了对于 PATH取得许多拷贝以及一些流程可能附加的东西到他们的本地 PATH的副本像Visual Studio做,或有从 PATH

Taking the entire process tree into account from Windows desktop to the batch file, there have been many copies made for PATH and some processes perhaps appended something to their local copy of PATH like Visual Studio has done, or have even removed paths from PATH.

您现在 SETX PATH%PATH%正在添加的 PATH的本地副本做什么在流程树的父进程已经修改完全系统的 PATH 不检查重复的路径。

What you do now with SETX PATH "%PATH% is appending the local copy of PATH modified already by the parent processes in process tree completely to system PATH without checking for duplicate paths.

好多了将使用 PATH的本地副本扔掉所有code ,而是读取系统的 PATH 的值,检查路径要加上尚未在系统中的 PATH ,如果不是这种情况,您要添加到系统中的 PATH SETX

Much better would be to throw away all code using local copy of PATH and instead read the value of system PATH, check if the path you want to add is not already in system PATH and if this is not the case, append the path you want to add to system PATH using setx.

这应该没有扩大环境变量系统的 PATH像的%SystemRoot%\\ SYSTEM32 C下进行:\\ Windows \\ System32下

更新

这是给你的任务在Windows 7 X64的测试所需的批次code。

Here is the batch code required for your task tested on Windows 7 x64.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "KeyName=HKLM\SOFTWARE\Ansoft\Designer\2014.0\Desktop"
set "ValueName=InstallationDirectory"
for /F "skip=2 tokens=3" %%N in ('%SystemRoot%\System32\reg.exe query "%KeyName%" /v "%ValueName%" 2^>nul') do (
    set "PathToAdd=%%N"
    goto GetSystemPath
)
echo Error: Could not find value "%ValueName%" under key
echo        %KeyName%
echo.
endlocal
pause
goto :EOF

:GetSystemPath
for /F "skip=2 tokens=3" %%P in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^>nul') do (
    set "SystemPath=%%P"
    goto CheckPath
)
echo Error: System environment variable PATH not found.
echo.
endlocal
pause
goto :EOF

:CheckPath
set "Separator="
if not "%SystemPath:~-1%" == ";" set "Separator=;"
if "!SystemPath:%PathToAdd%=!" == "%SystemPath%" (
    %SystemRoot%\System32\setx.exe PATH "%SystemPath%%Separator%%PathToAdd%" /M
)
endlocal

注:批code是Windows Vista和更高版本的Windows。它不能用于Windows XP或更老版本的Windows。

Note: The batch code is for Windows Vista and later versions of Windows. It can't be used for Windows XP or even older versions of Windows.

上面的批code使用一个简单的区分大小写字符串替换和区分大小写字符串比较,以检查是否追加该文件夹的路径是present已经在系统中的 PATH 。这只有当它是公知的文件夹路径是如何前加入并在此期间用户没有修改它。对于检查是否 PATH 的更安全的方法中包含的文件夹路径看便知在如何检查是否存在在目录%PATH%?通过书面 dbenham

The batch code above uses a simple case-insensitive string substitution and a case-sensitive string comparison to check if the folder path to append is present already in system PATH. This works only if it is well known how the folder path was added before and the user has not modified it in the meantime. For a safer method of checking if PATH contains a folder path see the answer on How to check if directory exists in %PATH%? written by dbenham.

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。

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.


  • 回声/?

  • ENDLOCAL /?

  • 为/?

  • 转到/?

  • 如果/?

  • 暂停/?

  • 注册/?章查询/?

  • 设置/?

  • SETLOCAL /?

  • SETX /?

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /? and reg query /?
  • set /?
  • setlocal /?
  • setx /?

这篇关于为什么其他的文件夹路径也加入到系统路径以SETX不仅指定的文件夹路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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