Improve company productivity with a Business Account.Sign Up

x
?
Solved

Check and replace Path system variable

Posted on 2007-12-06
12
Medium Priority
?
1,293 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:paulie99
  • 6
  • 5
12 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20419688
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20419976
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
 

Author Comment

by:paulie99
ID: 20427277
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 20427580

@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
 

Author Comment

by:paulie99
ID: 20499703
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20500017
Post your code as you are running it.
0
 

Author Comment

by:paulie99
ID: 20505520
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20506339
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
 

Author Comment

by:paulie99
ID: 20507816
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20507894
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
 

Author Comment

by:paulie99
ID: 20508682
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
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 total points
ID: 20508997
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question