使用vbs配置环境变量

开发工具
设置用户变量,如需设置系统变量在Environment内填写System(需管理员权限)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Set pSysEnv = CreateObject("WScript.Shell").Environment("User")

Function IsMatch(Str, Patrn)
Set r = new RegExp
r.Pattern = Patrn
IsMatch = r.test(Str)
End Function

Sub SetEnv(pPath, pValue)
Dim ExistValueOfPath
IF pValue <> "" Then
ExistValueOfPath = pSysEnv(pPath)
IF Right(pValue, 1) = "" Then pValue = Left(pValue, Len(pValue)-1)
If IsMatch(ExistValueOfPath, "*?" & Replace(pValue, "", "\") & "\?(b|;)") Then Exit Sub '已经存在该环境变量设置
If ExistValueOfPath <> "" Then pValue = ";" & pValue
pSysEnv(pPath) = ExistValueOfPath & pValue
Else
pSysEnv.Remove(pPath)
End IF
End Sub

Dim UserPath
UserPath = pSysEnv.Item("Path")
ZIP_HOME = "C:Program Files7-Zip"

IF Not IsMatch(UserPath, "%7Z_HOME%") Then
SetEnv "Path", ZIP_HOME
End If

MsgBox "Set environment variable for 7z successfully."