-
Notifications
You must be signed in to change notification settings - Fork 23
/
PathEdit.cmd
351 lines (335 loc) · 9.19 KB
/
PathEdit.cmd
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PathEdit.cmd
:: From the desk of Frank P. Westlake, 2013-03-25-a
:: Interactive PATH editor. Edits the path in the machine, user, or
:: volatile environment and merges the three into the console's PATH
:: variable.
:: This script should function on Todd Vargo's computer.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "ME=%~n0"
Set "MESELF=%~f0"
Set "user=HKCU\Environment"
Set "machine=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Set "volatile=HKCU\Volatile Environment"
Set "key="
Set "Caption="
Set "testAccess=true"
If /I "/L" EQU "%~1" ( Set "work=!PATH!" & Set "Caption=LOCAL" & Set "testAccess="
) Else If /I "/U" EQU "%~1" ( Set "key=%user%" & Set "Caption=USER"
) Else If /I "/M" EQU "%~1" ( Set "key=%machine%" & Set "Caption=MACHINE"
) Else If /I "/V" EQU "%~1" ( Set "key=%volatile%" & Set "Caption=VOLATILE"
) Else If /I "/?" EQU "%~1" ( Call :Usage & EXIT /B 0
)
If DEFINED key ( Call :getRegistryVariable work "%key%" PATH
) Else If /I "!Caption!" NEQ "LOCAL" ( Call :printAll & EXIT /B 0
)
:loop
Call :printList
If NOT DEFINED caption EXIT /B 0
If DEFINED testAccess Call :testWriteAccess "%key%" && Set "testAccess=" || Goto :EOF
Call :showCommands
For /F "tokens=1 delims==" %%a in ('Set "#" 2^>NUL:') Do Set "%%a="
Set "command="
Set /P "command=ENTER: <command> [item number] "
Echo;
For /F %%a in ("%command%") Do Set "#=%%a"
If /I "!#!" EQU "A" ( Call :Add !command!
) Else If /I "!#!" EQU "C" ( CLS
) Else If /I "!#!" EQU "E" ( Call :Edit !command!
) Else If /I "!#!" EQU "I" ( Call :Insert !command!
) Else If /I "!#!" EQU "M" ( Call :Move !command!
) Else If /I "!#!" EQU "Q" ( EXIT /B 0
) Else If /I "!#!" EQU "R" ( Call :Remove !command!
) Else If /I "!#!" EQU "S" ( Goto :Save
) Else If /I "!#!" EQU "?" ( Call :help !command!
)
Goto :loop
For %%a in ("!work:;=" "!") Do (
Choice /M "Remove '%%~a'?"
If !ErrorLevel! EQU 2 (
Set "newPath=!newPath!%%~a;"
) Else If !ErrorLevel! NEQ 1 (
Goto :EOF
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Edit
If DEFINED work (
For /F "tokens=2*" %%a in ("%*") Do (
Set "#1=%%~a"
Set "#2=%%~b"
)
If !itemCount! LEQ 1 ( Set "#1=!itemCount!"
) Else If NOT DEFINED #1 ( Set /P "#1=Remove which entry?: "
)
If DEFINED #1 (
Set "#="
Set /A "i=0"
For %%a in ("!work:;=" "!") Do (
Set /A "i+=1"
If !i! EQU !#1! (
If DEFINED #2 (
Set "#=!#!!#2!;"
) Else (
Echo OLD=%%~a
Set "t=%%~a"
Set /P "t=NEW="
If DEFINED t (
Set "#=!#!!t!;"
) Else (
Set "#=!#!%%~a;"
)
Echo;
)
) Else (
Set "#=!#!%%~a;"
)
)
Set "work=!#!"
If "!work:~-1!" EQU ";" (Set "work=!work:~0,-1!")
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Save
Set "save=!work!"
If /I "!caption!" NEQ "LOCAL" (
Call :setRegistryVariable "%key%" path "%work%"
Set "work="
For %%a in (MACHINE USER VOLATILE) Do (
Set "p="
Call :getRegistryVariable p "!%%~a!" path
If DEFINED p (
Set "add=true"
For %%b in ("!work:;=" "!") Do (If /I "%%~b" EQU "!p!" Set "add=")
If DEFINED add (
If DEFINED work (
Set "work=!work!;!p!"
) Else (
Set "work=!p!"
)
)
)
If "!work:~-1!" EQU ";" (Set "work=!work:~0,-1!")
)
Set "save=!work!"
)
EndLocal & Set "PATH=%work%"
PATH
EXIT /B 0
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Move
For /F "tokens=2*" %%a in ("%*") Do (
Set "m1=%%a"
Set "m2=%%b"
)
If NOT DEFINED m1 (Set /P "#1=Remove which entry?: ")
If DEFINED m1 (
If NOT DEFINED m2 (Set /P "m2=Move to which line?: ")
If DEFINED m2 (
If !m1! GTR !m2! (
Call :remove R !m1!
If DEFINED removed (
Call :insert I !m2! !removed!
)
) Else If !m1! LSS !m2! (
Set /A "m=0"
For %%a in ("!work:;=" "!") Do (
Set /A "m+=1"
If !m! EQU !m1! (
Call :insert I !m2! %%~a
Call :remove R !m1!
)
)
)
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Add
For /F "tokens=1*" %%a in ("%*") Do (
Call :Insert I 0x7FFFFFF %%~b
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Insert
For /F "tokens=2*" %%a in ("%*") Do (
Set "#1=%%~a"
Set "#2=%%~b"
)
If !itemCount! LSS 1 ( Set "#1=1"
) Else If NOT DEFINED #1 ( Set /P "#1=Insert at which line?: "
)
If DEFINED #1 (
If NOT DEFINED #2 (Set /P "#2=New entry: ")
If DEFINED #2 (
If DEFINED work (
Set "#="
Set /A "i=1"
For %%a in ("!work:;=" "!") Do (
If !i! EQU !#1! (Set "#=!#!!#2!;")
Set "#=!#!%%~a;"
Set /A "i+=1"
)
If !i! LEQ !#1! (Set "#=!#!!#2!;")
Set "work=!#!"
) Else (
Set "work=!#2!"
)
If "!work:~-1!" EQU ";" (Set "work=!work:~0,-1!")
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Remove
Set "removed="
If DEFINED work (
For /F "tokens=1*" %%a in ("%*") Do (
Set "#1=%%b"
)
If !itemCount! LEQ 1 ( Set "#1=!itemCount!"
) Else If NOT DEFINED #1 ( Set /P "#1=Remove which entry?: "
)
If DEFINED #1 (
Set "#="
Set /A "i=0"
For %%a in ("!work:;=" "!") Do (
Set /A "i+=1"
Set "remove="
For %%b in (!#1!) Do (
If !i! EQU %%b (
Set "remove=true"
Set "removed=%%~a"
)
)
If NOT DEFINED remove (
If "%%~a" NEQ "" (Set "#=!#!%%~a;")
)
)
Set "work=!#!"
If "!work:~-1!" EQU ";" (Set "work=!work:~0,-1!")
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:showCommands
Echo;
Echo COMMANDS: ^
A=Add, ^
C=Clear, ^
E=Edit, ^
I=Insert, ^
M=Move, ^
Q=Quit, ^
R=Remove, ^
S=Save, ^
?=Help
Echo;
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:help
Echo COMMAND INSTRUCTIONS
Echo;Enter the command character followed, if necessary, by additional
Echo;information as specified below.
Echo A: Add a new entry to the end.
Echo Ex: A C:\bin
Echo C: Clear the screen and print the PATH list.
Echo E #: Edit the entry.
Echo Ex: E 8 C:\Bin
Echo I #: Insert a new entry at the indicated position.
Echo Ex: I 1 C:\bin
Echo M # #: Move the indicated entry to the indicated destination.
Echo Ex: M 1 5
Echo Q: Abandon all changes and quit. The PATH will be unchanged.
Echo R #: Remove the indicated entry from the path variable.
Echo Ex: R 4 6 8
Echo S: Save all changes to the PATH variable and quit. The machine,
Echo user, and volatile paths will be merged into this console's
Echo PATH variable.
Echo #= The line numbers and additional information may follow the command
Echo or they will be requested at a following prompt.
Echo;
Call :PAUSE
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:PAUSE
For /F "delims=" %%p in ('PAUSE^<NUL:') Do (
Set /P "=%%~p"<NUL:
XCOPY /L /W "%~f0" "%~f0" >NUL: 2>&1
For /F %%q in ('COPY /Z "%~f0" NUL:') Do (
Set /P "=.%%q %%q"<NUL:
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getRegistryVariable <var> <key> <value>
Set "%~1="
For /F "tokens=2*" %%a in ('reg QUERY "%~2" /V "%~3" 2^>NUL:') Do Set "%~1=%%b"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:setRegistryVariable <key> <value> <data>
If "%~3" NEQ "" (
reg ADD "%~1" /V "%~2" /D "%~3"
) Else (
reg DELETE "%~1" /V "%~2" /f 2>NUL:
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:TestWriteAccess <key>
Set "now=%DATE%T%TIME%"
reg ADD "%~1" /v "%now%" /D "%ME% testing write access." /F >NUL: && (
reg DELETE "%~1" /v "%now%" /F >NUL: 2>&1
EXIT /B 0
) || (
EXIT /B 5
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:printlist
Set "cap=%Caption% PATH ****************************************"
Echo %Cap:~0,40%
Echo;
If DEFINED work (
Set /A "itemCount=0"
For %%a in ("!work:;=" "!") Do (
Set /A "itemCount+=1"
Set "n= !itemCount!"
Echo !n:~-3!: %%~a
)
) Else (
Echo EMPTY
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:printAll
For %%a in (MACHINE USER VOLATILE) Do (
Set "caption=%%~a"
Call :getRegistryVariable work "!%%~a!" path
Echo;
Call :printList
)
Set "work=!PATH!"
Set "caption=LOCAL"
Echo;
Call :printList
Echo;
Echo Usage **********************************
Echo;
Call :Usage
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Usage
Echo Interactive PATH editor
Echo;
Echo %ME% [/L ^| /M ^| /U ^| /V]
Echo;
Echo /L Edit the local console's PATH.
Echo /M Edit the machine (system^) Registry PATH.
Echo /U Edit the user's Registry PATH.
Echo /V Edit the volatile Registry PATH.
Echo;
Echo The default is to display the current path in each then exit.
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::