Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to add path to the PATH environment variable using VB/VBScript

Total Hit ( 5392)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Many times we need to add application path to the existing environment PATH variable. This can be very tedious job if you have to do for several machine. I came up with some handy script which makes my life easy and hopefully you can use too.

The sample code was used in VB6 but you can use it with VBScript with little or no modification. This program adds sql binn directory to the path variable if path doesn't exist.

Click here to copy the following block
Private Sub Command1_Click()
  Dim strToolsPath
  strToolsPath = GetSQLClentToolsPath
  If AddPath(strToolsPath & "\BINN") Then
    MsgBox strToolsPath & "\BINN" & " : Path added"
  Else
    MsgBox strToolsPath & "\BINN" & " : Path already exist."
  End If
End Sub

Function AddPath(ByVal PathToAdd)
  ' Adds a new path to the user path
  ' unless the path is already present
  ' in either user or system path.

  Dim oWSH, uENV, pENV, UserPath, PathElement, PathExists, OldPath

  Set oWSH = CreateObject("Wscript.Shell")
  Set uENV = oWSH.Environment("USER")
  Set pENV = oWSH.Environment("PROCESS")
  UserPath = uENV("path")


  ' Check if path already exists:
  OldPath = Split(pENV("path"), ";", -1, vbTextCompare)
  PathExists = False
  For Each PathElement In OldPath
    If StrComp(PathElement, PathToAdd, vbTextCompare) = 0 Then
      PathExists = True

      AddPath = False
      Exit For
    End If
  Next


  ' Only do if path not already exists:
  If Not PathExists Then
    ' Strip off trailing semicolons if present:
    Do While Right(UserPath, 1) = ";"
      UserPath = Left(UserPath, Len(UserPath) - 1)
    Loop


    ' Add new path to current path:
    If UserPath = "" Then
      UserPath = PathToAdd
    Else
      UserPath = UserPath & ";" & PathToAdd
    End If


    ' Set the new path into environment:
    uENV("path") = UserPath
    AddPath = True
  End If

  ' Destroy the objects:
  Set uENV = Nothing
  Set pENV = Nothing
  Set oWSH = Nothing
End Function

Function GetSQLClentToolsPath()
  Dim wSH, sCurPath

  Set wSH = CreateObject("WSCript.shell")

  sCurPath = wSH.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\ClientSetup\SQLPath")
  GetSQLClentToolsPath = sCurPath
End Function



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.