-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtools_miscellaneous.bat
More file actions
343 lines (307 loc) · 14.3 KB
/
tools_miscellaneous.bat
File metadata and controls
343 lines (307 loc) · 14.3 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
::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 :%*
goto End
::[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 End
::[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.
::prepare temporary file
set testTmpFile=%temp%\%random%.txt
if exist %testTmpFile% del /q/f %testTmpFile%
dir/b "%~dp0*.*" >> %testTmpFile%
echo [test sample] temporary file "%testTmpFile%" context:
type "%testTmpFile%"
call tools_message.bat noSleepMsg "%~0" 5
echo.
echo.
set _t1=%time%
timeout 3
set _t2=%time%
echo test call :calculateElapsedTime "%_t1%" "%_t2%" dur
call :calculateElapsedTime "%_t1%" "%_t2%" dur
echo %_t2% - %_t1% = %dur%
echo.
echo test call :getCmdOutput "dir/s/b tools_miscellaneous.bat" outVar
call :getCmdOutput "dir/s/b tools_miscellaneous.bat" outVar
echo outVar=%outVar%
echo.
echo test call :ShowScrollBar
call :ShowScrollBar
echo.
echo test call :SetErrorColor 0d
call :SetErrorColor 0d
echo.
echo test call :ResetErrorColor
call tools_message.bat noSleepMsg "%~0" 3
call :ResetErrorColor
echo.
echo test call :FindFirstUSBDisk %1
call :FindFirstUSBDisk %1
echo.
echo test call :PaselScatteredPoint ":Private_OnlyShowParam" "1 2 6 9 0"
call :PaselScatteredPoint ":Private_OnlyShowParam" "1 2 6 9 0"
echo.
echo test call :PaselScatteredPoint "tools_path.bat FindFilePathNoExt" "cmd where tree find"
call :PaselScatteredPoint "tools_path.bat FindFilePathNoExt" "cmd where tree find"
echo.
echo.
echo test call :PaselScatteredPoint "tools_path.bat FindFilePathShort" "devenv.exe windbg.exe"
call :PaselScatteredPoint "tools_path.bat FindFilePathShort" "devenv.exe windbg.exe"
echo.
goto :eof
::[DOS_API:ShowScrollBar]show dynamic scroll bar
::No any parameter
:ShowScrollBar
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
echo ��������������������������������������������������������������������
for /L %%i in (1 1 34) do set /p a=��<nul&ping -n 1 127.0.0.1 >nul
echo 100%
echo ��������������������������������������������������������������������
goto :eof
::[DOS_API:FindFirstUSBDisk]find first removable USB disk in current system.
::usage : call :FindFirstUSBDisk outputVar
::call e.g. : call :FindFirstUSBDisk driverName
:FindFirstUSBDisk
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :Clear_Environment_Variable tmpFindFirstUSBDisk
for /f "usebackq skip=1 tokens=1" %%i in ( `wmic LOGICALDISK where ( DriveType^=2 ^) get DeviceID` ) do (
if not defined tmpFindFirstUSBDisk set tmpFindFirstUSBDisk=%%i
)
if {%tmpFindFirstUSBDisk%}=={} echo No USB disk Found & goto :eof
if {%~1}=={} (
echo %tmpFindFirstUSBDisk%
) else (
set %~1=%tmpFindFirstUSBDisk%
)
goto :eof
::[DOS_API:calculateElapsedTime]calculate time duration
::usage : call :calculateElapsedTime startTime endTime outputDuration
::call e.g. : call :calculateElapsedTime "14:29:34.40" "14:29:34.75" dur
:calculateElapsedTime
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
:: https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file
rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%~1
set ENDTIME=%~2
call tools_message.bat enableDebugMsg "%~0" "STARTTIME: %STARTTIME%"
call tools_message.bat enableDebugMsg "%~0" "ENDTIME: %ENDTIME%"
rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
rem calculating the duratyion is easy
set /A DURATION=%ENDTIME%-%STARTTIME%
rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 360000
set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)
rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%
call tools_message.bat enableDebugMsg "%~0" "STARTTIME: %STARTTIME% centiseconds"
call tools_message.bat enableDebugMsg "%~0" "ENDTIME: %ENDTIME% centiseconds"
call tools_message.bat enableDebugMsg "%~0" "DURATION: %DURATION% in centiseconds"
set dur=%DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%
goto :eof
::[DOS_API:getCmdOutput]get output of DOS cmd and store into varName
::usage : call :getCmdOutput cmd varName
::call e.g. : call :getCmdOutput "dir/s/b tools_miscellaneous.bat" outVar
:getCmdOutput
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkParamCount 2 %*
for /F "usebackq tokens=*" %%i in ( ` %~1 ` ) do call set %~2=%%i
goto :eof
::[DOS_API:makePortalbe] collect necessary files for special feature.
::usage : call :makePortalbe cmd_or_bat_file
::call e.g. : call :makePortalbe "D:\work\shenxiaolong\core\shell_script\batch_env\common\tools_miscellaneous.bat"
:makePortalbe
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call tools_error.bat checkFileExist "%~fs1"
set "relDir=%~dp1"
set "scanFileDir=%~2"
if not defined scanFileDir set "scanFileDir=%~dp1"
call tools_path.bat noBashPath scanFileDir
if not defined defOutputDir set "defOutputDir=%~dpn1_portable"
if exist "%defOutputDir%" rd/s/q "%defOutputDir%\"
if not exist "%defOutputDir%" md "%defOutputDir%\"
call tools_string.bat toLower SystemRoot
call :makePortalbe.oneExistFile "%~fs1"
goto :eof
:makePortalbe.isCommentLine
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
set bCommentLine=1
set _tmpCmds=%*
if {"%_tmpCmds:~0,2%"}=={"::"} goto :eof
set _tmp_filter=%_tmpCmds:~0,3%
call tools_string.bat toLower _tmp_filter
if {"%_tmp_filter%"}=={"rem"} goto :eof
set bCommentLine=
goto :eof
:makePortalbe.findFilePath
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
set %~2=
for /f "usebackq tokens=*" %%i in ( ` dir/s/b %scanFileDir%\%~nx1 2^>nul ` ) do if not defined %~2 set "%~2=%%~i"
goto :eof
:makePortalbe.oneExistFile
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined _File_%~n1 goto :eof
set _tmpTargetType=%~x1
call tools_string.bat toLower _tmpTargetType
if {"%_tmpTargetType%"}=={".cmd"} call :makePortalbe.oneExistFile.script "%~f1"
if {"%_tmpTargetType%"}=={".bat"} call :makePortalbe.oneExistFile.script "%~f1"
if {"%_tmpTargetType%"}=={".exe"} call :makePortalbe.oneExistFile.bin "%~f1"
goto :eof
:makePortalbe.oneExistFile.script
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined _File_%~n1 goto :eof
set "_File_%~n1=%~f1"
call :makePortalbe.copyFile "%~1"
for /f "usebackq tokens=*" %%i in ( ` type "%~fs1" ^| find /i ".exe " ` ) do call :makePortalbe.oneLine.bin %%i
for /f "usebackq tokens=*" %%i in ( ` type "%~fs1" ^| find /i "call " ` ) do call :makePortalbe.oneLine.Call %%i
for /f "usebackq tokens=*" %%i in ( ` type "%~fs1" ^| find /i "start " ` ) do call :makePortalbe.oneLine.start %%i
goto :eof
:makePortalbe.oneExistFile.bin
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined _File_%~n1 goto :eof
set "_File_%~n1=%~f1"
set "_tmp_curFilePath=%~f1"
call tools_string.bat toLower _tmp_curFilePath
call tools_string.bat find "%_tmp_curFilePath%" "%SystemRoot%" bSysFile
if defined bSysFile goto :eof
call :makePortalbe.findFilePath "%~1" binTargetFilePath
if defined binTargetFilePath call :makePortalbe.copyFile "%binTargetFilePath%"
goto :eof
:makePortalbe.oneLine.bin
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :makePortalbe.isCommentLine %*
if defined bCommentLine goto :eof
set _tmpTargetType=%~x1
call tools_string.bat toLower _tmpTargetType
if {"%_tmpTargetType%"}=={".exe"} call :makePortalbe.findFilePath "%~nx1" binTargetFilePath
if defined binTargetFilePath call :makePortalbe.oneExistFile.bin "%binTargetFilePath%"
goto :eof
:makePortalbe.oneLine.Call
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :makePortalbe.isCommentLine %*
if defined bCommentLine goto :eof
set _tmpCmdLine=%*
call :makePortalbe.calParameter %_tmpCmdLine:*call =%
goto :eof
:makePortalbe.calParameter
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
set "_tmpTarget=%~1"
if {"%_tmpTarget:~0,1%"}=={":"} goto :eof
call :makePortalbe.findFilePath "%~1" calTargetFilePath
if defined calTargetFilePath call :makePortalbe.oneExistFile "%calTargetFilePath%"
goto :eof
:makePortalbe.oneLine.start
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
call :makePortalbe.isCommentLine %*
if defined bCommentLine goto :eof
set "_tmp_curFilePath=%~f1"
if not defined _tmp_curFilePath set "_tmp_curFilePath=%~f2"
call :makePortalbe.oneExistFile "%_tmp_curFilePath%"
goto :eof
:makePortalbe.copyFile
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if not exist "%~f1" goto :eof
set _tmpCurFile=%~f1
call set relPath=%%_tmpCurFile:%relDir%=%%
call :makePortalbe.copyFile.impl "%~f1" "%defOutputDir%\%relPath%"
goto :eof
:makePortalbe.copyFile.impl
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if not exist "%~dp2" md "%~dp2"
echo copy "%~nx1" ...
copy "%~f1" "%~f2"
goto :eof
::[DOS_API:Clear_Environment_Variable]clear local environment variable, its target is avoid to use delayed extenstion : setLocal enabledelayedexpansion
::usage : call :Clear_Environment_Variable EnvVarName
::call e.g. : call :Clear_Environment_Variable abc
:Clear_Environment_Variable
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if defined %~1 if not {%~1}=={} set %~1=
goto :eof
::[DOS_API:SetErrorColor] on error occur,change console color.
::usage : call :SetErrorColor Color
::call e.g. : call :SetErrorColor 0c
::Note : 2nd parameter can be empty, interface can provide default one
:SetErrorColor
if not {"%~1"}=={""} (
color %~1
) else (
::default error is 0E
color 0E
)
goto :eof
::[DOS_API:ResetErrorColor] reset console error color to default color
::usage : call :ResetErrorColor
::call e.g. : call :ResetErrorColor
:ResetErrorColor
::restore default console color
color
goto :eof
::[DOS_API:PaselScatteredPoint]process every element in one scattered set by special command
::usage : call :PaselScatteredPoint "CommandString" scatteredpoint
::Note : CommandString inner can't include ""
::call e.g. : call :PaselScatteredPoint "call :Private_OnlyShowParam" "1 2 6 9 0"
:PaselScatteredPoint
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if {"%~1"}=={} goto :eof
if {"%~2"}=={} goto :eof
for /f "usebackq tokens=1*" %%a in ( '%~2' ) do (
call %~1 %%a
call :PaselScatteredPoint "%~1" "%%b"
)
goto :eof
::[DOS_API:DisplayHelp]display all DOS APIs in one dos script module
:DisplayHelp
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if not exist "%~s1" call tools_message.bat popMsg "file[%~1] doesn't exist,pls check". & goto :eof
echo Script [%~nx1] provide below DOS_APIs:
echo.
for /f "usebackq tokens=*" %%i in ( ` findstr /n /r ^::\[DOS_API:.*\] %~s1 ` ) do (
call :Private_Display_DOSAPI_item "%%i" "%~dpnx1"
)
goto :eof
:Private_Display_DOSAPI_item
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
if {"%~1"}=={""} goto :eof
set p1=%~1
set p1=%p1:::[DOS_API:=%
set p1=%p1::=]%
for /f "delims=] tokens=1,2* " %%i in ( "%p1%" ) do (
if {%%j}=={} goto :eof
echo %%j
echo descript : %%k
echo [file:line]%~dpnx2 : %%i
echo Example "%~dpnx2 %%j"
echo.
)
goto :eof
:Private_OnlyShowParam
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [ %~nx0] commandLine: %0 %*
echo Parameter is "%1"
goto :eof
:End
@if defined _Stack @for %%a in ( 1 "%~nx0" "%0" ) do @if {"%%~a"}=={"%_Stack%"} @echo [----- %~nx0] commandLine: %0 %* & @echo.