Solved

Add to PATH environment variable if value does not already exist

Posted on 2013-05-16
3
1,468 Views
Last Modified: 2013-07-04
I am using the following to add to my PATH environment variable in DOS

Path=%Path%;C:\Windows\Microsoft.NET\Framework\v3.5

This works as expected, but the batch file will be run repeatedly so I want to add some code that will only append to the batch file if the value is not actually there? Is this possible?

Also, when I use the SET command in DOS the changes I have made to the environment variable are shown, but when I try and access the information via My Computer > Environment Variables it doesn't look like these have been refreshed? Is there a command I can use to refresh these?
0
Comment
Question by:Blowfelt82
3 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39170894
"Set" can only set variables whose scope is the current command prompt; as soon as the prompt is closed, the environment variables are gone.
If you want it to be available permanently, you need "setx.exe" (which does not set the variable for the current session!).
@echo off
set AddPath=C:\Windows\Microsoft.NET\Framework\v3.5
echo path| find /i "%AddPath%" >NUL
if errorlevel 1 (
	set Path=%Path%;%AddPath%
	setx.exe /m Path "%Path%;%AddPath%"
)

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39171066
A couple of notes.  First, I think the PATH in the above FIND needs % around it.  In addition, we need to watch out to make sure we match the full path we are trying to add, not just a subset of an existing but different path.  For example if we wanted to add "c:\temp" anmd there was already a c:\tempold" the above would fail to add.  Below are a couple of ways to deal with that, one adds a semicolon to the end of the new path to make sure it's a full match.  The other parses the current path breaking it up at the existing semicolons and then compares to the full path (some trickery needed there).

Also of course keep in mind that the PATH command is a bit odd, and there are other uncommon situations that can occur.  For example the paths can be relative in the PATH variable.  And it will work with paths using either \ or / between folder names.  Paths can be added with a training \ or not.  And Paths can be added with quotes around them. So lots of odd cases, but typically we just check for the normal stuff.

@echo off
set AddPath=C:\Windows\Microsoft.NET\Framework\v3.5
echo %PATH%; | find /i "%AddPath%;" >NUL || (
  set Path=%PATH%;%AddPath%
  setx.exe /m PATH "%PATH%;%AddPath%"
)

Open in new window

@echo off
set AddPath=C:\Windows\Microsoft.NET\Framework\v3.5
set Found=N
for %%A in ("%PATH:;=","%") do if /i "%%~A" EQU "%AddPath%" set Found=Y
if "%Found%" EQU "N" (
  set Path=%PATH%;%AddPath%
  setx.exe /m PATH "%PATH%;%AddPath%"
)

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 39192960
The proper way to do it is with the following command:

if not defined AddPath setx AddPath C:\Windows\Microsoft.NET\Framework\v3.5 /m

Open in new window

After issuing this command, open another DOS box and you will see the variable is set using the SET command.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
robocopy for today files 14 180
Question to Zip 8 90
MailSend Attachments 41 184
Batch Script for opening the DOS prompt and run a command 9 115
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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