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.
Batch file:
set KEY_NAME=HKEY_LOCAL_MACHINE\\SOFTWARE\\Ansoft\\Designer\\2014.0\\Desktop
set VALUE_NAME=InstallationDirectory
REG QUERY %KEY_NAME% /v %VALUE_NAME%
C++ file:
int main(void)
{
system(\"CALL C:\\\\HFSS\\\\setup_vars.bat\");
return 0;
}
UPDATE 1:
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.
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.
Running from command line works.
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
)
UPDATE 2:
I moved the placement of the option /M and it is now adding to right PATH variable.
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.
I\'m mot sure why that is happening.
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.
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.
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.
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
.
And this should be done without expanding the environment variables in system PATH like %SystemRoot%\\System32
to C:\\Windows\\System32
.
UPDATE
Here is the batch code required for your task tested on Windows 7 x64 and Windows XP x86.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set \"KeyName=HKLM\\SOFTWARE\\Ansoft\\Designer\\2014.0\\Desktop\"
set \"ValueName=InstallationDirectory\"
for /F \"skip=2 tokens=1,2*\" %%N in (\'%SystemRoot%\\System32\\reg.exe query \"%KeyName%\" /v \"%ValueName%\" 2^>nul\') do (
if /I \"%%N\" == \"%ValueName%\" (
set \"PathToAdd=%%P\"
goto GetSystemPath
)
)
echo Error: Could not find value \"%ValueName%\" under key
echo %KeyName%
echo/
endlocal
pause
goto :EOF
:GetSystemPath
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 CheckPath
)
)
echo Error: System environment variable PATH not found.
echo/
endlocal
pause
goto :EOF
:CheckPath
setlocal EnableDelayedExpansion
set \"Separator=\"
if not \"!SystemPath:~-1!\" == \";\" set \"Separator=;\"
set \"PathCheck=!SystemPath!%Separator%\"
if \"!PathCheck:%PathToAdd%;=!\" == \"!PathCheck!\" (
%SystemRoot%\\System32\\setx.exe Path \"!SystemPath!%Separator%%PathToAdd%\" /M
)
endlocal
endlocal
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 this folder path in PATH 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 Dave Benham.
Note: Command SETX is by default not available on Windows XP.
The batch script was tested with PATH containing currently a folder path with an exclamation mark and with a folder path being enclosed in double quotes which is necessary only if the folder path contains a semicolon.
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.
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
and reg query /?
set /?
setlocal /?
setx /?