Check if a a path is in PATH

  • Thread starter Thread starter BigMan
  • Start date Start date
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
 
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
[
 
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
 
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.
 
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)
 
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
 
Back
Top