Setting environment variables via Command Prompt from file - windows

Aside from setting N environment variables using these steps:
Open System Properties
Click Advanced system settings
Click Environment Variables
Create/Update/Delete Environment Variables
Is there a way to set multiple environment variables on a text file and load it on command prompt? The file would look like this:
SET <VariableName>=<PATH>
SET <VariableName>=<PATH>
SET <VariableName>=<PATH>
SET <VariableName>=<PATH>
SET <VariableName>=<PATH>
If so, what command should I run if the file would be saved as EnvironmentVariables.txt

Related

specify environment variables for conf file in windows cmd

My application.conf uses environment variables like this
cassandraUri = ${?DB_CASSANDRA_URI}
I am starting my Play application using the .bat file created by sbt dist. I have specified the value of DB_CASSANDRA_URI both in the .bat file and also in cmd.
set "PSK"="mysecret"
set "ALLOWED_NODES"="localhost"
set "DB_CASSANDRA_URI"="localhost"
set "DB_CASSANDRA_PORT"="9042"
set "DB_KEYSPACE_NAME"="ksname"
set "DB_USERNAME"="cassandra"
set "DB_PASSWORD"="cassandra"
set "EMAIL_SERVER"=""
set "EMAIL_USER"=""
set "EMAIL_PASSWORD"=""
set "MOCK_EMAIL"="true"
But the application still can't read the variables.
Am I setting the variables incorrectly? What is the right way?

Looking for a better way to set up Windows environment variables from batch file after installing a software

I am building a Windows .bat script to automatically compile and install the Geant4 toolkit from CERN (but my following questions are independent of which of software I want to deal with). What I managed to do so far seems to work OK-ish, but I am not satisfied with how the environment variables are set at the end of the script.
To complete the installation, I have to set up environment variables to indicate the path to required data-sets, and C++ include and library directories (I choose to modify the "PATH" variable for these lasts). I want to set them up for the current script (using set command) and for the next executions (using setx command)
The script I am using now to do that is the following:
REM to get the path to directory where this bat file is executed from.
set G4_bat_file_dir=%~dp0
REM set the environement variables for next cmd runs
REM adding to local (temporary) PATH
set G4dataset_RootDir="%G4_bat_file_dir%\install\share\Geant4-10.4.3\data\"
REM adding environment variables for current and next cmd executions
setx G4dataset_RootDir "%G4_bat_file_dir%\install\share\Geant4-10.4.3\data\"
setx G4ABLADATA %G4dataset_RootDir%\G4ABLA3.1
setx G4ENSDFSTATEDATA %G4dataset_RootDir%\G4ENSDFSTATE2.2
setx G4LEDATA %G4dataset_RootDir%\G4EMLOW7.3
setx G4LEVELGAMMADATA %G4dataset_RootDir%\PhotonEvaporation5.2
setx G4NEUTRONHPDATA %G4dataset_RootDir%\G4NDL4.5
setx G4NEUTRONXSDATA %G4dataset_RootDir%\G4NEUTRONXS1.4
setx G4PIIDATA %G4dataset_RootDir%\G4PII1.3
setx G4RADIOACTIVEDATA %G4dataset_RootDir%\RadioactiveDecay5.2
setx G4REALSURFACEDATA %G4dataset_RootDir%\RealSurface2.1.1
setx G4SAIDXSDATA %G4dataset_RootDir%\G4SAIDDATA1.1
set G4ABLADATA=%G4dataset_RootDir%\G4ABLA3.1
set G4ENSDFSTATEDATA=%G4dataset_RootDir%\G4ENSDFSTATE2.2
set G4LEDATA=%G4dataset_RootDir%\G4EMLOW7.3
set G4LEVELGAMMADATA=%G4dataset_RootDir%\PhotonEvaporation5.2
set G4NEUTRONHPDATA=%G4dataset_RootDir%\G4NDL4.5
set G4NEUTRONXSDATA=%G4dataset_RootDir%\G4NEUTRONXS1.4
set G4PIIDATA=%G4dataset_RootDir%\G4PII1.3
set G4RADIOACTIVEDATA=%G4dataset_RootDir%\RadioactiveDecay5.2
set G4REALSURFACEDATA=%G4dataset_RootDir%\RealSurface2.1.1
set G4SAIDXSDATA=%G4dataset_RootDir%\G4SAIDDATA1.1
setx Geant4_DIR %G4_bat_file_dir%\install\lib\Geant4-10.4.3\
REM adding to PATH the paths to libraries and includes for Qt4 and Geant4.
setx PATH "%G4_bat_file_dir%\install\lib;%G4_bat_file_dir%\install\bin;%G4_bat_file_dir%\xerces-c\instal\bin;%G4_bat_file_dir%\xerces-c\instal\lib;%G4_bat_file_dir%Qt4\install\bin;%G4_bat_file_dir%Qt4\install\lib;%PATH%"
The paths %G4_bat_file_dir%\install\lib;%G4_bat_file_dir%\install\bin;%G4_bat_file_dir%\xerces-c\instal\bin;%G4_bat_file_dir%\xerces-c\instal\lib;%G4_bat_file_dir%Qt4\install\bin;%G4_bat_file_dir%Qt4\install\lib being the ones I want to append.
This is a screenshot of the environment variables set-up I get after running the script 2 times :
http://djienne.free.fr/env.png
This is far from ideal, there several things that I am not happy with:
all the paths in the variables get fully expended, and then also the PATH variable gets too long and I get the error "WARNING: The data being saved is truncated to 1024 characters."
If I run the script twice in a row, it produces duplicates in the PATH entries (and everything above the 1024 characters limit is truncated)
also, if I put this code at the end of my main compilation/installation script it gives the error 'setx' is not recognized as an internal or external command, operable program or batch file. and so the environment variables are not created/modified. But if I run this script as a separate .bat file, it works. So there is something I don't understand.
(I specify that I always do "run as administrator" to run the scripts.)
Thanks in advance for the help.
Following the advice from the comments, I built a batch script launch_visual_studio.bat at the top level of my project, to launch Visual Studio with an updated local PATH. The file contains the code:
#echo off
REM Set the environment
set G4_bat_file_dir=%~dp0
set QTDIR=%G4_bat_file_dir%Qt5\qt-5.6.3\
set QMAKESPEC=win32-msvc2015
set Geant4_DIR=%G4_bat_file_dir%install\lib\Geant4-10.4.3\
REM split into two parts for readability
set PATH=%PATH%;%G4_bat_file_dir%install\bin;%G4_bat_file_dir%install\lib;%G4_bat_file_dir%install\include\Geant4
set PATH=%PATH%;%QTDIR%lib;%QTDIR%bin;%QTDIR%include
REM launch visual studio
"%vs140comntools%..\IDE\devenv.exe"
This works for visual studio 2015, but will be different for other versions.
For the environment variables other than PATH, QTDIR and Geant4_DIR, since they have very specific names (G4ABLADATA, G4ENSDFSTATEDATA, ...), it seems fine to set them permanently using setx, as shown previously.

Why are "Path" Windows's environment variable and the command line different?

On my Pc, running Windows XP 5.1.2600, following
"My Computer" > "Properties" > "Advanced" > "Environment Variables" > "Path"
I get a string, but in a batch file, or directly typing from prompt, by means of
echo %path%
I get a different one.
Can someone, please, explain what makes them differ and how to fix the issue, setting the "DOS-path" to the same value of "Windows-PATH" ?
Two things:
As others have mentioned, the full value of PATH is the combination of the user's PATH variable and the system's PATH variable.
If you change environment variables (including PATH), any processes already running (including the Command Prompt) will continue using the old environment variable values. You will need to restart those processes to pick up any changes that you've made.
The value of the PATH environment variable you see on the command line (which has nothing to do with "DOS" btw) is the sum of the system wide PATH variable and the user specific PATH variable.
If you open the dialog with the system variables from within the control panel, the upper part contains the user specific environment variables. The lower part contains the system variables.
If you set a PATH variable for your user to e.g. c:\foo, and the system path is be c:\Windows\System32 then you'd see c:\Windows\System32;c:\foo when you do an ECHO %PATH% on the commandline.
Probably the difference you are seeing is that when a new process is made, its default environment contains a PATH variable that is made by merging the system variable with the user variable.
As for how to "fix the issue", there is nothing to fix. The system behaves as designed.
I was getting the same issue where the System Variable that I get through the
"My Computer" > "Properties" > "Advanced" > "Environment Variables" >
"Path"
was different that the one being displayed through the command line interface via
ECHO %PATH%
The fix for this is basically to wait for the change to take place and this can be made faster by restarting the cmd and explorer. To do that you can simply end task for Explorer and CMD and then start a new task or restart it directly.
The same shall be done for the CMD as well.
The Windows registry can hold path entries that do not show up in the Environment Variables version of PATH you see via the control panel. (Why, I do not know).
The difference means that something (likely an install script) made changes to the registry.
To view or change the local user's path variable, look in the HKEY_CURRENT_USER\Environment key.
To view the system path, look in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

What is the difference between the Environmental Variables for Windows System and PowerShell?

We set the Windows System Path variable via System > Advanced System Settings > Environmental Variables > Path > Edit.
We set the PowerShell ones via Env:Path += ";Some\New\Path"
What, if any, is the difference between these two?
When you set the variable in powershell, it's set in that process (and its children) only. This is equivalent to using SET from the cmd prompt or a batch file.
In the dialog, you are setting the variables for the user (or for the system), which affects every process spawned under those contexts. This is analogous to using SETX from the cmd prompt or a batch file.

How to write a script/batch file on windows?

I am using a machine where I do not have administrator privileges and I need to set the e.g. JAVA_HOME each time I boot.
Thus, I'd like to write a script that sets the desired classpath automatically.
How do I do this?
You surely can set the environment variables with a batch file each time you need them, but bear in mind that this will only work for the current process (i. e. the cmd instance you're invoking the batch from) and processes spawned from there. I think you'd rather want persistent environment variables.
Now, in the UNIX world you'd just put them into your shell startup script. In Windows ... not exactly that way.
Right click on "My Computer", select "Properties".
There, go to the "Advanced" tab
Click the button labeled "Environment variables"
You can set JAVA_HOME and GRAILS_HOME under "User variables" there.
You would only need administrator access for changing the system environment variables.
You would want a file with .bat extension for Windows.
SET JAVA_HOME=path
SET GRAILS_HOME=path
On Windows, you could write a .BAT file (BATch), and then simply double-click it each time you log in. a .BAT file is simply a plain text file filled with any commands you would normally issue at the command prompt, e.g.:
cd c:\Temp
c:
set PATH=%PATH%;c:\Program Files\Some Directory\Some Subdirectory
You can optionally precede each line with the # symbol to stop it being echoed to the command window when it executes.
Try calling the following :
SETX JAVA_HOME path
SETX GRAILS_HOME path
If you're allowed they will be set in your profile and you won't need to run them again hopefully. Other wise you'll need to follow #Android's answer

Resources