Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append to Path within batch

Status
Not open for further replies.

TinCanMan

MIS
Jan 16, 2003
7
US
I need to append a string to the existing system (machine) path variable from within a batch file on a Win2k sp4 system. Set and setx don't work because they work with the user path or are only active from within that shell. I tried using setenv and that didn't work either, although the help file implies you can do that. It errored out when enclosing the existing ath in % signs and ignored enclosing them in quotes. Any suggestions?
 
You should be able to do it something like:

PATH %PATH%;C:\NewFolder

or

SET PATH=%PATH%;C:\NewFolder

John
 
Use the reskit tool pathman.exe:

Pathman accepts four command-line arguments, as follows.

/as path[;path[;path ...]]
Adds paths to the system path.

/au path[;path[;path ...]]
Adds paths to the user path.

/rs path[;path[;path ...]]
Removes the semicolon-separated paths from the system path.

/ru path[;path[;path ...]]
Removes the semicolon-separated paths from the user path.


Technical Notes

Pathman accepts multiple arguments in a single call. Identify each argument with one of the four command-line switches (/as, /au, /rs, and /ru). Each switch can be added in any order as many times as necessary. For each argument, Pathman can accept any number of paths to modify, separated by semicolons.


The /as and /au switches do not add a path that already exists. The /rs and /ru switches do nothing if the path does not currently exist.


Once the path modifications have been made, Pathman broadcasts a message to all top-level windows notifying them that the environment has changed. This causes many applications, including the system shell, to update their environments, thereby obtaining the modified path.


To ensure downward compatibility, Windows NT also loads path information from the Autoexec.bat file in the root directory. However, Pathman does not manage information contained in Autoexec.bat.

Examples
Example 1
pathman /au c:\temp;c:\users\name;d:\utils

In this example, the /au flag tells Pathman to add paths to the current user’s path. All three paths are added to the user’s path if they do not already exist.

Example 2
pathman /au c:\temp;c:\users\name;d:\utils /ru d:\data /as d:\data;c:\reskit

In this example, Pathman adds paths to the current user’s path, removes d:\data from the current user’s path, and adds d:\data and c:\reskit to the system path.

Example 3
pathman /au c:\temp;c:\app\bin /rs %systemroot% /au c:\app2\bin

This command adds c:\temp, c:\app\bin, and c:\app2\bin to the current user's path and removes (%systemroot%) from the system path.

Path Abnormalities
Pathman can handle many path abnormalities. These include:

• Additional leading semicolons

• Additional trailing semicolons

• Multiple consecutive semicolons

• Addition of duplicate paths using /au or /as

• Removal of duplicate paths using /ru or /rs

• Addition and removal of the same path (results in removal)

• Addition of a path that already exists

• Removal of a path that already exists


Pathman does not check the validity of new paths.

Direct download:
 
John:

Thanks for the reply. Set doesn't work in this instance because it only sets the user path in the current environment. I need to set the machine path.
 
bcastner:

Thanks for the reply. That looks something like what I'd like to use. You said pathman comes with the reskit. I have the reskit for NT, 2k and 2k server. Haden't stumbled on pathman yet. Looks like I need to go back and check it out.
If this runs OK from w/i a batch I may have a solution. Thanks again.

TinCanMan
 
Hello TinCanMan,

To add (system) path via vbscript, you can do it this way.
Code:
'[green]new paths to add, edit by expanding it or contracting it[/green]
[blue]apath=array("c:\cygwin", "c:\mysql\bin")[/blue]

set wshshell=createobject("wscript.shell")
'win2000-specific
skey="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
svn="Path"
on error resume next
s=wshshell.regread(skey & "\" & svn)
if err.number<>0 then
    wscript.echo "Vital valuename not existing. Operation aborted."
    wscript.quit(1)
end if
for each spath in apath
    if instr(1,s,spath,1)<>0 then
        wscript.echo "The folder " & spath & " is already present in the path."
    else
        if len(s)=0 then s=spath else s=s & ";" & spath
        wshshell.regwrite skey & "\" & svn, s, "REG_EXPAND_SZ"
        wscript.echo "The folder " & spath & " is added to the path."
    end if
next
set wshshell=nothing
To incorporate it with a going batch, you can make the above some .vbs file, say addpath.vbs. In the batch, you add at appropriate moment:
Code:
::above some stuff
start cscript.exe addpath //B //Nologo 
::continue with some other stuff
regards - tsuji
 
Thanks tsuji:

I'm not a VB script writer but a co-worker is and I can pass that info on. We occasionally write VB script for various needs.
 
TinCanMan,

It's me pleasure to have your colleague to evaluate the script. Just ask a favor to add the following line to make it better suit my preferred style, forgotton to add during editing.
Code:
end if                    '<<<below this line
[COLOR=green]on error goto 0[/color]           '<<<add this line to this location
for each spath in apath   '<<<above this line
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top