Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check and replace Path system variable

Posted on 2007-12-06
12
Medium Priority
?
1,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

618 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