Reputation: 181
I am trying to set an environment variable in case it is not present and if it is present the script will just exit. I am trying it with this following code.
@echo off
echo %machine% > c:\machineoutput.txt
findstr /I /c:"C:\Oracle\Ora12c\perl\bin" c:\machineoutput.txt
if %errorlevel% == 0 (
goto SETPATH
) else (
EOF
)
:SETPATH
setx /m machine "%oracle_home%\perl\bin;%path%"
:EOF
exit
But some how it's not working as it should. I am able to set the variable but even if the variable exist it sets it again and then goes to exit.
Please help me in finding out what is that I'm doing wrong.
Upvotes: 0
Views: 2504
Reputation: 49086
I don't know why is checked if C:\Oracle\Ora12c\perl\bin
exists in non standard environment variable machine
for determining if %oracle_home%\perl\bin
should be added to the system environment variable PATH. I suggest checking system PATH for existence of this directory path.
EOF
is no valid command. The Windows Command Processor outputs therefore an error message and continues with next command line being the line with setx
. That is the reason for batch code posted in the question is not working as expected. The command in ELSE branch should be goto :EOF
or exit /B
.
Much better would be following code which checks if %oracle_home%\perl\bin
or its expanded equivalent exists in system PATH and appends instead of prepends it to system PATH if not existing in system PATH.
@echo off
if not exist "%oracle_home%\perl\bin\*" exit /B
setlocal EnableExtensions DisableDelayedExpansion
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^>nul') do (
if /I "%%G" == "Path" (
set "SystemPath=%%I"
if defined SystemPath goto CheckPath
)
)
echo Error: System environment variable PATH not found with a non-empty value.
echo(
endlocal
pause
exit /B
:CheckPath
setlocal EnableDelayedExpansion
set "PathToAdd=%%oracle_home%%\perl\bin"
set "OraclePerlPath=!oracle_home!\perl\bin"
if "!SystemPath:~-1!" == ";" (set "Separator=") else set "Separator=;"
if "!SystemPath:%PathToAdd%=!" == "!SystemPath!" (
if "!SystemPath:%OraclePerlPath%=!" == "!SystemPath!" (
set "PathToSet=!SystemPath!%Separator%!PathToAdd!"
set "UseSetx=1"
if not "!PathToSet:~1024,1!" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "!PathToSet!" /M >nul
) else (
set "ValueType=REG_EXPAND_SZ"
if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ"
%SystemRoot%\System32\reg.exe ADD "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul
)
)
)
endlocal
endlocal
For details on this batch code read the answer on Why are other folder paths also added to system PATH with SetX and not only the specified folder path? which is used as template for this batch code. It explains also why the batch code in the question is definitely not good at all.
Note 1: Command setx
is by default not available on Windows XP.
Note 2: Command setx
truncates values longer than 1024 characters to 1024 characters.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
pause /?
reg /?
and reg add /?
and reg query /?
set /?
setlocal /?
setx /?
Upvotes: 1