-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathEnvironment.ahk
235 lines (204 loc) · 9.25 KB
/
Environment.ahk
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
; Script: Environment.ahk
; Author: iseahound
; Date: 2017-02-11
; Recent: 2017-11-12
;
; ExpandEnvironmentStrings(), RefreshEnvironment() by NoobSawce + DavidBiesack (modified by BatRamboZPM)
; https://autohotkey.com/board/topic/63312-reload-systemuser-environment-variables/
;
; RPath_Absolute() Modified by Iggy_
; https://autohotkey.com/board/topic/17922-func-relativepath-absolutepath/page-3
; Global Error Values
; 0 - Success.
; -1 - Error when writing value to registry.
; -2 - Value already added or value already deleted.
; -3 - Need to Run As Administrator.
Env_UserAdd(name, value, type := "", location := ""){
RegRead, registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
if (!ErrorLevel) {
Loop, parse, registry, `;
{
if (A_LoopField == value)
return -2
}
registry .= (registry ~= "(;$|^$)") ? "" : ";"
value := registry . value
}
type := (type) ? type : (value ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
RegWrite, % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % value
SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE
RefreshEnvironment()
return (ErrorLevel) ? -1 : 0
}
Env_SystemAdd(name, value, type := ""){
return (A_IsAdmin) ? Env_UserAdd(name, value, type, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
Env_UserSub(name, value, type := "", location := ""){
RegRead, registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
if ErrorLevel
return -2
Loop, parse, registry, `;
{
if (A_LoopField != value) {
output .= (A_Index > 1 && output != "") ? ";" : ""
output .= A_LoopField
}
}
if (output != "") {
type := (type) ? type : (output ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
RegWrite, % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % output
}
else
RegDelete, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE
RefreshEnvironment()
return (ErrorLevel) ? -1 : 0
}
Env_SystemSub(name, value, type := ""){
return (A_IsAdmin) ? Env_UserSub(name, value, type, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
Env_UserNew(name, value := "", type := "", location := ""){
type := (type) ? type : (value ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
RegWrite, % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % value
SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE
RefreshEnvironment()
return (ErrorLevel) ? -1 : 0
}
Env_SystemNew(name, value := "", type := ""){
return (A_IsAdmin) ? Env_UserNew(name, value, type, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
; Value does nothing except let me easily change between functions.
Env_UserDel(name, value := "", location := ""){
RegDelete, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE
RefreshEnvironment()
return 0
}
Env_SystemDel(name, value := ""){
return (A_IsAdmin) ? Env_UserDel(name, value, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
Env_UserRead(name, value := "", location := ""){
RegRead, registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
if (value) {
Loop, parse, registry, `;
{
if (A_LoopField = value) {
return A_LoopField
}
}
return ; Value not found
}
return registry
}
Env_SystemRead(name, value := ""){
return Env_UserRead(name, value, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
}
; Value does nothing except let me easily change between functions.
Env_UserSort(name, value := "", location := ""){
RegRead, registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
Sort, registry, D`;
type := (type) ? type : (registry ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
RegWrite, % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % registry
return (ErrorLevel) ? -1 : 0
}
Env_SystemSort(name, value := ""){
return (A_IsAdmin) ? Env_UserSort(name, value, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
; Value does nothing except let me easily change between functions.
Env_UserRemoveDuplicates(name, value := "", location := ""){
RegRead, registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
Sort, registry, U D`;
type := (type) ? type : (registry ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
RegWrite, % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % registry
return (ErrorLevel) ? -1 : 0
}
Env_SystemRemoveDuplicates(name, value := ""){
return (A_IsAdmin) ? Env_UserRemoveDuplicates(name, value, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") : -3
}
Env_UserBackup(fileName := "UserEnvironment.reg", location := ""){
_cmd .= (A_Is64bitOS <> A_PtrSize >> 3) ? A_WinDir "\SysNative\cmd.exe" : ComSpec
_cmd .= " /K " Chr(0x22) "reg export " Chr(0x22)
_cmd .= (location == "") ? "HKEY_CURRENT_USER\Environment" : location
_cmd .= Chr(0x22) " " Chr(0x22)
_cmd .= fileName
_cmd .= Chr(0x22) . Chr(0x22) . " && pause && exit"
try RunWait % _cmd
catch
return "FAIL"
return "SUCCESS"
}
Env_SystemBackup(fileName := "SystemEnvironment.reg"){
return Env_UserBackup(fileName, "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
}
Env_UserRestore(fileName := "UserEnvironment.reg"){
try RunWait % fileName
catch
return "FAIL"
return "SUCCESS"
}
Env_SystemRestore(fileName := "SystemEnvironment.reg"){
try RunWait % fileName
catch
return "FAIL"
return "SUCCESS"
}
RefreshEnvironment()
{
Path := ""
PathExt := ""
RegKeys := "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment,HKCU\Environment"
Loop, Parse, RegKeys, CSV
{
Loop, Reg, %A_LoopField%, V
{
RegRead, Value
If (A_LoopRegType == "REG_EXPAND_SZ" && !ExpandEnvironmentStrings(Value))
Continue
If (A_LoopRegName = "PATH")
Path .= Value . ";"
Else If (A_LoopRegName = "PATHEXT")
PathExt .= Value . ";"
Else
EnvSet, %A_LoopRegName%, %Value%
}
}
EnvSet, PATH, %Path%
EnvSet, PATHEXT, %PathExt%
}
ExpandEnvironmentStrings(ByRef vInputString)
{
; get the required size for the expanded string
vSizeNeeded := DllCall("ExpandEnvironmentStrings", "Str", vInputString, "Int", 0, "Int", 0)
If (vSizeNeeded == "" || vSizeNeeded <= 0)
return False ; unable to get the size for the expanded string for some reason
vByteSize := vSizeNeeded + 1
If (A_IsUnicode) { ; Only 64-Bit builds of AHK_L will return 8, all others will be 4 or blank
vByteSize *= 2 ; need to expand to wide character sizes
}
VarSetCapacity(vTempValue, vByteSize, 0)
; attempt to expand the environment string
If (!DllCall("ExpandEnvironmentStrings", "Str", vInputString, "Str", vTempValue, "Int", vSizeNeeded))
return False ; unable to expand the environment string
vInputString := vTempValue
; return success
Return True
}
; Modified: AbsolutePath
RPath_Absolute(AbsolutPath, RelativePath, s="\") {
len := InStr(AbsolutPath, s, "", InStr(AbsolutPath, s . s) + 2) - 1 ;get server or drive string length
pr := SubStr(AbsolutPath, 1, len) ;get server or drive name
AbsolutPath := SubStr(AbsolutPath, len + 1) ;remove server or drive from AbsolutPath
If InStr(AbsolutPath, s, "", 0) = StrLen(AbsolutPath) ;remove last \ from AbsolutPath if any
StringTrimRight, AbsolutPath, AbsolutPath, 1
If InStr(RelativePath, s) = 1 ;when first char is \ go to AbsolutPath of server or drive
AbsolutPath := "", RelativePath := SubStr(RelativePath, 2) ;set AbsolutPath to nothing and remove one char from RelativePath
Else If InStr(RelativePath,"." s) = 1 ;when first two chars are .\ add to current AbsolutPath directory
RelativePath := SubStr(RelativePath, 3) ;remove two chars from RelativePath
Else If InStr(RelativePath,".." s) = 1 { ;otherwise when first 3 char are ..\
StringReplace, RelativePath, RelativePath, ..%s%, , UseErrorLevel ;remove all ..\ from RelativePath
Loop, %ErrorLevel% ;for all ..\
AbsolutPath := SubStr(AbsolutPath, 1, InStr(AbsolutPath, s, "", 0) - 1) ;remove one folder from AbsolutPath
} Else ;relative path does not need any substitution
pr := "", AbsolutPath := "", s := "" ;clear all variables to just return RelativePath
Return, pr . AbsolutPath . s . RelativePath ;concatenate server + AbsolutPath + separator + RelativePath
}