title: Why are other folder paths also added to system PATH with SetX and not only the specified folder path?
body: <p>I have a batch file which I am calling from C++ using <code>system("name.bat")</code>. In that batch file I am trying to read the value of a registry key. Calling the batch file from C++ causes <code>set KEY_NAME=HKEY_LOCAL_MACHINE\stuff</code> to fail.</p>
  
  <p>However, when I directly run the batch file (double clicking it), it runs fine. Not sure what I am doing wrong.</p>
  
  <p>Batch file:</p>
  
  <pre><code>set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Ansoft\Designer\2014.0\Desktop
  set VALUE_NAME=InstallationDirectory
  REG QUERY %KEY_NAME% /v %VALUE_NAME%
  </code></pre>
  
  <p>C++ file:</p>
  
  <pre><code>int main(void)
  {
      system("CALL C:\\HFSS\\setup_vars.bat");
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>UPDATE 1:</p>
  
  <p>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.</p>
  
  <p>Now I am having an issue adding that path to my <strong>PATH</strong> variable. Instead of creating a system variable, it is creating a user variable <strong>PATH</strong> and adding it there.</p>
  
  <p>Running from command line works.</p>
  
  <p>Code:</p>
  
  <pre><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%"&gt;NUL
  
  if NOT ERRORLEVEL 1 (
     SETX PATH "%PATH%
  ) else (
     SETX PATH "%PATH%;%addPath%;" /M
  )
  </code></pre>
  
  <hr>
  
  <p>UPDATE 2:</p>
  
  <p>I moved the placement of the option /M and it is now adding to right <strong>PATH</strong> variable.</p>
  
  <p>However, when I am doing this, it is adding the <strong>PATH</strong> more than once (3 times) and then it is also adding a path to visual studio amd64 folder.</p>
  
  <p>I'm mot sure why that is happening.</p>