http://www.computerhope.co
Main Topics
Browse All TopicsWhat is the command to set a system environment variable from a batch command file ?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
http://www.computerhope.co
mordi, what exactly is it that you need to do with your batch file.
Knowiing these details we may be able to offer specific advice.
You should be aware that, if you just set a variable in a batch file that you then run from within Windows, it will only be held in memory for the duration of that batch file. If used from Full DOS, it will only last for the duration of that session. Set from somewhere like C:\Autoexec.bat, the variable should remain set for the duration of the Windows session.
Type the single command SET in a DOS Window to see the environment variables currently set on your system.
There is another utility file named WINSET.EXE from which you can set Windows environment variables that outlasts the batch file from which the command was run, and should remain for the duration of the Windows session.
Let us know what you are trying to do.
http://www.wilsonmar.com/1
But XP, not 98, has more pre-defined ones like %date% and %time%:
http://kennethhunt.com/arc
http://www.microsoft.com/r
mordi.
Yes, I do know. System environment variables are for global use and User environment variables are for the logged on user. The latter is mostly applicable to Windows NT-based systems or multi-user logons.
Here's some extracts of notes I previously made a while ago which you may find useful.
When setting a path in autoexec.bat or another batch file, I'm sure that there is a maximum limit of 127 or 128 characters. As I understand it, setting the path from config.sys does not have this limitation.
The Win9x Environment Space is limited, and you could easily exceed this and see an error message "Out of Environment Space".
You can increase the space by adding the following lines:
C:\CONFIG.SYS:
SHELL=C:\WINDOWS\COMMAND.C
C:\Windows\SYSTEM.INI under the [NonWindowsApp] section header:
CommandEnvSize=2424
If you set a variable that comprises a path containing spaces
eg: set BILLSPICS=C:\Windows\Profi
and you want to use the %BILLSDOCS% variable in a batch file, then enclose it in " " like this:
DIR /on /s "%BILLSPICS%\*.jpg" > %TEMP%\BillsJPGs.txt
Preferably, use the DOS names for variables using paths with spaces
eg: set BILLSPICS=C:\Windows\Profi
If an environment variable contains a directory name ending with a trailing backslash (\)
eg: set dir=c:\folder1\
COMMAND.COM will not accept the CD (change directory) command using the specified directory held in the variable. Avoid using a trailing \ in path variables.
If you redirected some program output that appends a \ to a path, and then tried to SET that as a variable, you COULD HAVE stripped off the trailing backslash on a Windows NT-based system using the following command, but I don't think there is a way in Windows 98:
set STRING=%1
if "%STRING:~-1%"=="\" set STRING=%STRING:~0,-1%
PATH TIP:
To clear all search-path settings other than current directory), use the following syntax:
PATH ;
You shouldn't have to use anything other than autoexec.bat, config.sys, and msdos.sys to set system environment variables, but if you do come upon any limitations then look at utility programs like this:
http://www.codeguru.com/cp
WINSET.EXE
----------
First of all, beware of rogue versions of Winset.exe:
http://www.pcreview.co.uk/
Winset Uses:
By Al Fasoldt:
http://aroundcny.com/Techn
By Eric Phelps:
Winset lets you set environment variables globally (they persist once your batch file ends).
ftp://ftp.microsoft.com/Pr
(self extracting .exe - unpack it to its own folder).
Note: Winset cannot be run from FULL DOS.
Other Notes (not sure if they are applicable to Windows 98 as well as 95):
The logon script processor for Windows NT logon scripts does not support setting environment variables in Windows 95. WINSET.EXE is a Win32 console application that overcomes this limitation. When called from a batch file (including an NT Logon script) it sets an environment variable in Window's global environment. Please note that WINSET.EXE does not set an environment variable in the batch file's environment, only Window's global environment. The syntax for WINSET.EXE is the same as the SET command in DOS, with one exception. It won't list all the environment variables set as you would see if you typed the SET command with no parameters.
Bill
My information doesn't only refer to setting the variables at system startup. That's the reason I mentioned the fact that the SET command run from a batch file within Windows will only last while that DOS window is open, or any other DOS window if the variable has been passed on to it.
That's also the reason I mentioned winset.exe ie. as a way to set environment variables that persist beyond the term of the batch file that called it.
You will find winset.exe in the ..\tools\reskit\scrpting folder of a Win98se CD. The file's properties describe the program file as "WINSET - Sets variables in the Windows 95 master environment". Yes, I know it says "Windows 95", but it works in Windows 98/98se or it wouldn't be on the CD.
WINSET [variable=[string]]
variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.
Try it out and compare the results with the SET command when used from a batch file within Windows.
Get the batch file to set one variable using the SET command, and another variable using WINSET then exit.
Run a 2nd batch file and have it show the current environment variables using the SET command with no parameters.
The variable set using WINSET will have persisted, while the one created with the SET command will have been lost.
If you need to change the variable several times, then just clear the variable's value and reset it to another value.
TEST1.BAT - Run this first
@echo off
:: Clear any existing values for both variables
set VAR1=
set VAR2=
winset VAR1=
winset VAR2=
::
set VAR1=Created with Set (VAR1)
winset VAR2=Created with Winset (VAR2)
::
exit
TEST2.BAT - Run this after TEST1.BAT terminates and closes the DOS window
@echo off
echo.
echo Environment Variables shown by the 'SET' command:
echo.
set
echo.
echo Do you see VAR1 listed above?
echo No, because it was set using the 'SET' command.
echo VAR2, however, was set using the 'WINSET' command.
echo.
echo Now 'echoing' the variables ...
echo.
echo Value of VAR1: " %VAR1% " - Do you see anything?
echo Value of VAR2: "%VAR2%" - Bet you see this value!
echo.
echo press any key to end ...
pause>nul
:: Clear variables again before terminating
set VAR1=
set VAR2=
winset VAR1=
winset VAR2=
exit
OK, so maybe that doesn't quite do what you need, but try it anyway.
Hi mordi
set VARIABLENAME=Value
Related info:
http://www.microsoft.com/r
http://www.microsoft.com/r
http://www.microsoft.com/r
http://support.microsoft.c
1. Log on with admin rights (any user can modify a "user" variable, but not a "system" one)
2. Right-click "My Computer" then click "Properties"
3. Click the Advanced tab
4. Click Environment variables
5. Options: "user" or "system" variable:
(a) New variable - to add a new variable name and value <------- This one **
(b) existing variable > Edit to change variable name or value
(c) existing variable > Delete to remove an existing variable
http://vlaurie.com/compute
Windows, DOS, and other operating systems - interest value :
http://en.wikipedia.org/wi
http://www.wilsonmar.com/1
Hope this helps
Bill
You still haven't given an example of a system variable you might need to change, so here's a method to change system or user environment variables from the command line.
Get your WinXP professional CD and navigate to the \SUPPORT\TOOLS\ folder. Unzip the file SUPPORT.CAB to its own folder with WinZip, or use WinXP's own decompression shell extension to extract the following two files from SUPPORT.CAB: setx.exe and suptools.chm.
The program file setx.exe is documented very briefly in the *.chm help file as follows:
>>>
This command-line tool offers administrators a batch method for setting environment variables in the user or system environment and requires no programming or scripting. In addition to taking an environment variable and its associated value from the command line, it can also get the values of registry keys and write them to text files.
You can use SetX to set values for user and system environment variables from one of three sources: Command Line Mode, Registry Mode, or File Mode.
SetX is similar to the UNIX utility SETENV.
SetX writes variables to the master environment in the registry.
Variables set by using SetX are available only in future command windows and not in the current command window.
SetX provides the only command-line or programmatic way to directly set system environment values for Windows XP. System environment variables can be manually configured by using Control Panel or by using Regedit. The Set command (which is part of the Windows operating system) only uses environment variables for the current console window.
<<<
You can redirect the syntax help from setx.exe by running the command:
setx /? > setx_syntax.txt
in the same folder as setx.exe.
I have detailed the output from this below
>>>
SETX:
This program is used to set values in the environment of the MACHINE or currently logged on USER using one of three modes.
1) Command Line Mode:
setx variable value [-m]
Optional Switches:
-m Set value in the Machine environment. Default is User.
2) Registry Mode:
setx variable -k hive\key\...\value
Optional Switches:
-m Set value in the Machine environment. Default is User.
3) File Mode: setx variable -f file {-a x,y | -r x,y "string"} [-d d] [-x] [-m]
Required Switches:
-f file : Specify file name to use.
-a x,y : Specify absolute coordinates and offset.
-r x,y "string" : Specify coordinates and offset relative to string.
Optional Switches
-d ,:\ etc. : Specify additional delimiters.
-x : Displays file coordinates. Switches -a -r -e ignored!!
-m : Set value in the Machine environment. Default is User.
<<<
For more information and examples use: SETX -i (output as follows):
>>>
SETX Examples:
===========
For the file type examples you must first create the file that you wish to parse by using "command > filename" ie ipconfig > ipconfig.out.
IMPORTANT: SETX writes variables to the master environment in the registry. Variables set using SETX are only available in future command windows and not in the current command window.
SETX Command Line Examples:
--------------------------
SETX MACHINE COMPAQ
Sets value of MACHINE to be COMPAQ in the users environment.
SETX MACHINE "COMPAQ COMPUTER" -m
Sets value of MACHINE to be "COMPAQ COMPUTER" in the machine environment.
SETX MYPATH %PATH%
Sets the value of MYPATH to the CURRENT value of the PATH environment variable.
SETX MYPATH ~PATH~
Sets the value of MYPATH to ALWAYS be equal to the value of the PATH environment
variable even in the event that the PATH variable changes.
SETX Registry Examples:
--------------------------
SETX TZONE -k HKEY_LOCAL_MACHINE\System\
Sets the value of TZONE to the above key ie. "Central Standard Time"
SETX BUILD -k "HKEY_LOCAL_MACHINE\Softwa
Sets the value of BUILD to the current Windows NT build ie. "1314"
Note: Quotes must be used because of the embedded space in "Windows NT".
SETX File Examples:
-------------------------
SETX VAR -f ipconfig.out -x
Displays the coordinates for the contents of the file "ipconfig.out".
SETX IPADDR -f ipconfig.out -a 5,11
Finds value at absolute offset 5,11 of the file ipconfig.out
Sets IPADDR to the IP Address (absolute offset 5,11)
SETX OCTET1 -f ipconfig.out -a 5,3 -d .
Finds value at absolute offset 5,3 and uses "." as an additional delimiter.
Sets OCTET1 to the first octet of the IP Address
SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
Finds value at relative offset 0,7 to the keyword "Gateway"
Sets IPGATEWAY to the first octet of the IP Address
<<<
If you wish to know the registry keys that hold the environment variables, then export the following keys to *.REG or *.TXT files and you will see the preconfigured ones, plus any you or other programs may have added:
[HKEY_LOCAL_MACHINE\System
[HKEY_CURRENT_USER\Environ
Hopefully this will help you do what you need to do, but if you need more info or perhaps some example CMD or BAT files then you'll get thousands of hits searching google.com for SETX.EXE
Bill
Business Accounts
Answer for Membership
by: callrsPosted on 2006-07-26 at 04:06:44ID: 17182915
set
:\E\HEX\01 0EDI~1"
e.g.
set PATH=D:\usr\bin\;%PATH%;"D
Then to use the variable e.g. in an IF statement, you have to use this syntax: %PATH%