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
Solved

Check and replace Path system variable

Posted on 2007-12-06
12
1,210 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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