-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetEnvironment.vbs
executable file
·332 lines (292 loc) · 12.2 KB
/
SetEnvironment.vbs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Windows Environment Setup Tool '
' @author:Lonelyer '
' @Email: hexiyou.cn@gmail.com '
' @date:2015-06 '
' '
' description:Add or Remove Path Environment or setup Others Environment variables. '
' (Java Environment etc..) '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Argvc,sysEnv,userEnv,quietMode,fsoObject,argDictionary
Const TristateUseDefault = -2
quietMode = False
Function Init()
If VarType(sysEnv)=0 Then Set sysEnv = CreateObject("WScript.Shell").Environment("SYSTEM")
If VarType(userEnv)=0 Then Set userEnv = CreateObject("WScript.Shell").Environment("USER")
End Function
Function InitFso()
If VarType(fsoObject)=0 Then Set fsoObject = CreateObject("scripting.filesystemobject")
End Function
Function Destory()
If Not sysEnv Is Nothing Then Set sysEnv = Nothing
If Not userEnv Is Nothing Then Set userEnv = Nothing
End Function
Sub MainEnter()
Dim Args,CmdArr,i,keyIndex
Argvs = ArgumentsToString()
If InStr(Argvs,"-q")>0 Or InStr(Argvs,"-quiet")>0 Then
quietMode = True
'use cscript run myself on quietMode is open
If Right(LCase(WScript.FullName),11)<>"cscript.exe" Then
RunCmd = "cscript //nologo " & WScript.ScriptFullName & " " & Replace(Argvs,"|"," ")
CreateObject("Wscript.Shell").run RunCmd
WScript.Quit
End If
End If
Set argDictionary = CreateObject("Scripting.Dictionary")
CmdArr = Split(Argvs,"|")
keyIndex = 0
For i=0 To UBound(CmdArr)
If CmdArr(i)<>"-q" And CmdArr(i)<>"-quiet" Then
argDictionary.Add keyIndex,CmdArr(i)
keyIndex = keyIndex+1
End If
Next
Call Main()
End Sub
Function Main()
Argvc = argDictionary.Count
Select Case Argvc
Case 3
Select Case LCase(argDictionary.Item(0))
Case "-env","--env","-set","--set"
Call SetEnv(argDictionary.Item(1),argDictionary.Item(2))
Case Else
Call PrintMsg("Involid Command Option, please check!",True)
End Select
Case 2
Select Case LCase(argDictionary.Item(0))
Case "-add","-append","--add","--append"
Call AddPath(argDictionary.Item(1))
Case "-insert","-prepend","--insert","--prepend"
Call InsertPath(argDictionary.Item(1))
Case "-export","--export"
Call ExportPath(argDictionary.Item(1))
Case "-remove","-del","--remove","--del"
Call RemovePath(argDictionary.Item(1))
Case "-read","--read"
Call ReadEnv(argDictionary.Item(1))
Case "-outhelp"
Call ExportHelpInfo(argDictionary.Item(1))
Case Else
Call PrintMsg("Involid Command Option, please check!",True)
End Select
Case 1
Call ParseSingleParam(argDictionary.Item(0))
Case Else
Call PrintUsage("Please use correct commandline options run this script!")
End Select
End Function
' Method:Add String To %PATH%
Sub AddPath(pathStr)
Call Init()
Dim oldPath,newPath,perAddPath
oldPath = sysEnv.Item("PATH")
perAddPath = pathStr
' check the path is exists or not,if exists do exit
If CheckPathHasExists(oldPath,pathStr)=True Then Call PrintMsg("The Path has Exists, script be quit now!",True)
newPath = oldPath
If HasSemi(oldPath,"end")=False And HasSemi(pathStr,"begin")=False Then newPath = newPath & ";"
'Add path at last
newPath = newPath & perAddPath
sysEnv.Item("PATH") = newPath
Call PrintMsg("The Path added to the %PATH% Environment." & vbCrLf & "+" & Space(8) & perAddPath, True)
End Sub
''
'' parse CommandLine action when only one option
Sub parseSingleParam(args)
Select Case args
Case "-h","/?","--help"
Call PrintUsage("Script example:")
Case "-export","--export"
Call ExportPath("pathenv_export.txt")
Case "-about"
Call showAbout()
Case "-outhelp"
Call ExportHelpInfo("setenv_help.txt")
Case Else
Call SinglePath(args)
'Call PrintMsg("Undefined commandline option " & args, True)
End Select
End Sub
'
'' description : insert path string at %PATH% beginning
Sub InsertPath(pathStr)
Call Init()
Dim oldPath,newPath,perAddPath
oldPath = sysEnv.Item("PATH")
perAddPath = pathStr
' check the path is exists or not,if exists do exit
If CheckPathHasExists(oldPath,pathStr)=True Then Call PrintMsg("The Path has Exists, script be quit now!",True)
newPath = oldPath
If HasSemi(oldPath,"begin")=False And HasSemi(perAddPath,"end")=False Then newPath = ";" & newPath
'Add path at last
newPath = perAddPath & newPath
'MsgBox newPath
sysEnv.Item("PATH") = newPath
Call PrintMsg("The Path inserted to the %PATH% Environment Now." & vbCrLf & Space(8) & perAddPath & " +", True)
End Sub
Sub RemovePath(pathStr)
Dim tmpReg
Call Init()
Dim oldPath,newPath
oldPath = sysEnv.Item("PATH")
Set tmpReg = New RegExp
pathStr = Replace(pathStr, "\","\\")
pathStr = Replace(pathStr, ",","\,")
pathStr = Replace(pathStr, ".","\.")
pathStr = Replace(pathStr, "-","\-")
pathStr = Replace(pathStr, "_","\_")
pathStr = Replace(pathStr, "+","\+")
pathStr = Replace(pathStr, ":","\:")
pathStr = Replace(pathStr, "[","\[")
pathStr = Replace(pathStr, "]","\]")
pathStr = Replace(pathStr, "(","\(")
tmpReg.Pattern = ";?" & pathStr & "(;|$)"
tmpReg.IgnoreCase = True
tmpReg.Global = True
newPath = tmpReg.Replace(oldPath,"")
sysEnv.Item("PATH") = newPath
Call PrintMsg("The Path has be removed Now." & vbCrLf & Space(8) & perAddPath, True)
End Sub
'check the path is a file or a directory
Sub SinglePath(pathStr)
Call InitFso()
If fsoObject.FolderExists(pathStr)=True Then
Call AddPath(pathStr)
ElseIf fsoObject.FileExists(pathStr)=True Then
'if target is a file,Join parentfoldpath to PATH
Call AddPath(fsoObject.GetParentFolderName(pathStr))
Else
Call PrintMsg("missing params or undefined error,please check commandline option! ", True)
End If
End Sub
Sub ExportPath(filePath)
Call Init()
Call InitFso()
Set file = fsoObject.OpenTextFile(filePath,2,True, TristateUseDefault)
file.WriteLine "################## SYSTEM PATH Below ###################"
file.Write sysEnv.Item("PATH")
file.WriteBlankLines 1
file.WriteLine "################### USER PATH Below ####################"
file.Write userEnv.Item("Path")
file.WriteLine "################### End OutPut ####################"
file.Close
PrintMsg "PATH Environment be exported to the file!" & vbCrLf & " - " & filePath,True
Set File = Nothing
End Sub
Sub ExportHelpInfo(helpFile)
Call InitFso()
Set file = fsoObject.OpenTextFile(helpFile,2,True, TristateUseDefault)
file.Write "Usage: " &vbCrLf &_
"(FilePath|FolderPath) Add file or folder Absloute path to %PATH% Var,support mouse drag operation" & vbCrLf & vbCrLf &_
"-add, -append [Path] Add directory Path To %PATH% Var" & vbCrLf & vbCrLf &_
"-remove, -del [Path] Add directory Path From %PATH% Var" &vbCrLf & vbCrLf &_
"-insert, -prepend [Path] Insert directory Path To %PATH% Var at The Beginging of the value" & vbCrLf & vbCrLf & _
"-query Path Query path string in %PATH% Var,this operation allow use wildcard (* and ?)" & vbCrLf & vbCrLf & _
"-export [Filename|FilePath] Export the %PATH% Var to one file.(include USER Environment)" & vbCrLf & vbCrLf & _
"-env $ENV_Item Value Set value for other Environment VARS except %PATH% var" & vbCrLf & vbCrLf & _
"-read, --read $ENV_Item Read value of Environment VARS include %PATH% var" & vbCrLf & vbCrLf & _
"-h, --help, /? Show this help message box"
file.Close
PrintMsg "Help message be exported to the file!" & vbCrLf & " - " & helpFile,True
Set File = Nothing
End Sub
Sub ReadEnv(envItem)
Call Init()
Call PrintMsg(sysEnv.Item(envItem), False)
If quietMode=True Then
Call PrintMsg(vbCrLf, False)
End If
'''--------------- print user env,by user custom
Call PrintMsg(userEnv.Item(envItem), True)
End Sub
'设置单项环境变量为固定值
Sub SetEnv(envItem,envValue)
Call Init()
If envItem<>"" AND envValue<>"" Then
sysEnv.Item(envItem) = envValue
End If
Call PrintMsg("The Environment Var """ & envItem & """ has been set to." & vbCrLf & "+" & Space(8) & envValue, True)
End Sub
Sub showAbout()
PrintMsg "This is an Environment Setting Tool use VBScript programing" & vbCrLf & vbCrLf & _
Space(58) & "- Author: lonely" & vbCrLf & _
Space(45) & "- Email: hexiyou.cn@gmail.com " & vbCrLf & _
Space(56) & "- Date: 2015-06-15", _
True
End Sub
Function CheckPathHasExists(EnvPath,pathStr)
Set tmpReg = New RegExp
pathStr = Replace(pathStr, "\","\\")
pathStr = Replace(pathStr, ",","\,")
pathStr = Replace(pathStr, ".","\.")
pathStr = Replace(pathStr, "-","\-")
pathStr = Replace(pathStr, "_","\_")
pathStr = Replace(pathStr, "+","\+")
pathStr = Replace(pathStr, ":","\:")
pathStr = Replace(pathStr, "[","\[")
pathStr = Replace(pathStr, "]","\]")
pathStr = Replace(pathStr, "(","\(")
pathStr = Replace(pathStr, ")","\)")
tmpReg.Pattern = "" & pathStr & "(;|$)"
tmpReg.Global = True
tmpReg.IgnoreCase = True
CheckPathHasExists = tmpReg.Test(EnvPath)
Set tmpReg = Nothing
End Function
Function checkSinglePath(pathStr)
Set tmpReg = New RegExp
tmpReg.Pattern = "^[^;]+$"
tmpReg.Global = True
checkSinglePath = tmpReg.Test(pathStr)
Set tmpReg = Nothing
End Function
Function HasSemi(Str,Pos)
Set tmpReg = New RegExp
Select Case LCase(Pos)
Case "end"
TmpReg.Pattern = ";$"
Case "begin"
TmpReg.Pattern = "^;"
Case Else
TmpReg.Pattern = "^.*([.]+;)+;$"
End Select
tmpReg.Global = True
tmpReg.IgnoreCase = True
HasSemi = TmpReg.Test(Str)
Set tmpReg = Nothing
End Function
Function ArgumentsToString()
Dim tmpArr(),i
i=0
For Each Arg In WScript.Arguments
ReDim Preserve tmpArr(i)
tmpArr(i)=Arg
i=i+1
Next
ArgumentsToString = Join(tmpArr,"|")
End Function
Sub PrintUsage(Msg)
Wscript.Echo Msg & vbCrLf & "Usage: " &vbCrLf &_
"(FilePath|FolderPath) Add file or folder Absloute path to %PATH% Var,support mouse drag operation" & vbCrLf & vbCrLf &_
"-add, -append [Path] Add directory Path To %PATH% Var" & vbCrLf & vbCrLf &_
"-remove, -del [Path] Add directory Path From %PATH% Var" &vbCrLf & vbCrLf &_
"-insert, -prepend [Path] Insert directory Path To %PATH% Var at The Beginging of the value" & vbCrLf & vbCrLf & _
"-query Path Query path string in %PATH% Var,this operation allow use wildcard (* and ?)" & vbCrLf & vbCrLf & _
"-export [Filename|FilePath] Export the %PATH% Var to one file.(include USER Environment)" & vbCrLf & vbCrLf & _
"-env $ENV_Item Value Set value for other Environment VARS except %PATH% var" & vbCrLf & vbCrLf & _
"-read, --read $ENV_Item Read value of Environment VARS include %PATH% var" & vbCrLf & vbCrLf & _
"-h, --help, /? Show this help message box" _
,64,"Usage"
End Sub
Sub PrintMsg(Msg,isExit)
If Not quietMode Then
MsgBox Msg,64,"Message Tip"
Else
WScript.Echo Msg
End If
If isExit=True Then WScript.Quit
End Sub
Call MainEnter()