-
Notifications
You must be signed in to change notification settings - Fork 0
/
_CMDLib2-ToBeMigrated.cmd.bak
354 lines (331 loc) · 9.61 KB
/
_CMDLib2-ToBeMigrated.cmd.bak
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
352
353
354
@echo OFF
@if not “%ECHO%”==”” echo %ECHO%
@if not “%OS%”==”Windows_NT” goto DOSEXIT
rem If no arguments, show version information and exit
^ if “%1”==”” (
(echo Script MTP Script Library [%0] $Revision: 2 $)
(goto :EOF)
)
rem At least one argument, so dispatch to procedure
set _PROC=%1
shift /1
goto %_PROC%
rem //////////////////////////////////////////////////////////////////////
rem INIT procedure
rem Must be called in local state before other procs are used
rem
:INIT
if defined TRACE %TRACE% [proc %0 %*]
goto :EOF
rem /////////////////////////////////////////Part II: Real-World Scripting
rem VARDEL procedure
rem Delete multiple variables by prefix
rem
rem Arguments: %1=variable name prefix
rem
:VARDEL
if defined TRACE %TRACE% [proc %0 %*]
for /f “tokens=1 delims==” %%I in (‘set %1 2^>nul’) do set %%I=
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem PARSECMDLINE procedure
rem Parse a command line into switches and args
rem
rem Arguments: CMDLINE=command text to parse
rem %1=0 for new parse (def) or 1 to append to existing
rem
rem Returns: CMDARG_n=arguments, CMDSW_n=switches
rem CMDARGCOUNT=arg count, CMDSWCOUNT=switch count
rem RET=total number of args processed
rem
:PARSECMDLINE
if defined TRACE %TRACE% [proc %0 %*]
if not {%1}=={1} (
(call :VARDEL CMDARG_)
(call :VARDEL CMDSW_)
(set /a CMDARGCOUNT=0)
(set /a CMDSWCOUNT=0)
)
set /a RET=0
call :PARSECMDLINE1 %CMDLINE%
set _MTPLIB_T1=
goto :EOF
:PARSECMDLINE1
if {%1}=={} goto :EOF
set _MTPLIB_T1=%1
set _MTPLIB_T1=%_MTPLIB_T1:”=%
set /a RET+=1
shift /1
if “%_MTPLIB_T1:~0,1%”==”/” goto :PARSECMDLINESW
if “%_MTPLIB_T1:~0,1%”==”-” goto :PARSECMDLINESW
set /a CMDARGCOUNT+=1
set CMDARG_%CMDARGCOUNT%=%_MTPLIB_T1%
goto :PARSECMDLINE1
:PARSECMDLINESW
set /a CMDSWCOUNT+=1
set CMDSW_%CMDSWCOUNT%=%_MTPLIB_T1%
goto :PARSECMDLINE1
goto :EOF Chapter 5: A Scripting Toolkit
rem //////////////////////////////////////////////////////////////////////
rem GETARG procedure
rem Get a parsed argument by index
rem
rem Arguments: %1=argument index (1st arg has index 1)
rem
rem Returns: RET=argument text or empty if no argument
rem
:GETARG
if defined TRACE %TRACE% [proc %0 %*]
set RET=
if %1 GTR %CMDARGCOUNT% goto :EOF
if %1 EQU 0 goto :EOF
if not defined CMDARG_%1 goto :EOF
set RET=%%CMDARG_%1%%
call :RESOLVE
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem GETSWITCH procedure
rem Get a switch argument by index
rem
rem Arguments: %1=switch index (1st switch has index 1)
rem
rem Returns: RET=switch text or empty if none
rem RETV=switch value (after colon char) or empty
rem
:GETSWITCH
if defined TRACE %TRACE% [proc %0 %*]
(set RET=) & (set RETV=)
if %1 GTR %CMDSWCOUNT% goto :EOF
if %1 EQU 0 goto :EOF
if not defined CMDSW_%1 goto :EOF
set RET=%%CMDSW_%1%%
call :RESOLVE
for /f “tokens=1* delims=:” %%I in (“%RET%”) do (set RET=%%I) &
(set RETV=%%J)
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem FINDSWITCH procedure
rem Finds the index of the named switch
rem
rem Arguments: %1=switch name
rem %2=search start index (def: 1)
rem
rem Returns: RET=index (0 if not found)
rem RETV=switch value (text after colon) Part II: Real-World
Scripting
rem
:FINDSWITCH
if defined TRACE %TRACE% [proc %0 %*]
if {%2}=={} (set /a _MTPLIB_T4=1) else (set /a _MTPLIB_T4=%2)
:FINDSWITCHLOOP
call :GETSWITCH %_MTPLIB_T4%
if “%RET%”==”” (set RET=0) & (goto :FINDSWITCHEND)
-if /i “%RET%”==”%1” (set RET=%_MTPLIB_T4%) & (goto :FINDSWITCHEND)
set /a _MTPLIB_T4+=1
goto :FINDSWITCHLOOP
:FINDSWITCHEND
set _MTPLIB_T4=
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem REGSETM and REGSETU procedures
rem Set registry values from variables
rem
rem Arguments: %1=reg context (usually script name)
rem %2=variable to save (or prefix to save set of vars)
rem
:REGSETM
if defined TRACE %TRACE% [proc %0 %*]
for /f “tokens=1* delims==” %%I in (‘set %2 2^>nul’)
do call :REGSET1 HKLM %1 %%I “%%J”
goto :EOF
:REGSETU
if defined TRACE %TRACE% [proc %0 %*]
-for /f “tokens=1* delims==” %%I in (‘set %2 2^>nul’)
do call :REGSET1 HKCU %1 %%I “%%J”
goto :EOF
:REGSET1
set _MTPLIB_T10=%4
set _MTPLIB_T10=%_MTPLIB_T10:\=\\%
reg add %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nul
-reg update %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nul
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem REGGETM and REGGETU procedures
rem Get registry value or values to variables
rem
rem Arguments: %1=reg context (usually script name)
rem %2=variable to restore (def: restore entire context)
rem
rem Returns: RET=value of last variable loaded
rem
rem WARNING: The “delims” value in the FOR commands below is a TAB
Chapter 5: A Scripting Toolkit
rem character, followed by a space. If this file is edited by
rem an editor which converts tabs to spaces, this procedure
rem will break!!!!!
rem
:REGGETM
if defined TRACE %TRACE% [proc %0 %*]
for /f “delims= tokens=2*” %%I in
(‘reg query HKLM\Software\MTPScriptContexts\%1\%2 ^|find “REG_SZ”’)
do call :REGGETM1 %%I “%%J”
goto :EOF
:REGGETU
if defined TRACE %TRACE% [proc %0 %*]
for /f “delims= tokens=2*” %%I in
(‘reg query HKCU\Software\MTPScriptContexts\%1\%2 ^|find “REG_SZ”’)
do call :REGGETM1 %%I “%%J”
goto :EOF
:REGGETM1
set _MTPLIB_T10=%2
set _MTPLIB_T10=%_MTPLIB_T10:\\=\%
set _MTPLIB_T10=%_MTPLIB_T10:”=%
set %1=%_MTPLIB_T10%
set RET=%_MTPLIB_T10%
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem REGDELM and REGDELU procedures
rem Delete registry values
rem
rem Arguments: %1=reg context (usually script name)
rem %2=variable to delete (def: delete entire context)
rem
:REGDELM
if defined TRACE %TRACE% [proc %0 %*]
call :GETTEMPNAME
echo y >%RET%
reg delete HKLM\Software\MTPScriptContexts\%1\%2 <%RET% >nul
del %RET%
goto :EOF
:REGDELU
if defined TRACE %TRACE% [proc %0 %*]
call :GETTEMPNAME
echo y >%RET%
reg delete HKCU\Software\MTPScriptContexts\%1\%2 <%RET% >nul
del %RET%
goto :EOF
rem ////////////////////////////////////////Part II: Real-World Scripting
rem SRAND procedure
rem Seed the random number generator
rem
rem Arguments: %1=new seed value
rem
:SRAND
if defined TRACE %TRACE% [proc %0 %*]
set /a _MTPLIB_NEXTRAND=%1
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem RAND procedure
rem Get next random number (0 to 32767)
rem
rem Returns: RET=next random number
rem
:RAND
if defined TRACE %TRACE% [proc %0 %*]
if not defined _MTPLIB_NEXTRAND set /a _MTPLIB_NEXTRAND=1
set /a _MTPLIB_NEXTRAND=_MTPLIB_NEXTRAND * 214013 + 2531011
set /a RET=_MTPLIB_NEXTRAND ^>^> 16 ^& 0x7FFF
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem RESOLVE procedure
rem Fully resolve all indirect variable references in RET variable
rem
rem Arguments: RET=value to resolve
rem
rem Returns: RET=as passed in, with references resolved
rem
:RESOLVE
if defined TRACE %TRACE% [proc %0 %*]
:RESOLVELOOP
if “%RET%”==”” goto :EOF
set RET1=%RET%
for /f “tokens=*” %%I in (‘echo %RET%’) do set RET=%%I
if not “%RET%”==”%RET1%” goto :RESOLVELOOP
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem GETINPUTLINE procedure
rem Get a single line of keyboard input
rem
rem Returns: RET=Entered line
rem
:GETINPUTLINE
if defined TRACE %TRACE% [proc %0 %*]Chapter 5: A Scripting Toolkit
call :GETTEMPNAME
set _MTPLIB_T1=%RET%
copy con “%_MTPLIB_T1%” >nul
for /f “tokens=*” %%I in (‘type “%_MTPLIB_T1%”’) do set RET=%%I
if exist “%_MTPLIB_T1%” del “%_MTPLIB_T1%”
set _MTPLIB_T1=
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem GETSYNCFILE procedure
rem Get a sync file name (file will not exist)
rem
rem Returns: RET=Name of sync file to use
rem
:GETSYNCFILE
if defined TRACE %TRACE% [proc %0 %*]
call :GETTEMPNAME
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem SETSYNCFILE procedure
rem Flag sync event (creates the file)
rem
rem Arguments: %1=sync filename to flag
rem
:SETSYNCFILE
if defined TRACE %TRACE% [proc %0 %*]
echo . >%1
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem DELSYNCFILE procedure
rem Delete sync file
rem
rem Arguments: %1=sync filename
rem
:DELSYNCFILE
if defined TRACE %TRACE% [proc %0 %*]
if exist %1 del %1
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem WAITSYNCFILE
rem Wait for sync file to flag
rem
rem Arguments: %1=sync filename
rem %2=timeout in seconds (def: 60)
rem Part II: Real-World Scripting
rem Returns: RET=Timeout remaining, or 0 if timeout
rem
:WAITSYNCFILE
if defined TRACE %TRACE% [proc %0 %*]
if {%2}=={} (set /a RET=60) else (set /a RET=%2)
if exist %1 goto :EOF
:WAITSYNCFILELOOP
sleep 1
set /a RET-=1
if %RET% GTR 0 if not exist %1 goto :WAITSYNCFILELOOP
goto :EOF
rem //////////////////////////////////////////////////////////////////////
rem GETTEMPNAME procedure
rem Create a temporary file name
rem
rem Returns: RET=Temporary file name
rem
:GETTEMPNAME
if defined TRACE %TRACE% [proc %0 %*]
if not defined _MTPLIB_NEXTTEMP set /a _MTPLIB_NEXTTEMP=1
if defined TEMP (
(set RET=%TEMP%)
) else if defined TMP (
(set RET=%TMP%)
) else (set RET=%SystemRoot%)
:GETTEMPNAMELOOP
set /a _MTPLIB_NEXTTEMP=_MTPLIB_NEXTTEMP * 214013 + 2531011
set /a _MTPLIB_T1=_MTPLIB_NEXTTEMP ^>^> 16 ^& 0x7FFF
set RET=%RET%\~SH%_MTPLIB_T1%.tmp
if exist “%RET%” goto :GETTEMPNAMELOOP
set _MTPLIB_T1=
goto :EOF
rem These must be the FINAL LINES in the script...
:DOSEXIT
echo This script requires Windows NT