Solved

Check and replace Path system variable

Posted on 2007-12-06
12
1,180 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility

@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
Comment Utility
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
Comment Utility
Post your code as you are running it.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:paulie99
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now