Jump to content

Recommended Posts

Posted

We have a set of command-line tools (AMPL with additional 'solvers') that we are going to deploy to a couple dozen computers. In order to run these from CMD without typing the full folder path, we need to add their folder path to the PATH Environment Variable in Windows. On our test computer I've create a folder in Program Files named AMPL, which contains the AMPL executable, and each of the additional solver executables are in subfolders within this folder, for example: Program Files > AMPL > xpress

 

Using PowerShell, I am able to add the AMPL folder to PATH. My issue arrises when I try to add one of the subfolders to PATH, the subfolder does not append to the list of path variables, it overwrites the previously added AMPL folder path.

 

Basically, the first part of the script runs, and it adds the AMPL folder to path, with this result:

C:\Windows\System32>pathPATH=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;
...
C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
[b]C:\Program Files\ampl[/b]

 

As the script continues onto the next folder, it's overwriting the previous:

C:\Windows\System32>pathPATH=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;
...
C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
[b]C:\Program Files\ampl\cplex[/b]

C:\Windows\System32>pathPATH=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;
...
C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
[b]C:\Program Files\ampl\xpress[/b]

 

Here is an example of the crude PowerShell script I'm using (Note: I'm very new to PowerShell, go easy on me)

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$ampl_path = "C:\Program Files\ampl"
if( $PATH -notlike "*"+$ampl_path+"*" ){
   [Environment]::SetEnvironmentVariable("PATH", "$PATH;$ampl_path", "Machine")
}
$cplex_path = "C:\Program Files\ampl\cplex"
if( $PATH -notlike "*"+$cplex_path+"*" ){
   [Environment]::SetEnvironmentVariable("PATH", "$PATH;$cplex_path", "Machine")
}
$xpress_path = "C:\Program Files\ampl\xpress"
if( $PATH -notlike "*"+$xpress_path+"*" ){
   [Environment]::SetEnvironmentVariable("PATH", "$PATH;$xpress_path", "Machine")
}

Is it possible to add the parent AMPL as well as the subfolders CPLEX and XPRESS to the path? Or should I just use the alternative method of dumping all the executables + associated files for these command line tools in one parent folder?

Posted (edited)

$env:Path will return what is in the Path Enviroment Variables in PowerShell.

 

To add to env:Path you need to read out the existing path and then add your new path(s) so a bit like this:-

 

$envReg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"

$envNewPath = "c:\Program Files\ampl\xpress;c:\Program Files\ampl"

$envOldPath = (Get-ItemProperty -Path "$envReg" -Name PATH).Path

$envNewPath = $envOldPath + ’;’ + $envNewPath

Set-ItemProperty -Path "$envReg" -Name PATH –Value $envNewPath -Confirm

 

Or if you just need a process level Enviroment variable

 

$env:xpressPath = "c:\Program Files\ampl\xpress"

 

Think that makes sense! I would guess you would be best updating the Path env with the new paths as in the first example.

 

I am not getting your matching logic "*"+$var"*" anyhow I am not on a device with >_ and have had the best part of 4 hours! sleep in the past 24, not firing on all cylinders!

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...