WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Environment

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Modify the Path in Windows NT

Keywords: modify the path Windows NT 3.51/4.0/2000/XP User Environment Path

The path in NT consists of the user environment (set under Current User in the registry) and the system environment (set under Local Machine in the registry) which are appended together by Windows as a composite system+user PATH.

Note that the path in Windows95 and/or WinNT can be modified a number of ways by users. For example, if you go into a DOS window, and click on the system menu (in the window title), and go into properties, you can see a number of ways to alter the PATH. You could specify a DOS batch file there, for ex, which changes the PATH. There is a utility in the NT Resource Kit that can help you to unravel this mess, and possibly see how the path is built.

Here's some sample code to modify path in Windows 3.51/4.0/2000/XP :

 
 ; Following is location of users path in Windows NT
 ; For the User Environment Path variable use:
    userenv="Environment[path]"
 ; For the System Environment Path variable use:
    sysenv="SYSTEM\CurrentControlSet\Control\Session Manager\Environment[path]"
 
    ; Directory to add to the path
    addpath="Q:\"
 
 if WinMetrics(-4)==4	 ;Check to make sure system is Windows NT 3.51.
    LastError()		 ;clear the errors
    Errormode(@off)	 ;tell WB we'll handle the errors
    oldpath=RegQueryExpSz(@RegCurrent, userenv)
    ErrorMode(@cancel)
 
    If LastError() == 1233
	 Message("Users Path", "No path found, replacement not made.")
    else
	 newpath=strcat(oldpath,";",addpath)
	 c=RegSetExpSz(@RegCurrent, userenv, newpath)
	 Message("Users new path is", newpath)
    endif
    exit
 else
    Message("RegQueryExpSz","Not supported by Non-NT registry")
 endif
 exit

To effect these changes without having to log off, broadcast a WM_WININICHANGE message to all windows in the system, so that any interested applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update.

The WININICHG is for telling running applications the PATH has changed. Newly run applications will pick up the new environment automatically.

This is done as follows:

IntControl (59, -1, "Environment", 0, 0)

OR
   WININICHG=26
   DaDll=Strcat(DirWindows(1),"USER32.DLL")
   DllCall( DaDll, long:"SendMessageA", long:-1, long:WININICHG, long:0, lpstr:"Environment") 
NOTE: This trick only works for Windows NT. The following registry updates in Win95 do work, but the Win95 registry editor cannot view them, and Win95 effectively ignores them.

For modifying the path in Windows 95, look closely at the RegApp function. It might do what you need in Win95.


Related Question:

I am needing to find a way to replace an existing path in the Windows NT path variables setting.

The example above appears to append the path variable but not search for an existing path and replace it.

For example, the path might read like this:

   %SystemRoot%\system32;%SystemRoot%;C:\SNA\system;g:\ceis;g:\rumbant\system;c:\winbatch;g:\site31
Is there a way to change g:\rumbant\system to g:\walldata\system without effecting the other path settings?

Answer:

You would read the path info (with RegQueryExpSz...), then:
	path=StrReplace(path,"g:\rumbant\system","g:\walldata\system")
and then write the path back out with RegSetExpSz.

Re: Using Volatile Environment PATH

Question:

The suggested procedure above doesn't work for me. I am using Windows NT 4.0 but I have no PATH entry in HKEY_CURRENT_USER\Environment

However, there is a PATH entry in

 
HKEY_CURRENT_USER\Volatile Environment
Should I use that instead? If I did, would the change be effective? Would I broadcast the change in the same way?

The PATH entry in Volatile Environment is a REG_SZ type, not a REG_EXPAND_SZ. Is this significant?

Answer:

>doesn't work for me. I am
>using Windows NT 4.0 but I
>have no PATH entry in
>HKEY_CURRENT_USER\Environment
That's normal. There's no default PATH for a user. You can create one through Control Panel/System or with Regedt32. You can use either REG_SZ or REG_EXPAND_SZ
>However, there is a PATH entry
>in
>HKEY_CURRENT_USER\Volatile
>Environment
Hmm, this key is normally absent on NT. It's created when you connect to a domain and only contains the value USERDOMAIN. Maybe, if you specify some policies or similar, there could be other values too. I wouldn't use it as it's values are cleared when logging off/and/or rebooting ...

I would still try using the Environment instead of the Volatile Environment. Especially if you want a permanent change.

Reg_expand_sz is like reg_sz except than in reg-expand-sz varialbles like %systemroot% are expanded into their proper values.

Also see the RegApp function.


Article ID:   W12913
Filename:   Changing the Path in WinNT.txt
File Created: 2004:07:13:08:07:12
Last Updated: 2004:07:13:08:07:12