-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappend_if_missing.vbs
More file actions
83 lines (66 loc) · 2.13 KB
/
append_if_missing.vbs
File metadata and controls
83 lines (66 loc) · 2.13 KB
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
Sub Assert(boolCheck, strMessage)
If boolCheck Then
WScript.Echo "Assert:", strMessage
Rem WScript.Echo "Assert:", strMessage
End If
End Sub
If WScript.Arguments.Count <= 0 Then
Rem no argument given
Assert True, "Needs 1 ore more arguments. Put it between Quotes if necessary"
WScript.Quit(1)
End If
Set objShl = CreateObject("WScript.Shell")
Set objEnv = objShl.Environment("SYSTEM")
Set objFso = CreateObject("Scripting.FileSystemObject")
sysVarName = "Path"
For Each addPath In WScript.Arguments
Do
curPath = objEnv(sysVarName)
REM addPath = WScript.Arguments.Item(0)
If Not objFso.FolderExists(addPath) Then
Assert True, "Not a valid path"
Exit Do
End If
If curPath = "" Then
curPath = ""
End If
lenAPath = Len(addPath)
lenCPath = Len(curPath)
diffPath = lenCPath - lenAPath
dontAssert = False
lackPath = True
Rem StartsWith
lackPath = lackPath And ( InStr(1, curPath, addPath & ";") <> 1 )
lackPath = lackPath And ( InStr(1, curPath, ";" & addPath & ";") <> 1 )
Assert ( Not lackPath ) And Not dontAssert, "StartsWith"
dontAssert = Not lackPath
Rem EndsWith
lackPath = lackPath And ( InStr(1, curPath, ";" & addPath ) <> diffPath )
lackPath = lackPath And ( InStr(1, curPath, ";" & addPath & ";") <> diffPath - 1 )
Assert ( Not lackPath ) And Not dontAssert, "EndsWith"
dontAssert = Not lackPath
Rem IsEqual
lackPath = lackPath And ( curPath <> addPath )
lackPath = lackPath And ( curPath <> ";" & addPath )
lackPath = lackPath And ( curPath <> ";" & addPath & ";" )
lackPath = lackPath And ( curPath <> addPath & ";" )
Assert ( Not lackPath ) And Not dontAssert, "IsEqual"
dontAssert = Not lackPath
Rem Contains
lackPath = lackPath And ( InStr(1, curPath, ";" & addPath & ";") = 0 )
Assert ( Not lackPath ) And Not dontAssert, "Contains"
dontAssert = Not lackPath
If lackPath Then
Assert True, "Appending Path"
prePath = ";"
If Right(curPath, 1) = ";" Then
prePath = ""
End If
newPath = curPath & prePath & addPath & ";"
Assert True, newPath
objEnv(sysVarName) = newPath
Else
Assert Not lackPath, "Found the path"
End If
Loop While False
Next