修改环境变量并立即生效的VBS代码。
xxxxxxxxxx
1
Set pSysEnv = CreateObject("WScript.Shell").Environment("System")
2
3
'Check whether a character string matches a regular expression
4
' ^\w+[@]\w+[.]\w+$ E-MailAddress
5
' ^[0-9-]+$ Numeral
6
Function IsMatch(Str, Patrn)
7
Set r = new RegExp
8
r.Pattern = Patrn
9
IsMatch = r.test(Str)
10
End Function
11
12
Sub SetEnv(pPath, pValue)
13
Dim ExistValueOfPath
14
IF pValue <> "" Then
15
ExistValueOfPath = pSysEnv(pPath)
16
IF Right(pValue, 1) = "\" Then pValue = Left(pValue, Len(pValue)-1)
17
If IsMatch(ExistValueOfPath, "\*?" & Replace(pValue, "\", "\\") & "\\?(\b|;)") Then Exit Sub '已经存在该环境变量设置
18
If ExistValueOfPath <> "" Then pValue = ";" & pValue
19
pSysEnv(pPath) = ExistValueOfPath & pValue
20
Else
21
pSysEnv.Remove(pPath)
22
End IF
23
End Sub
24
25
'--------设置TOF目录---------
26
TOF = "D:\Workshop\tof\TOF_Common\Library\Tencent.OA.Framework.dll.config"
27
28
SetEnv "TOF", TOF
29
30
MsgBox "Set environment variable for TOF successfully."