Skip to main content
Post Undeleted by Redsplinter
deleted 632 characters in body
Source Link

Using the suggestions here I've combined some of the above answers to come up with a neat little solution that ensures that an entry is in the PATH variable. While I acknowledge it's not a direct answer to the OP, I thought it might help others like myself that have scattered batch files and don't wantthis to clutter the PATH variable or riskensure that a system restore/admin/etc removegiven path entriesentry exists exactly as given with as much brevity as possible and no junk echos on the command line.

set myPath=<pathToEnsure|%1>myPath=<pathToEnsure | %1> echo ;%PATH%; | find /C /I ";%myPath%;" >nul if %ERRORLEVEL%==0 GOTO Else rem Path element NOT found.  Adding... %ERRORLEVEL% NEQ 0 set PATH=%PATH%;%myPath% GOTO EndIf :Else rem Path element found. :EndIf 

Of course the if/else structure isn't necessary, I just left it in as an example. (It's an artifact of my testing... never can remember batch file syntax...)

As a BTW, if anyone knows a command line option for making environment variable changes permanent other than setx - which I can't seem to make like me - I'm all ears.

Using the suggestions here I've come up with a neat little solution that ensures that an entry is in the PATH variable. While I acknowledge it's not a direct answer to the OP, I thought it might help others like myself that have scattered batch files and don't want to clutter the PATH variable or risk that a system restore/admin/etc remove path entries.

set myPath=<pathToEnsure|%1> echo ;%PATH%; | find /C /I ";%myPath%;" >nul if %ERRORLEVEL%==0 GOTO Else rem Path element NOT found.  Adding...  set PATH=%PATH%;%myPath% GOTO EndIf :Else rem Path element found. :EndIf 

Of course the if/else structure isn't necessary, I just left it in as an example. (It's an artifact of my testing... never can remember batch file syntax...)

As a BTW, if anyone knows a command line option for making environment variable changes permanent other than setx - which I can't seem to make like me - I'm all ears.

I've combined some of the above answers to come up with this to ensure that a given path entry exists exactly as given with as much brevity as possible and no junk echos on the command line.

set myPath=<pathToEnsure | %1> echo ;%PATH%; | find /C /I ";%myPath%;" >nul if %ERRORLEVEL% NEQ 0 set PATH=%PATH%;%myPath% 
Post Deleted by Redsplinter
Source Link

Using the suggestions here I've come up with a neat little solution that ensures that an entry is in the PATH variable. While I acknowledge it's not a direct answer to the OP, I thought it might help others like myself that have scattered batch files and don't want to clutter the PATH variable or risk that a system restore/admin/etc remove path entries.

set myPath=<pathToEnsure|%1> echo ;%PATH%; | find /C /I ";%myPath%;" >nul if %ERRORLEVEL%==0 GOTO Else rem Path element NOT found. Adding... set PATH=%PATH%;%myPath% GOTO EndIf :Else rem Path element found. :EndIf 

Of course the if/else structure isn't necessary, I just left it in as an example. (It's an artifact of my testing... never can remember batch file syntax...)

As a BTW, if anyone knows a command line option for making environment variable changes permanent other than setx - which I can't seem to make like me - I'm all ears.