Repository navigation
Expand file tree
/
Copy pathx3rd.cmd
More file actions
2160 lines (1960 loc) · 75.7 KB
/
Copy pathx3rd.cmd
File metadata and controls
2160 lines (1960 loc) · 75.7 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
0> /* :
@echo off
:: Copyright 2017 bin jin
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
:: http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
::
:: Framework:
::
:: A function whose name conforms to the specification gets:
:: External invocation. (Support short name completion)
:: Error handling.
:: Help information.
:: The functions list.
::
:: The annotation [:::] is the help text and must sit above the label it describes. For example:
:: ::: "[brief_introduction]" "" "Usage: %~n0 [function_name] [OPTION]..."
:: :[script_name_without_suffix]\[function_name]
:: [function_body]
:: :: Normal comment; [:::] is reserved for the annotation.
:: call :sub\[function_name]\%*
:: ...
:: :: Return false status.
:: exit /b 1
::
:: ::: " -o, --option=FILE [description]"
:: :sub\[function_name]\[--arg]
:: [function_body]
:: :: Exit and display [error_description_1].
:: exit /b 2 @REM [error_description_1]
::::::::::::::::::::::::::
:: Third-Party Programs ::
::::::::::::::::::::::::::
:: Reset errorlevel.
set errorlevel=
:: Add this script's directory to PATH when it is not on PATH yet.
for %%a in (%~nx0) do if "%%~$path:a"=="" set path=%path%;%~dp0
:: Re-entry for the pipe and the for /f form: a [\\] token is not a real path, so
:: this branch is the only one that runs it. The token names the label to call
:: and the arguments follow it, e.g. [\\txt\--fence SOURCE TAG]. The indirection
:: matters: a pipe runs this file in a child cmd, where calling a label is not
:: allowed, whereas the call above runs the file again as a whole. This is the
:: shape osdt.cmd uses with [call "%~f0" \\disk\detail_disk ^| diskpart].
if "%~d1"=="\\" call :%~pnx1 %2 %3 %4 %5 & goto :eof
if "%~2"=="-h" call :this\annotation :%~n0\%~1 & goto :eof
if "%~2"=="--help" call :this\annotation :%~n0\%~1 & goto :eof
2>nul call :%~n0\%*
if not errorlevel 0 exit /b 1
:: Show the function help when the function returns an error status.
if errorlevel 1 call :this\annotation :%~n0\%* & goto :eof
exit /b 0
:x3rd\
:x3rd\--help
:x3rd\-h
call :this\annotation
exit /b 0
::: "Print version and exit" "" "Usage: %~n0 version"
:x3rd\version
>&3 echo 0.26.9.26
exit /b 0
::: "Clink completion for xlib.cmd and x3rd.cmd, generated from the annotations" "Usage: %~n0 comp [OPTION]..."
:x3rd\comp
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\comp\%*
if not errorlevel 0 exit /b %errorlevel%
@REM Show the option help, or report the unknown option.
call :this\annotation :%~n0\%~1 %~2
exit /b 0
::: " -c, --clink print the clink completion script (Lua)"
:sub\comp\--clink
:sub\comp\-c
call :comp\run --clink
exit /b %errorlevel%
::: " -i, --install[=DIR] install the completion script to the clink directory"
:sub\comp\--install
:sub\comp\-i
:: [--install] alone installs to the clink directory. [--install=DIR] and
:: [--install DIR] name the directory; the value is split off and passed
:: as a separate argument, which survives the layers of expansion.
if "%~1"=="" call :comp\run --install & exit /b %errorlevel%
setlocal enabledelayedexpansion
set "_idir=%~1"
if "%~2" neq "" set "_idir=%~2"
if "!_idir:~0,10!"=="--install=" set "_idir=!_idir:~10!"
call :comp\run --install "!_idir!"
set "_code=!errorlevel!"
endlocal & exit /b %_code%
::: " -k, --check check the annotations and print warnings"
:sub\comp\--check
:sub\comp\-k
call :comp\run --check
exit /b %errorlevel%
::: " -l, --list print the parsed completion metadata"
:sub\comp\--list
:sub\comp\-l
call :comp\run --list
exit /b %errorlevel%
::: "Git tools" "" "Usage: %~n0 git [OPTION]... [ARG]..." ""
:x3rd\git
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\git\%* 2>nul
goto :eof
::: " -b, --backup backup git repositories"
:sub\git\--backup
:sub\git\-b
call :sub\path\--contain git.exe || exit /b 2 @REM The git command was not found.
setlocal enabledelayedexpansion
for /r /d %%a in (
hook?
) do if exist "%%~dpaobjects" (
:: Git repository.
set "_src=%%~dpa"
set "_src=!_src:~0,-1!"
:: Project name.
set _name=!_src:\.git=!
set _name=!_name:.git=!
for %%b in ("!_name!") do set "_name=%%~nxb"
:: Get the latest commit timestamp for the bundle name.
for /f "usebackq delims=" %%b in (
`git.exe --git-dir^="!_src!" log -1 --all --pretty^=format:%%cd --date^=format:%%y%%m%%d%%H%%M`
) do set _tamp=%%b
if defined _tamp (
set "_out=.\!_name!_!_tamp!.git"
if not exist "!_out!" (
echo create bundle: !_name!
@REM git.exe --git-dir="!_src!" bundle create "!_out!" HEAD master
git.exe --git-dir="!_src!" bundle create "!_out!" --all && git.exe bundle verify "!_out!"
git.exe --git-dir="!_src!" gc
echo.
) else echo exist: !_out!
) else echo skip: !_src!
)
endlocal
exit /b 0
::: " -ua, --update-all update all repositories"
:sub\git\--update-all
:sub\git\-ua
for /r /d %%a in (.g?t) do echo [INFO] %%~dpa&& git.exe --git-dir="%%a" --work-tree="%%~dpa" pull
goto :eof
::: " -id, --install-doc install the 'doc' alias to ~/.gitconfig"
:sub\git\--install-doc
:sub\git\-id
call :sub\path\--contain git.exe || exit /b 2 @REM The git command was not found.
:: The 'print' argument is for :sub\git\doc\--alias, which prints the 'doc' alias of this file.
if /i "%~1"=="print" call :sub\git\doc\--alias & exit /b 0
setlocal
set "_cfg=%userprofile%\.gitconfig"
if not exist "%_cfg%" type nul > "%_cfg%"
:: x3rd writes the alias with backslash continuations, but this script writes
:: CRLF, so it installs the same value on a single line instead. Both hold
:: one value: compare the value git computes, not the text of the config.
set "_new="
for /f "usebackq delims=" %%a in (`
call "%~f0" git -id print 2^>nul ^| git.exe config --file - alias.doc ^| git.exe hash-object --stdin
`) do set "_new=%%a"
set "_old="
for /f "usebackq delims=" %%a in (`
git.exe config --global alias.doc 2^>nul ^| git.exe hash-object --stdin
`) do set "_old=%%a"
if not defined _new exit /b 3 @REM Failed to read the 'doc' alias entry.
if "%_new%"=="%_old%" (
echo exist: alias.doc
endlocal
exit /b 0
)
git.exe config --global --unset-all alias.doc >nul 2>nul
call :sub\git\doc\--add "%_cfg%" || exit /b 4 @REM Failed to install the 'doc' alias.
echo install: alias.doc -^> "%_cfg%"
endlocal
exit /b 0
:: Append the 'doc' alias to the end of the config file, under an [alias] header.
:sub\git\doc\--add
if "%~1"=="" exit /b 2
setlocal disabledelayedexpansion
set "_end="
for /f "usebackq eol= delims=" %%a in (
`findstr.exe /b /c:"[" "%~1"`
) do set "_end=%%a"
set "_end=!_end: =!"
set "_end=!_end:"=!"
:: The blank line also ends a config file whose last line has no newline.
>> "%~1" echo.
if "!_end!"=="[alias]" (
>> "%~1" call :sub\git\doc\--print
) else (
>> "%~1" echo [alias]
>> "%~1" call :sub\git\doc\--print
)
>> "%~1" echo.
endlocal
exit /b 0
:: Write the 'doc' alias to stdout without a trailing newline: the lines of the
:: [sh] block of this file are joined with the 7 spaces of the x3rd
:: continuations, and the indent of each line is dropped by 'set /p', so the
:: value stays on one line and matches the one x3rd installs.
:sub\git\doc\--print
setlocal disabledelayedexpansion
for /f "usebackq delims=" %%a in (`
call "%~f0" \\txt\--fence "%~f0" sh
`) do <nul set /p "=%%a "
endlocal
exit /b 0
:: Print the alias as a config fragment for 'call "%~f0" git -id print': a
:: pipe runs the file in a child cmd, where calling a label is not allowed,
:: whereas the call above runs the file again as a whole; see 'hosts' in xlib.cmd.
:sub\git\doc\--alias
echo [alias]
call :sub\git\doc\--print
exit /b 0
::: "Maven repository tools" "" "Usage: %~n0 m2 [OPTION]..." ""
:x3rd\m2
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\m2\%* 2>nul
goto :eof
::: " -c, --trim[=PATH] print broken files in the local maven repository"
:sub\m2\--trim
:sub\m2\-c
setlocal
set _lu=0
set _m2_repo=%~1
if not defined _m2_repo set _m2_repo=%userprofile%\.m2
for /r "%_m2_repo%" %%a in (
*.md5 *.sha1 *.lastUpdated
) do call :m2\show-trim-info "%%~a"
echo @REM lastUpdated: %_lu%
endlocal
goto :eof
:m2\show-trim-info
if /i "%~x1"==".lastUpdated" erase %1 && set /a _lu+=1 && goto :eof
set _hash=
set _t=
set /p _hash=<%1
if "%_hash%"=="" (set /p _hash=&set /p _hash=)<%1
if "%~x1"==".sha1" set _hash=%_hash:~0,40%&set _t=SHA1
if "%~x1"==".md5" set _hash=%_hash:~0,32%&set _t=MD5
call :this\hash %_t% "%~dpn1" _h
if /i "%_h%" neq "%_hash%" echo echo %_t% %_hash%, %_h% ^& erase %~dpn1* && goto :eof
goto :eof
:this\hash [type] [file_path] [var]
if "%~3"=="" exit /b 2
setlocal
set _0=
for /f "usebackq delims=" %%a in (
`certutil.exe -hashfile %2 %~1`
) do for /f "usebackq tokens=1,2 delims=:" %%b in (
'%%a'
) do if "%%a"=="%%b" call set "_0=%%a"
endlocal & set %~3=%_0: =%
exit /b 0
::: "Oracle service start/stop"
:x3rd\orcld
if "%~1"=="" exit /b 2 @REM SID is empty, default is orcl
setlocal
set _sid=OracleService%~1
for /f "usebackq tokens=1,3" %%a in (
`sc.exe query %_sid%`
) do if /i "%%a"=="STATE" (
set _srv=OracleOraDb11g_home1TNSListener
if "%%b"=="4" (
call :orcld\setService
call :orcld STOP START
) else call :orcld START STOP
)
if not defined _srv >&2 echo Error: No %_sid% Service
echo.
endlocal
exit /b 0
:: For :x3rd\orcld.
:orcld
echo Oracle Service allready %~2, Press {Enter} to %~1
set /p _input=or type any characters to EXIT.
if defined _input exit /b 0
echo.
net.exe %1 %_srv%
net.exe %1 %_sid%
exit /b 0
:: For :x3rd\orcld.
:orcld\setService
for /f "usebackq tokens=1,3" %%a in (
`sc.exe qc %_srv%`
) do if /i "%%a%%b" == "START_TYPE2" >nul (
for %%c in (
%_srv% %_sid%
) do sc.exe config %%c start= demand
for %%c in (
OracleVssWriterORCL
OracleDBConsoleorcl
OracleJobSchedulerORCL
OracleMTSRecoveryService
OracleOraDb11g_home1ClrAgent
) do sc.exe config %%c start= disabled
)
exit /b 0
::: "VirtualBox Manage" "" "Usage: %~n0 vbox {start,stop,stopAll,ova} [VM_NAME]" ""
:x3rd\vbox
setlocal enabledelayedexpansion
:: Add the VirtualBox install path to PATH.
set path=%path%;%VBOX_MSI_INSTALL_PATH%
:: Fail when VBoxManage.exe is not on PATH.
call :sub\path\--contain VBoxManage.exe || exit /b 2 @REM VBoxManage.exe was not found.
2>nul call :sub\vbox\%*
if %errorlevel%==1 if "%~2"=="" call :sub\vbox\start %*
endlocal & exit /b %errorlevel%
:: List all VM names and mark the running ones.
:: TODO: list the MAC address with VBoxManage.exe showvminfo %_vms% --machinereadable | find "macaddress".
:sub\vbox\
call :vbox\setVar vms _vms
call :vbox\setVar runningvms _run
for /f "usebackq delims==" %%a in (
`set _vms 2^>nul`
) do if defined _run\%%~nxa (
call :this\txt\--all-col-left %%~nxa
call :this\txt\--all-col-left (running^)
echo.
) else echo %%~nxa
@REM call :this\txt\--all-col-left 0 0
exit /b 0
:: Start a virtual machine.
::: " start start VM by name"
:sub\vbox\start
call :vbox\init %1 || exit /b 0
if defined _run\%vm% (
echo %vm% is running...
) else VBoxManage.exe startvm %vm% -type headless
exit /b 0
:: Stop a virtual machine.
::: " stop/save stop VM by name"
:sub\vbox\stop
@REM :sub\vbox\save
call :vbox\init %1 || exit /b 0
if defined _run\%vm% (
VBoxManage.exe controlvm %vm% poweroff
) else echo %vm% not running...
exit /b 0
:: Stop all running virtual machines.
::: " stopAll stop all running VMs"
:sub\vbox\stopAll
for /f "usebackq" %%a in (
`VBoxManage.exe list runningvms`
) do VBoxManage.exe controlvm %%~a poweroff
echo All vm stop
exit /b 0
::: "" " ova [VM_NAME] [EULA_FILE_PATH]" " package the VM as an ova file"
:sub\vbox\ova
if "%~1"=="" exit /b 51 @REM The VM name is empty.
if not exist "%~2" exit /b 52 @REM The EULA file was not found.
setlocal enabledelayedexpansion
call :vbox\init %1 && VBoxManage.exe export %vm% -o ".\%vm%.ova" --legacy09 --manifest --options nomacs --vsys 0 --eulafile %2
endlocal
exit /b 0
:: Resolve a VM name prefix and set the 'vm' variable.
:vbox\init
if "%1"=="" exit /b 1
:: Load the registered and running VM names.
call :vbox\setVar vms _vms
call :vbox\setVar runningvms _run
:: Enumerate the names that match the prefix and print the candidates.
set vm=
set i=0
for /f "usebackq delims==" %%a in (
`set _vms\%1 2^>nul`
) do (
set /a i+=1
if !i!==2 call :this\txt\--all-col-left !vm!
set vm=%%~nxa
if !i! gtr 1 call :this\txt\--all-col-left %%~nxa
)
:: Close the left-aligned column layout.
call :this\txt\--all-col-left 0 0
if %i%==1 (
exit /b 0
) else if %i% gtr 1 (
>&2 echo Error: name conflict
) else >&2 echo Error: no name found
exit /b 1
:: Register the virtual machine names reported by VBoxManage.
:vbox\setvar
for /f "usebackq tokens=1,2" %%a in (
`VBoxManage.exe list %1`
) do set %2\%%~a=%%b
exit /b 0
:::::::::::::
:: Convert ::
:::::::::::::
::: "Convert to media" "" "Usage: %~n0 c2 [OPTION]... [OPERAND]..." ""
:x3rd\c2
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\path\--contain ffmpeg.exe || exit /b 2 @REM The ffmpeg command was not found.
call :sub\c2\%*
goto :eof
::: " -2f, --2flac convert alac, ape, m4a, tta, tak, wav to flac format" " run it with the target directory as the current directory"
:sub\c2\--2flac
:sub\c2\-2f
pushd "%cd%"
for /r . %%a in (
*.alac *.ape *.m4a *.tta *.tak *.wav
) do cd /d "%%~dpa" && (
echo cd /d %%~dpa
if not exist .\"%%~na.flac" ffmpeg.exe -hide_banner -i ".\%%~nxa" -acodec flac ".\%%~na.flac" 2>&1
)
popd
exit /b 0
::: "" " -b2, --vob2=[DRIVE:] [OUTPUT_FILE]" " convert a DVD drive to a video file" " e.g. %~n0 c2 -b2 D: E:\out.mkv"
:sub\c2\--vob2
:sub\c2\-b2
if not exist "%~dp2" exit /b 22 @REM The output path was not found.
if "%~x2"=="" exit /b 23 @REM The output file has no suffix.
@REM if /i "%~d1"=="%~d2" exit /b 4
setlocal enabledelayedexpansion
set _src=
for /f "usebackq delims=" %%a in (
`dir /b %~d1\VTS_01_*.VOB`
) do if defined _src (
set _src=!_src!^|%~d1\%%a
) else set _src=%~d1\%%a
endlocal & ffmpeg.exe -hide_banner -i concat:"%_src%" "%~f2"
goto :eof
::: "" " -2g, --2gif=[VIDEO_FILE] [TIME_RANGE]" " convert a video to a gif" " e.g. %~n0 c2 -2g D:\src.mp4 796-797"
:sub\c2\--2gif
:sub\c2\-2g
::: "" " -ss, --screenshot=[VIDEO_FILE] [TIME_RANGE]" " take screenshots of a video over a time range"
:sub\c2\--screenshot
:sub\c2\-ss
if not exist "%~1" exit /b 32 @REM The input video path was not found.
if "%~2"=="" exit /b 33 @REM The time range was not set.
setlocal
set "_range=%~2"
if "%_range:-=%"=="%~2" exit /b 34 @REM The time range format is invalid.
set _out=%~n1_%_range::=%
for /f "usebackq tokens=3 delims=2" %%a in ('%0') do goto c2\gif
::: screenshot :::
mkdir "%_out%"
ffmpeg.exe -hide_banner -ss %_range:-= -to % -i %1 -y -qscale:v 3 "%_out%\screenshot_%%d.png"
endlocal
goto :eof
::: gif :::
:c2\gif
set /a _width=480, _fps=10
@REM ffmpeg.exe -hide_banner -ss %_range:-= -to % -i %1 -r %_fps% -vf "fps=%_fps%,scale=%_width%:-1" -y -f gif "%_out%.gif"
ffmpeg.exe -hide_banner -ss %_range:-= -to % -i %1 -r %_fps% -vf fps=%_fps%,scale=%_width%:-1:flags=lanczos,palettegen -y "%tmp%\%_out%.png"
ffmpeg.exe -hide_banner -ss %_range:-= -to % -i %1 -i "%tmp%\%_out%.png" -r %_fps% -lavfi fps=%_fps%,scale=%_width%:-1:flags=lanczos[x];[x][1:v]paletteuse -y -f gif "%_out%.gif"
erase "%tmp%\%_out%.png"
endlocal
goto :eof
::: "Play all media in a directory" "" "Usage: %~n0 play [OPTION]... [DIRECTORY]..." "" " -r, --random play in random order" " -a, --ast=NUMBER select the desired audio stream" " -j, --skip=NUMBER skip NUMBER files" "" " PLAY_VOLUME environment variable, e.g. 0.5" "" " PLAY_SCALE environment variable, e.g. -1:480"
:x3rd\play
call :sub\path\--contain ffplay.exe || exit /b 12 @REM ffplay command not found
if "%~1"=="" exit /b 13 @REM args is empty
setlocal enabledelayedexpansion
set _media=
set _random=
set _stream_specifier=
set _skip=
:play\args
if /i "%~1"=="--random" set _random=RANDOM
if /i "%~1"=="-r" set _random=RANDOM
:: -ast stream_specifier select the desired audio stream.
if /i "%~1"=="--ast" call :sub\is\--integer %~2 && set "_stream_specifier=-ast %~2"& shift /1
if /i "%~1"=="-a" call :sub\is\--integer %~2 && set "_stream_specifier=-ast %~2"& shift /1
if /i "%~1"=="--skip" call :sub\is\--integer %~2 && set "_skip=%~2"& shift /1
if /i "%~1"=="-j" call :sub\is\--integer %~2 && set "_skip=%~2"& shift /1
call :sub\dir\--isdir %1 && set _media=%_media% "%~1"
shift /1
if "%~1" neq "" goto play\args
set _c=1000000000
set /a _skip+=_c
for %%a in (
%_media%
) do (
pushd "%cd%"
cd /d "%%~a"
if defined _random (
for /r %%b in (
*.alac *.ape *.avi *.divx *.flac *.flv *.m4a *.mkv *.mp? *.ogg *.rm *.rmvb *.tta *.tak *.vob *.wav *.webm *.wm?
) do (
set /a _c+=1
set "_track!random!=%%b"
)
) else (
for /f "usebackq delims=" %%b in (
`dir /b /s /on *.alac *.ape *.avi *.divx *.flac *.flv *.m4a *.mkv *.mp? *.ogg *.rm *.rmvb *.tta *.tak *.vob *.wav *.webm *.wm?`
) do (
set /a _c+=1
if !_c! geq %_skip% set "_track!_c!=%%b"
)
)
popd
)
set /a _c=%_c% %% 1000000000, _i=%_skip% %% 1000000000
for /f "usebackq tokens=2 delims==" %%a in (
`set _track 2^>nul`
) do set /a _i+=1& call :this\ff "%%a"
endlocal
exit /b 0
:this\ff
echo Progress #%_i% / %_c%, %_random%
:: -sn ::disable subtitling.
:: -ch_layout stereo ::ED..A... set number of audio channels (from 0 to INT_MAX) (default 0) ::Convert the 5.1 track to stereo.
if not defined PLAY_VOLUME set PLAY_VOLUME=0.85
if not defined PLAY_SCALE set PLAY_SCALE=-1:-1
for %%a in (
avi divx flv mkv mp4 mpg rm rmvb vob webm wmv
) do if /i "%~x1"==".%%a" ffplay.exe -hide_banner %_stream_specifier% -ch_layout stereo -sn -autoexit -vf "scale=%PLAY_SCALE%" -af "volume=%PLAY_VOLUME%" %1 2>&1
for %%a in (
alac ape flac m4a mp3 ogg tta tak wav wma
) do if /i "%~x1"==".%%a" start /b /wait /min ffplay.exe -hide_banner -autoexit -af "volume=0.05" %1 2>&1
exit /b 0
@REM :x3rd\cam
@REM if "%~1"=="" call :this\annotation %0 & goto :eof
@REM call :sub\path\--contain ffmpeg.exe || exit /b 2 @REM The ffmpeg command was not found.
@REM call :sub\cam\%*
@REM goto :eof
@REM
@REM :sub\cam\--list
@REM :sub\cam\-l
@REM ffmpeg.exe -hide_banner -f dshow -list_devices true -i "" 2>&1 | find.exe "]"
@REM exit /b 0
@REM
@REM :sub\cam\--show
@REM :sub\cam\-s
@REM ffplay.exe -hide_banner -f dshow -video_size $size -framerate 25 -pixel_format 0rgb -probesize 10M -i "0":"0" 2>&1
@REM exit /b 0
::: "Docker batch command" "Usage: %~n0 moby {start,stop}"
:x3rd\moby
call :sub\path\--contain docker.exe || exit /b 2 @REM The docker client command was not found.
2>nul call :sub\moby\%*
goto :eof
:sub\moby\start
for /f "usebackq skip=1" %%a in (
`docker.exe ps -f status=exited`
) do docker.exe start %%a
exit /b 0
:sub\moby\stop
for /f "usebackq skip=1" %%a in (
`docker.exe ps`
) do docker.exe stop %%a
exit /b 0
@REM ::: "Get docker tags" "" " %~n0 tags [image_name]"
@REM :sub\moby\tags
@REM for %%a in (bash.exe) do if "%%~$path:a"=="" exit /b 2 @REM bash.exe was not found; install Git for Windows and add it to the PATH.
@REM for /f "usebackq tokens=2,4 " %%a in (
@REM `bash.exe -c 'curl https://index.docker.io/v1/repositories/%1/tags 2^>nul ^^^| sed -e "s/\}, /\n/g;s/,//g;s/\}\]//g"`
@REM ) do echo %%~a %1:%%~b
@REM exit /b 0
::: "Compress PNG images" "Usage: %~n0 cpng [SRC_DIR] [OUT_DIR]"
:x3rd\cpng
call :sub\path\--contain pngquant.exe || exit /b 2 @REM The pngquant command was not found.
if not exist "%~f1" exit /b 3 @REM The source path does not exist.
if "%~2"=="" exit /b 4 @REM The output path was not set.
if /i "%~f1"=="%~f2" exit /b 5 @REM The input and output directories are the same.
setlocal enabledelayedexpansion
set "_src=%~f1"
set "_out=%~f2"
if "%_src:~-1%"=="\" set "_src=%_src:~0,-1%"
if "%_out:~-1%"=="\" set "_out=%_out:~0,-1%"
for /r %1 %%a in (
*.png
) do set "_tag=%%~dpa"& call ^
set "_tag=%_out%\!_tag:%_src%\=!"& (
echo.
if not exist "!_tag!" mkdir "!_tag!"
) & 2>&1 pngquant.exe --quality 70-90 --speed 1 --strip --verbose --output "!_tag!%%~nxa" "%%~fa"
endlocal
exit /b 0
::: "Compress to tar.gz with 7za" "" "Usage: %~n0 tar.gz [PATH]"
:x3rd\tar.gz
::: "Compress to tar.bz2 with 7za" "" "Usage: %~n0 tar.bz2 [PATH]"
:x3rd\tar.bz2
call :sub\path\--contain 7za.exe || exit /b 10 @REM 7za.exe was not found.
setlocal enabledelayedexpansion
if not exist "%~1" exit /b 2 @REM The target was not found.
if "%~2"=="" call :this\equalsDeputySuffix %1 .tar && (
call :this\tarDecompresses %1
goto :eof
)
for %%a in (%0) do set _suffix=%%~nxa
set _suffix=%_suffix:~4%
call :this\tarCompresses %*
endlocal
exit /b 0
:: For the :x3rd\tar.gz and :x3rd\tar.bz2 commands.
:this\tarCompresses
if not defined _tarName set _tarName=%~n1
if "%~2"=="" call :this\dir\--isdir %1 || for %%a in (
7z cab rar zip
) do if /i "%~x1"==".%%~a" (
pushd %cd%
call :this\str\--now _nowTar && set _nowTar=%temp%\!_nowTar!
mkdir !_nowTar! && chdir /d !_nowTar! && 7za.exe x %1 -aoa
popd
call %0 !_nowTar!\*
rmdir /s /q !_nowTar! && set _nowTar=
goto :eof
)
7za.exe a dummy -ttar -so %* | 7za.exe a -si -t%_suffix:z=zip% "!_tarName!.tar.%_suffix%" -aoa
set _tarName=
goto :eof
:: For the :x3rd\tar.gz and :x3rd\tar.bz2 commands.
:this\tarDecompresses
for %%a in ("%~n1") do (
7za.exe x %1 -so | 7za.exe x -si -ttar -o. -aoa
@REM call :this\molting ".\%%~na" %2
)
goto :eof
@REM @REM for :this\tarDecompresses
@REM :this\molting
@REM if "%~1"=="" goto :eof
@REM @REM if "%~2"=="" goto :eof
@REM for /f "usebackq delims=" %%a in (
@REM `dir /a /b "%~1"`
@REM ) do if not defined _molting (
@REM call :this\dir\--isdir "%~1\%%a" || goto :eof
@REM set "_molting=%%a"
@REM ) else set _molting= & goto :eof
@REM call :this\str\--now _nowM && rename "%~1" !_nowM!
@REM >nul move /y "%~dp1!_nowM!\!_molting!" "%~dp1" && rmdir /s /q "%~dp1!_nowM!"
@REM set _nowM=
@REM @REM set "%~2=%~dp1!_molting!"
@REM set _molting=
@REM @REM call %0 "!%~2!" %~2
@REM goto :eof
:: For the :x3rd\tar.gz and :x3rd\tar.bz2 commands.
:this\equalsDeputySuffix
for %%a in (
"%~n1"
) do if /i "%%~xa"=="%~2" exit /b 0
exit /b 1
@REM :x3rd\ftp
@REM >%temp%\.bb315509-cf9c-5caa-c096-24d258c3d95d (
@REM call :this\ftp\init [ip] [name] [password] [dir]
@REM if "%~1"=="" (
@REM call :this\ftp\dir
@REM ) else call :this\ftp\upload %*
@REM )
@REM ftp.exe -s:%temp%\.bb315509-cf9c-5caa-c096-24d258c3d95d
@REM erase %temp%\.bb315509-cf9c-5caa-c096-24d258c3d95d
@REM exit /b 0
@REM
@REM :this\ftp\init
@REM echo open %~1
@REM echo %~2
@REM echo %3
@REM echo cd %4
@REM goto :eof
@REM
@REM :this\ftp\upload
@REM echo binary
@REM for %%a in (
@REM %*
@REM ) do if exist "%%~a" echo put "%%~a"
@REM echo close
@REM echo quit
@REM goto :eof
@REM
@REM :this\ftp\dir
@REM echo dir
@REM echo quit
@REM goto :eof
::::::::::::::::::
:: Base ::
:: :: :: :: ::
:: Test whether the target is on the PATH.
:sub\path\--contain
if "%~1" neq "" if "%~$path:1" neq "" exit /b 0
exit /b 1
:: Print the clink command to run: [clink.bat] when it is on the PATH, else the
:: clink.bat of the directory named by CLINK_DIR, else the clink named by the
:: session macro, else the usual install directories. Set the variable named by
:: %~1 and return 0, or return 1.
:: It writes in the caller's scope on purpose, so no setlocal is used.
:this\clink\--path [var]
call :this\clink\--search %~1
if defined %~1 exit /b 0
exit /b 1
:this\clink\--search [var]
set "%~1="
if exist "%CLINK_DIR%\clink.bat" set "%~1=%CLINK_DIR%\clink.bat"
for %%a in (clink.bat clink_x64.exe clink.cmd) do if not defined %~1 if not "%%~$path:a"=="" set "%~1=%%~$path:a"
:: An injected clink names itself with the [clink] doskey macro, and a portable
:: installation is on no PATH and in no usual directory, so read the launcher
:: from there. The macro is [clink="PATH" $*], and the first word that is a
:: file is the launcher, whether it is [clink.bat] or [clink_x64.exe].
for /f "usebackq tokens=1,* delims==" %%a in (`
2^>nul doskey.exe /macros
`) do if /i "%%a"=="clink" for %%f in (%%b) do if not defined %~1 if exist "%%~ff" set "%~1=%%~ff"
for %%a in (
"%LOCALAPPDATA%\clink\clink.bat"
"%ProgramFiles%\clink\clink.bat"
"%ProgramFiles(x86)%\clink\clink.bat"
) do if not defined %~1 if exist %%a set "%~1=%%~a"
exit /b 0
:: Print the fenced block tagged [%~2] of any text file [%~1]. The block is
:: markdown: its opening fence carries the tag, its closing fence is a bare
:: ``` line. The [lua] block of this file holds the completion generator
:: [comp]; the [sh] block holds the 'doc' alias that [git -id] installs.
::
:: A pipe cannot call a label, so reach it as a child with the token of the
:: re-entry branch, [call "%~f0" \\txt\--fence SOURCE TAG].
::
:: Delayed expansion is off while the block is copied, so an exclamation mark in
:: a block is kept; the 'doc' alias holds one.
:txt\--fence [source_path] [tag]
if "%~2"=="" exit /b 2 @REM The block tag is empty.
setlocal disabledelayedexpansion
set "_on="
for /f "usebackq delims=" %%a in ("%~1") do (
if defined _on (
if "%%~a"=="```" (set "_on=") else echo(%%a
) else if "%%~a"=="``` %~2" set _on=1
)
endlocal
exit /b 0
:: Extract the embedded generator and run it with the Lua engine that clink
:: embeds, so no Lua interpreter has to be installed and no scratch file is
:: written. The generator reads its inputs from the environment, because a
:: script on standard input has no arguments of its own. The [ERROR] message
:: goes to the [>&3] console, which the [2>nul] of the dispatch line keeps.
:comp\run
setlocal enabledelayedexpansion
call :this\clink\--path _clink
if not defined _clink (
>&3 echo [ERROR] clink was not found. Install clink, or set CLINK_DIR.
endlocal & exit /b 3
)
:: The mode is the bare option name, so [--check] becomes [check].
set "_mode=%~1"
set "XCMD_SOURCE=%~dp0xlib.cmd"
set "XCMD_SOURCE2=%~dp0x3rd.cmd"
if "!_mode:~0,2!"=="--" set "_mode=!_mode:~2!"
set "XCMD_MODE=!_mode!"
if /i "%~1"=="--install" set "XCMD_DIR=%~2"
call "%~f0" \\txt\--fence "%~f0" lua | "!_clink!" lua
set "_code=!errorlevel!"
endlocal & exit /b %_code%
:: Test whether a string is an integer. \* @see xlib.cmd *\
:sub\is\--integer
if "%~1"=="" exit /b 10
setlocal
set _tmp=
:: A valid integer round-trips through 'set /a' unchanged.
2>nul set /a _code=10, _tmp=%~1
if "%~1"=="%_tmp%" set _code=0
endlocal & exit /b %_code%
:: Test whether a path is a directory. \* @see dis.cmd *\
:sub\dir\--isdir
setlocal
set _path=%~a1-
:: The first attribute character is 'd' for a directory.
set _code=10
if %_path:~0,1%==d set _code=0
endlocal & exit /b %_code%
:: Display the time as [YYYYMMDDhhmmss]. \* @see xlib.cmd *\
:sub\str\--now
if "%~1"=="" exit /b 2
set date=
set time=
:: English and Chinese date formats.
for /f "tokens=1-8 delims=-/:." %%a in (
"%time: =%.%date: =.%"
) do if %%e gtr 1970 (
set %~1=%~2%%e%%f%%g%%a%%b%%c%~3
) else if %%g gtr 1970 set %~1=%~2%%g%%e%%f%%a%%b%%c%~3
exit /b 0
:: :: :: :: ::
:: Base ::
::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::
:: Framework ::
:: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Show the function list or one function's help, print an error message, or complete a function name.
:this\annotation
setlocal enabledelayedexpansion & set /a _err_code=%errorlevel%
set _annotation_more=
set _err_msg=
for /f "usebackq skip=74 delims=" %%a in (
"%~f0"
) do for /f "usebackq tokens=1,2* delims=\ " %%b in (
'%%a'
) do (
if /i "%%~b"==":::" (
set _annotation=%%a
set _annotation=!_annotation:* =!
) else if defined _func_eof (
if %_err_code% gtr 1 (
set _err_msg=%%~a
set _un_space=!_err_msg: =!
:: Match the error message.
if "!_un_space:exit/b%_err_code%=!" neq "!_un_space!" >&2 ^
echo [ERROR] !_err_msg:* @REM =! ^(%~f0!_func_eof!^)&& exit /b 1
) else if %_err_code%==1 >&2 echo [ERROR] invalid option '%~2' ^(%~f0!_func_eof!^)&& exit /b 1
)
:: Match a sub function label.
if /i "%%~b\%%~c"==":sub\%~nx1" (
set _func_eof=%%~a
if defined _annotation if %_err_code%==0 call %0\more !_annotation!
set _annotation=
) else if /i "%%~b"==":%~n0" (
:: Match a new function and clear the previous one.
if defined _annotation_more exit /b 0
if defined _err_msg >&2 echo unknown error.& exit /b 1
set _func_eof=
:: Match the target function.
if /i "%%~b\%%~c"=="%~1" (
set _func_eof=%%~a
if defined _annotation if %_err_code%==0 call %0\more !_annotation!
set _annotation=
)
:: Register the annotation for the list and name completion; skip the dispatch labels (-h, --help).
set _fn_name=%%~c
if not "!_fn_name!"=="" if not "!_fn_name:~0,1!"=="-" set _prefix_4_auto_complete\%%~c=!_annotation! ""
)
)
if defined _annotation_more exit /b 0
if defined _err_msg >&2 echo unknown error.& exit /b 1
:: Print the function list in columns.
call :xlib\cols _col
set /a _i=0, _col/=16
for /f usebackq^ tokens^=1^,2^ delims^=^=^" %%a in (`
2^>nul set _prefix_4_auto_complete\%~n1
`) do if "%~1" neq "" (
:: Print the matching function names in columns.
set /a _i+=1
if !_i!==1 (
set _cache_arg=%%~nxa
if "%~2" neq "" (
set _args=%*
set _args=%%~nxa !_args:* =!
) else set _args=%%~nxa
) else if !_i!==2 (
call :sub\txt\--all-col-left !_cache_arg! %_col%
call :sub\txt\--all-col-left %%~nxa %_col%
) else if !_i! geq 2 call :sub\txt\--all-col-left %%~nxa %_col%
) else call :sub\str\--2col-left %%~nxa "%%~b"
:: Close the column layout.
if !_i! gtr 0 call :sub\txt\--all-col-left 0 0
:: Report the error, or call the matched function.
endlocal & if %_i% gtr 1 (
echo.
>&2 echo [WARN] function sort name conflict
exit /b 1
) else if %_i%==0 (
if "%~1" neq "" >&2 echo [ERROR] No function found& exit /b 1
) else if %_i%==1 2>nul call :%~n0\%_args% || call %0 :%~n0\%_args%
goto :eof
:this\annotation\more
echo.%~1
shift /1
if "%~1" neq "" goto %0
if .%1==."" goto %0
set _annotation_more=true
exit /b 0
:: Make the second column left-aligned.
:sub\str\--2col-left
if "%~2"=="" exit /b 1
setlocal enabledelayedexpansion
set _str=%~10123456789abcdef
if "%_str:~31,1%" neq "" call :strModulo
set /a _len=0x%_str:~15,1%
set "_spaces= "
echo %~1!_spaces:~0,%_len%!%~2
endlocal
exit /b 0
:: Pad with spaces on the right to make all columns left-aligned.
:sub\txt\--all-col-left
if "%~1"=="" exit /b 1
if "%~2" neq "" if 1%~2 lss 12 (if defined _acl echo. & set _acl=) & exit /b 0
setlocal enabledelayedexpansion
set _str=%~10123456789abcdef
if "%_str:~31,1%" neq "" call :strModulo
if "%~2" neq "" if 1%_acl% geq 1%~2 echo. & set /a _acl-=%~2-1
set /a _len=0x%_str:~15,1%
set "_spaces= "
>&3 set /p=%~1!_spaces:~0,%_len%!<nul
set /a _acl+=1
if "%~2" neq "" if 1%_acl% geq 1%~2 echo. & set _acl=
endlocal & set _acl=%_acl%
exit /b 0
:: For :this\txt\--all-col-left.
:strModulo
set /a _acl+=1
set _str=%_str:~15%
if "%_str:~31,1%"=="" exit /b 0
goto %0
:: Get the console width in columns.
:xlib\cols
for /f "usebackq skip=4 tokens=2" %%a in (`mode.com con`) do (
if "%~1"=="" (
echo %%a
) else set %~1=%%a
exit /b 0