SETX DOS command issue

February 6, 2009 at 02:31:27
Specs: Windows XP, 1GB
Hello Guys,

I need to set, rather edit the system environment variable PATH to include 2 additional paths say C:\TEST and C:\SETUP using DOS script. I'am doing the same using the SETX command (got by installing the WINXP service Pack2's Support Tools).

The script I use is
SETX PATH "%PATH%;C:\TEST;C:\SETUP" (current user level)
SETX PATH "%PATH%;C:\TEST;C:\SETUP" -M (machine level)

Now my issue is that the above command seems to dulplicate the values in the final path variable (when observed from control panel). That is, it seems as though the above command concatenates the entire path variable to obtain a resule of the form final path variable = original path variable + new path variable where new path variable = original + C:\TEST;C:\SETUP

Anyone out there got any clues on why this could be happenning?

See More: SETX DOS command issue

Report •

February 6, 2009 at 04:21:48
First, the PATH seen by a new process after you give those SETX commands is a concatenation of the System and User paths. You should only add new paths in the user path list, and not duplicate those in the system path list.

Second, there is a difference between %PATH% as seen by a batch file and the actual PATH value stored in the registry. %PATH% has all the variables expanded, so for instance if PATH is defined in terms of %SystemDrive% or %ProgramFiles% this will not be reflected when you type %PATH%.

Third, if you run that batch file more than once, it will keep adding the same paths to the list over and over again.

Fourth, you should use PATHMAN from the Resource Kit, it's more suited to what you're doing.

Report •

February 8, 2009 at 08:55:44
Thanks for that information klint. Will try PATHMAN from resource kit.

So, is it a SETX specific behaviour wherein the paths get added to the list over n over again, in the event of repeated execution of the script? Because I dont see the logic for setx to behave that way.

Report •

February 8, 2009 at 16:34:06
No, that behaviour is nothing to do with SETX itself. It's simply doing exactly what you tell it to. For example, imagine you do this:


Now %PATH% equates to C:\Windows\System32;C:\TEST

Now do it again, using the new value of PATH:


Now since %PATH% already had C:\TEST, and you are now adding another C:\TEST to the end of it, its new value is now C:\Windows\System32;C:\TEST;C:\TEST

So the more times you run that script the more repetitions of C:\TEST it will have.

Report •

Related Solutions

February 9, 2009 at 01:26:28
Thanks klint..although I've got another question

assume a batch script with the following set of commands:


:: do some task #1
:: do some task #2


:: do some task #3
:: do some task #4

What Iv'e observed is that, although the paths c:\test and c:\setup are reflected by the PATH variable (control panel>system>env variables) immediately after the first setx goes through, once the batch file executes the second setx command, the previously set values (c:\test;c:\setup) are lost, and only c:\temp is seen in the path variable.

So it seems like the first setx is not being persisted in the system's path variable.

Any idea on why this happens?

Report •

February 9, 2009 at 04:16:37
Yes, that is because SETX changes the value of the PATH environment in the Registry. The %PATH% variable you use in the batch file is the current process's copy of the PATH variable.

For background information, every time a process starts (in your case, the cmd.exe process) the environment is read from the Registry into the local process table. Anything that you do from then on in the Registry is not immediately reflected in any currently running process's environment table. But if you start up a new instance of cmd.exe (Command Prompt) you will get the updated value of the PATH.

Report •

February 9, 2009 at 04:39:29
thanks klint.

Report •

Ask Question