|
|
|
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)
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")
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
If Not PathExists Then Do While Right(UserPath, 1) = ";" UserPath = Left(UserPath, Len(UserPath) - 1) Loop
If UserPath = "" Then UserPath = PathToAdd Else UserPath = UserPath & ";" & PathToAdd End If
uENV("path") = UserPath AddPath = True End If
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 ) |
|
|