Check and replace Path system variable

Hi

I have  a MSDOS batch file which adds paths to the Path system variable on Windows, the problem is if I run it again the same paths are just added to the end of the existing path for example

C:\Program Files\cvsnt;C:\Program Files\cvsnt;C:\Program Files\cvsnt;

Is there a way of checking for the presence of an existing path and only add it, if its not there.
setx path "%path%;C:\Program Files\IONA\asp\6.3\bin;C:\Program Files\IONA\bin;C:\Program Files\Bin;C:\Program Files\Bin; -m

Open in new window

paulie99Asked:
Who is Participating?
 
SteveGTRConnect With a Mentor Commented:
Check your path a get rid of any duplicate or unnecessary path's. Best place to do this is in the system properties/environment variables dialog.
0
 
Ashish PatelCommented:
There is IF command, so, probably you will have to search google to do this. For some information how if is used please look at the link below.
http://www.robvanderwoude.com/path.html
0
 
SteveGTRCommented:
Could try something like this:
@echo off
 
setlocal
 
set find=C:\Program Files\cvsnt;C:\Program Files\cvsnt;C:\Program Files\cvsnt;
 
if not "%~1"=="" set find=%~1
 
set found=
 
for /f %%a in ('echo %path% ^| findstr /i /c:"%find%" 2^>NUL') do set found=Y
 
if "%found%"=="Y" echo Found %find% in path
if not "%found%"=="Y" echo Did not find %find% in path

Open in new window

0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
paulie99Author Commented:
thanks but the code seems to be adding whatever I'm looking for to the path in the active msdos box:, so the PATH variables are alwatys added to the path.
Rather than echo an answer i want to write a set path if the path varaible is not there and if it is there to do nothing.

0
 
SteveGTRCommented:

@echo off
 
setlocal
 
set find=C:\Program Files\cvsnt;C:\Program Files\cvsnt;C:\Program Files\cvsnt;
 
if not "%~1"=="" set find=%~1
 
set found=
 
for /f %%a in ('echo %path% ^| findstr /i /c:"%find%" 2^>NUL') do set found=Y
 
if "%found%"=="Y" echo Found %find% in path&set path=%path%;%find%
if not "%found%"=="Y" echo Did not find %find% in path

Open in new window

0
 
paulie99Author Commented:
I have to use setx * -m in order to update the environment varaiable PATH when setting the PATH. But while I can see the code working in the active MSDOS box, after putting in pauses, but once the script is complete and I view the path via my computer or via the command line the path has not been updated.
0
 
SteveGTRCommented:
Post your code as you are running it.
0
 
paulie99Author Commented:
ok code attached, I'm checking for a dummy value for test purposes. Ignore the pauses. As I said above I can see the path being added in active msdos box but when the script is complete it is not there when i open up another msdos or view it via mycomputer. Here is an example of the code I presently use
setx path "%path%;C:\Program Files\IONA\asp\6.3\bin -m
@echo on
 
setlocal
 
set find=C:\C600\hfdihfihasdBIN
 
pause
if not "%~1"=="" set find=%~1
pause
set found=
 pause
for /f %%a in ('echo %path% ^| findstr /i /c:"%find%" 2^>NUL') do set found=Y
 
pause
rem if "%found%"=="Y" echo Found %find% in path&set path=%path%;%find%
pause
if not "%found%"=="Y" set PATH=%PATH%;C:\C600\hfdihfihasdBIN
paused

Open in new window

0
 
SteveGTRCommented:
The code you posted doesn't use setx. It uses set.
@echo on
 
setlocal
 
set find=C:\C600\hfdihfihasdBIN
 
if not "%~1"=="" set find=%~1
 
set found=
 
for /f %%a in ('echo %path% ^| findstr /i /c:"%find%" 2^>NUL') do set found=Y
 
if not "%found%"=="Y" setx "PATH=%PATH%;%find%" -m

Open in new window

0
 
paulie99Author Commented:
Why is it that this line
for /f %%a in ('echo %path% ^| findstr /i /c:"%find%" 2^>NUL') do set found=Y
always sets the value of found to be Y.
0
 
SteveGTRCommented:
It will only be true if the path contains find. I just verified this again on my machine.

If it is always coming up a Y then I'd suggest checking your path.
0
 
paulie99Author Commented:
My mistake I had a double quotes in, when i execute now I get the following error:
C:\>if not "" == "Y" setx PATH="C:\iona\bin;C:\iona\COMet\bin;;C:\Oracle\product
\10.1.0\Client_1\bin;C:\Oracle\product\10.1.0\Client_1\jre\1.4.2\bin\client;C:\O
racle\product\10.1.0\Client_1\jre\1.4.2\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WI
NDOWS\System32\Wbem;c:\bin;C:\Program Files\IONA\asp\6.3\bin;C:\Program Files\IO
NA\bin;C:\Program Files\Bin;C:\Program Files\Bin;C:\Program Fi
les\" -m
ERROR: Length of the command line argument should not exceed 255 characters.
0
All Courses

From novice to tech pro — start learning today.