-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtools_userInput.bat
More file actions
376 lines (336 loc) · 17.6 KB
/
tools_userInput.bat
File metadata and controls
376 lines (336 loc) · 17.6 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
::Author : Shen Xiaolong((xlshen@126.com))
::Copyright : free use,modify,spread, but MUST include this original two line information(Author and Copyright).
:: @set _Echo=1
:: set _Stack=%~nx0
@if {"%_Echo%"}=={"1"} ( @echo on ) else ( @echo off )
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo. & @echo [+++++ %~nx0] commandLine: %0 %*
where "%~nx0" 1>nul 2>nul || set "path=%~dp0;%path%"
if {%1}=={} call :Test NoOutput & goto End
call :%~1 %2 %3 %4 %5 %6 %7 %8 %9
goto End
::******************************DOS API section**************************************************************************
::[DOS_API:waitString]wait user to input string
::usage : call tools_userInput.bat waitString outVar bCanbeEmpty
::outVar : output var
::bCanbeEmpty : check input can be empty, non 1 mean cann't be empty -- return without any input (except \r\n)
::example : call tools_userInput.bat waitString myDir1 //myDir1 can not be empty
:: call tools_userInput.bat waitString myDir2 1 //myDir2 can be empty
:waitString
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call set %~1=
call :waitString.loop %~2
call set %~1=%_waitUserInput%
call tools_message.bat enableDebugMsg "%~0" "set %~1=%_waitUserInput%"
call set _waitUserInput=
goto :eof
::[DOS_API:waitNumber]wait user to input number, any non digital will be filtered
::usage : call tools_userInput.bat waitNumber outVar bCanbeEmpty
::outVar : output var
::bCanbeEmpty : check input can be empty, non 1 mean cann't be empty -- return without any input (except \r\n)
::example : call tools_userInput.bat waitNumber myDir1 //myDir1 can not be empty
:: call tools_userInput.bat waitNumber myDir2 1 //myDir2 can be empty
:waitNumber
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :waitString %*
if not defined %~1 goto :eof
call tools_isXXX.bat isNumber "%%%~1%%" _tmpIsNum
if not {"%_tmpIsNum%"}=={"1"} (
call tools_message.bat NotifyMsg "'%%%~1%%' is not one number, please only input number , range [0 -9 ]."
goto :waitNumber %*
)
goto :eof
::[DOS_API:waitPath] wait user to input one existed path.
::usage : call waitPath outVar checkFolderExist
::example : call waitPath outVar checkFileExist
:waitPath
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined waitPathPrompt echo %inputPathPrompt%
if defined waitPathPrompt2 echo %inputPathPrompt2%
call set %~1=
call :waitString %~1
set customized_exitHandler=echo retry...
call tools_error.bat %~2 "%%%~1%%"
if not {"%errorlevel%"}=={"1"} goto :waitPath %*
call tools_message.bat enableDebugMsg "%~0" "set %~1=%%%~1%%"
set customized_exitHandler=
goto :eof
::[DOS_API:waitPathFolder]wait user to input one file path, if non-current directory, MUST full path
::usage : call tools_userInput.bat waitPathFolder outVar
::outVar : output var
::example : call tools_userInput.bat waitPathFolder filePath
:waitPathFolder
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 1 %*
if defined waitPathFolderPrompt set inputPathPrompt=%waitPathFolderPrompt%
if defined waitPathFolderPrompt2 set inputPathPrompt2=%waitPathFolderPrompt2%
call :waitPath %* checkFolderExist
goto :eof
::[DOS_API:waitPathFile]wait user to input any file path, if non-current directory, MUST full path
::usage : call tools_userInput.bat waitPathFile outVar
::outVar : output var
::example : call tools_userInput.bat waitPathFile filePath
:waitPathFile
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 1 %*
if defined inputFilePrompt set inputPathPrompt=%inputFilePrompt%
if defined inputFilePrompt2 set inputPathPrompt2=%inputFilePrompt2%
call :waitPath %* checkFileExist
goto :eof
::[DOS_API:waitPathFileSpec]wait user to input specified(e.g.myApp.exe) file path, if non-current directory, MUST full path
::usage : call tools_userInput.bat waitPathFileSpec outVar
::outVar : output var
::example : call tools_userInput.bat myApp.exe filePath
:waitPathFileSpec
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 2 %*
if defined waitPathFileSpecPrompt echo %waitPathFileSpecPrompt%
if defined waitPathFileSpecPrompt2 echo %waitPathFileSpecPrompt2%
call :waitPathFileSpec.execute %*
goto :eof
::[DOS_API:waitBackspace]get the back space char, it is used to erase printed string in console window.
::usage : call tools_userInput.bat waitBackspace outVar
::outVar : output var
::example : call tools_userInput.bat waitBackspace bkChar
:: echo %myString%%bkChar%%bkChar% //will not display last chars in %myString%.
:: set /p "=12345" <nul & <nul set /p "=%bkChar%%bkChar%%bkChar%" //will only show 1234
:waitBackspace
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
for /F "tokens=1 delims=#" %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "%~1=%%~a"
goto :eof
::[DOS_API:waitBackspaceN]base on waitBackspace, get more than one char back space string.
::usage : call tools_userInput.bat waitBackspace outVar N
::outVar : output var
::example : call tools_userInput.bat waitBackspace bkChar 5
:: echo %myString%%bkChar%%bkChar% will not display last chars in %myString%.
:waitBackspaceN
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
set _tmpBkchar=
call :waitBackspace _tmpBkchar
for /L %%i in ( 1 1 %~2 ) do call :waitBackspaceN.addOne %~1
goto :eof
::[DOS_API:waitConfirm]wait user to input , and give one confirm.
::usage : call tools_userInput.bat waitConfirm outVar bCanbeEmpty
::outVar : output var
::bCanbeEmpty : check input can be empty, non 1 mean cann't be empty -- return without any input (except \r\n)
::example : call tools_userInput.bat waitConfirm waitNumber myDir1 //myDir1 can not be empty
:: call tools_userInput.bat waitConfirm waitString myDir2 1 //myDir2 can be empty
:waitConfirm
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :%*
call :waitConfirm.confirm %*
goto :eof
::[DOS_API:waitSelect]get user input char in choice command,used to limit user input range to avoid invald data
::usage : call tools_userInput.bat waitSelect optSet selChar
::optSet : option set, user can select only one in this set
::selChar : output user select char
::example : call tools_userInput.bat waitSelect abcdef selChar
:: user input d in console window
::result e.g : set selChar=d
:waitSelect
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 2 %*
set _tmpChoiceSet=%~1
choice /c %_tmpChoiceSet% /M "Please select one item "
set returnCode=%errorlevel%
if %returnCode% EQU 0 (
@echo user break this job.
goto :eof
)
if %returnCode% EQU 255 (
call tools_message.bat errorMsg "DOS inner error occurs."
echo.
goto :eof
)
set /a _tmpSelIdx=%returnCode%-1
call set %~2=%%_tmpChoiceSet:~%_tmpSelIdx%,1%%
goto :eof
::[DOS_API:waitSelectWithTimeout]get user input char in choice command,used to limit user input range to avoid invald data
::usage : call tools_userInput.bat waitSelectWithTimeout "choiceText" selChar "promptText" defaultSelect timeout
::choiceText : option set, devided by char ;
::selChar : output user select char, the select option text saved in variabe choiceArray[%selChar%]
::example : call tools_userInput.bat waitSelectWithTimeout "choice1;choice2;choiceX;choiceY;choiceZ" selChar "please make your selected:" 0 6
:: call tools_userInput.bat waitSelectWithTimeout "choice1;choice2;choiceX;choiceY;choiceZ" selChar
:: user input 2 in console window
::result e.g : set selChar=2
:waitSelectWithTimeout
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :waitSelectWithTimeout.reset
set "_tmpChoiceListText=%~1"
call :waitSelectWithTimeout.addItem "%_tmpChoiceListText:;=" & call :waitSelectWithTimeout.addItem "%"
for /L %%i in (0 1 %_tmpChoiceN%) do call :waitSelectWithTimeout.showItem %%i
if not {"%~3"}=={""} set "_tmpChoiceOpt=/M "%~3""
if not {"%~4"}=={""} set "_tmpChoiceOpt=/D %~4 /T 30 /M "%~3,%~4 will be selected automatically after 30 seconds:""
if not {"%~5"}=={""} set "_tmpChoiceOpt=/D %~4 /T %~5 /M "%~3,%~4 will be selected automatically after %~5 seconds:""
choice /c %_tmpChoiceListNum% %_tmpChoiceOpt%
set /a _tmpChoiceSel=%errorlevel%-1
set %~2=%_tmpChoiceSel%
call tools_message.bat enableDebugMsg "%~0" "your choice : %%_tmpChoiceArray[%_tmpChoiceSel%]%%"
goto :eof
::[DOS_API:readClipboard] read string from system clip board.
::usage : call readClipboard outVar
::example : call readClipboard outVar
:readClipboard
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
set "_tmpReadClipboard=%~1"
for /f "eol=; tokens=*" %%i in ('powershell Get-Clipboard') do call :readClipboard.trim %%i
goto :eof
::[DOS_API:processClipboard] read clipboard data and verify / show some info
::usage : call :processClipboard <outVar>
::e.g. : call :processClipboard tmpParam
:: call set tmpParam="C:\myCaache\srdcws1087" "findFile"
:processClipboard
:: call :task.processClipboard <subCmdName> [optParameter]
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
@where tools_userInput.bat 1>nul 2>nul || @set "path=%myWinScriptPath%\common;%path%"
call :readClipboard _tmpClipboardData
if not defined _tmpClipboardData call :processClipboard.noData
set _tmpClipboardDataShow=%_tmpClipboardData:"='%
if defined _tmpClipboardData call colorTxt.bat "clipboard data : {0d} %_tmpClipboardDataShow% {\n}{#}"
set %~1=%_tmpClipboardData%
goto :eof
::[DOS_API:writeClipboard] write string to system clip board
::usage : call writeClipboard inVar
::example : call writeClipboard inVar
:writeClipboard
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
echo %~1 | clip
rem if exist special chars
rem powershell Write-Output 'a^|b^|c' | clip
goto :eof
::[DOS_API:setFile2Var]check file should exist, if not, print error info and pause
::call e.g : call :setFile2Var 1 myFile.txt VarName //set VarName=C:\temp\myFile.txt permanent
:: call :setFile2Var 0 myFile.txt VarName //set VarName=C:\temp\myFile.txt temporary (available in current session)
:setFile2Var
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 3 %*
if defined setFile2VarPrompt set waitPathFileSpecPrompt=%setFile2VarPrompt%
if defined setFile2VarPrompt2 set waitPathFileSpecPrompt2=%setFile2VarPrompt2%
call :waitPathFileSpec "%~2" %~3
if {%~1}=={1} call setx %~3 %%%~3%%
goto :eof
::[DOS_API:setFileDirectory2Var]check file should exist, if not, print error info and pause
::call e.g : call :setFileDirectory2Var 1 myFile.txt VarName //add file path to %path% permanent
:: call :setFileDirectory2Var 0 myFile.txt VarName //add file path to %path% temporary (available in current session)
:setFileDirectory2Var
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 3 %*
if defined %~3 call if exist "%%%~3%%\%~2" exit /b 1
if defined setFileDirectory2VarPrompt set waitPathFileSpecPrompt=%setFileDirectory2VarPrompt%
if defined setFileDirectory2VarPrompt2 set waitPathFileSpecPrompt2=%setFileDirectory2VarPrompt2%
call :waitPathFileSpec "%~2" filePath
call :setFileDirectory2Var.execute %~1 %~3 %filePath%
goto :eof
::******************************implement section**************************************************************************
:readClipboard.trim
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if {"%~2"}=={""} set "%_tmpReadClipboard%=%~1"
if not {"%~2"}=={""} set %_tmpReadClipboard%=%*
goto :eof
:waitString.loop
if defined waitStringPrompt echo %waitStringPrompt%
if defined waitStringPrompt2 echo %waitStringPrompt2%
set _waitUserInput=
set /p _waitUserInput=
if not {"%~1"}=={"1"} (
if not defined _waitUserInput (
if not defined emptyInputPrompt set "emptyInputPrompt=input can't be empty, please input again."
echo %emptyInputPrompt%
goto :waitString.loop %~1
)
)
goto :eof
:processClipboard.noData
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined _noData_OK set "noDataMsg=no data is in clipboard , and using default setting."
if not defined _noData_OK set "noDataMsg=no data is in clipboard , error and press any key to exit."
call colorTxt.bat "{02}%noDataMsg%{\n}{#}"
if not defined _noData_OK pause > nul
if not defined _noData_OK exit /b 1
goto :eof
:waitConfirm.confirm
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call set _tmpInput=%%%~2%%
call colorTxt.bat purple_L "your input is '%_tmpInput%'"
echo.
if not defined waitStringPromptBakup set "waitStringPromptBakup=%waitStringPrompt%"
set waitStringPrompt=press any key to confirm current input. or input N/n to re-input.
call :waitString confirmQuery 1
set "waitStringPrompt=%waitStringPromptBakup%"
if {"%confirmQuery%"}=={"N"} goto :waitConfirm %*
if {"%confirmQuery%"}=={"n"} goto :waitConfirm %*
set waitStringPromptBakup=
goto :eof
:waitPathFileSpec.execute
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call set %~2=
call :inputFile "%~2"
call :waitPathFileSpec.check "%~1" "%%%~2%%"
if not {"%errorlevel%"}=={"1"} call :waitPathFileSpec %*
goto :eof
:waitPathFileSpec.check
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if not {"%~nx1"}=={"%~nx2"} (
call tools_message.bat warningMsg "wrong file path,the file extension name should be %~1"
exit /b 0
)
exit /b 1
goto :eof
:setFileDirectory2Var.execute
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :setPath2var %~1 %~3 "%~dp3"
goto :eof
:setFileDirectory2Var
call tools_userInput.bat waitPathFileSpec "jabber.natvis" filePath
set "path=%filePath%;%path%"
goto :eof
:waitSelectWithTimeout.addItem
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if not defined _tmpChoiceN set _tmpChoiceN=-1
set /a _tmpChoiceN+=1
set "_tmpChoiceArray[%_tmpChoiceN%]=%~1"
set _tmpChoiceListNum=%_tmpChoiceListNum%%_tmpChoiceN%
goto :eof
:waitSelectWithTimeout.reset
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined _tmpChoiceN for /L %%i in (0 1 %_tmpChoiceN%) do call set _tmpChoiceArray[%%i]=
set _tmpChoiceListNum=
set _tmpChoiceOpt=
set _tmpChoiceN=
goto :eof
:waitSelectWithTimeout.showItem
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call echo %~1. %%_tmpChoiceArray[%~1]%%
goto :eof
:waitBackspaceN.addOne
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call set "%~1=%%%~1%%%_tmpBkchar%"
goto :eof
::******************************help and test section**********************************************
::[DOS_API:Help]display help information
:Help
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_miscellaneous.bat DisplayHelp "%~f0"
goto :eof
::[DOS_API:Test] Test DOS API in this script file
:Test
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
@echo [%~nx0] Run test case [%0 %*]
echo.
echo test call :Help
call :Help
echo.
echo.
echo [skip] test call %~nx0 waitString myDir1
echo call %~nx0 waitString myDir1
echo [skip] test call %~nx0 waitString myDir2 1
echo call %~nx0 waitString myDir2 1
echo.
echo [skip] test call %~nx0 waitPathFile myFilePath
echo call %~nx0 waitPathFile myFilePath
echo.
echo [skip] test call %~nx0 waitSelect abcdef selChar
echo call %~nx0 waitSelect abcdef selChar
goto :eof
::******************************exit section**********************************************
:End
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [----- %~nx0] commandLine: %0 %* & @echo.