Check if a a path is in PATH


B

BigMan

How should I check if a path is in the PATH environment variable?
 
Ad

Advertisements

M

Matthias Tacke

BigMan said:
How should I check if a path is in the PATH environment variable?
To first check and then append a path given as argument.

::Add2Path.bat:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
echo.%path%|find /C /I "%1">NUL
if errorlevel 1 set Path=%Path%;%1
::Add2Path.bat:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Or from commandline:
@echo.%path%|find /C /I "c:\newpath" >NUL ||set path=%path%;c:\newpath

HTH
 
S

Stefan Kanthak

Matthias Tacke said:
To first check and then append a path given as argument.

::Add2Path.bat:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
echo.%path%|find /C /I "%1">NUL
That's to silly?! The following is correct, but too clumsy for me:

echo.^;%path%^;|find /C /I ";%1;">NUL

So: Why not use

For %! In (%PATH%) Do If "%!" == "%1" Set ...
if errorlevel 1 set Path=%Path%;%1
::Add2Path.bat:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Stefan
[
 
Ad

Advertisements

B

BigMan

OK, please give the source of a bat file that writes every path in PATH to
the screen...
 
M

Matthias Tacke

BigMan said:
OK, please give the source of a bat file that writes every path in PATH to
the screen...
commandline:
for %A in ("%path:;=";"%") do @echo %~A

Batch:
for %%A in ("%path:;=";"%") do @echo %%~A
 
M

Matthias Tacke

BigMan said:
Wow!
Could you explain this: "%path:;=";"%"
The standard for has the space "," and the ";" as standard delmiters.
(at least - may be locale dependant)
If there are spaces in any of the folders in the path, for will
break at that point. To avoid this I quote every single path by
replacing all simicolons with quote semicolon quote plus quotes
at the begin and end resulting in proper parsed output.

See:
for /?
set /?
for details on handling environment vars.
 
B

Bill Stewart

BigMan said:
How should I check if a path is in the PATH environment variable?
If you mean "how do I check if a directory exists in the PATH", then one
option is my editpath freeware tool.

editpath -s -c "directory name"

If the exit code is zero, then the specified directory name exists in
the system path (omit -s to check if it exists in the user path).

You can download it from:

http://www.cybermesa.com/~bstewart/misctools.html

(epath1.zip)
 
Ad

Advertisements

B

Bill Stewart

BigMan said:
OK, please give the source of a bat file that writes every path in PATH to
the screen...
Not a .bat file, but my freeware tool editpath.exe can do this for the
system or user path, and it can also expand the environment variables if
needed.

editpath -s -l

Add -x to expand the variables, and omit -s to list the current user
path rather than the system path.

You can download epath1.zip from here:

http://www.cybermesa.com/~bstewart/misctools.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Path 3
how to judge whether a path is relative path or absolute path 20
Search path 7
set path 4
Search PATH for a file 2
dir without path? 5
UNC paths in cmd.exe 9
Append to path statement 10

Top