-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxlib.cmd
More file actions
5205 lines (4603 loc) · 191 KB
/
xlib.cmd
File metadata and controls
5205 lines (4603 loc) · 191 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:
::
:: If the function names conform to the specifications:
:: External call function. (Support short name completion)
:: Error handling.
:: Display help information.
:: Print the functions list.
::
:: e.g.
:: ::: "[brief_introduction]" "" "[description]" ...
:: :[script_name_without_suffix]\[function_name]
:: ...
:: @REM call sub function
:: call :sub\[function_name]\%*
:: ...
:: @REM return false status
:: exit /b -1
::
:: ::: "[description_sub_function]" ...
:: :sub\[function_name]\[--arg]
:: ...
:: @REM exit and display [error_description_1]
:: exit /b 2 @REM [error_description_1]
::
::
:: Thread:
:: e.g.
:: \\:[function_name] [args1] [args2] ...
:: ...
:: :[function_name]
:: ...
:: goto :eof
:::::::::::::::::::::
:: basic functions ::
:::::::::::::::::
@REM init errorlevel
set errorlevel=
@REM For thread
if "%~d1"=="\\" call :thread "%*" & exit
@REM Init PATH
for %%a in (%~nx0) do if "%%~$path:a"=="" set path=%path%;%~dp0
if "%~2"=="-h" call :this\annotation :%~n0\%~1 & exit /b 0
if "%~2"=="--help" call :this\annotation :%~n0\%~1 & exit /b 0
2>nul call :%~n0\%*
if not errorlevel 0 exit /b 1
@REM Test type function
if errorlevel 1 call :this\annotation :%~n0\%* & goto :eof
exit /b 0
:::::::::::::::
:: functions ::
:::::::::::
:xlib\
:xlib\--help
:xlib\-h
call :this\annotation
exit /b 0
::: "Output version and exit"
:xlib\version
>&3 echo 0.22.12.17
exit /b 0
::: "Variable tool, Use '-h' for a description of the options" "" "usage: %~n0 var [option] [...]" ""
:xlib\var
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\var\%*
goto :eof
@REM override:
@REM "": [variable_prefix_name*]
::: " --unset, -u [[var_name]] Unset variable, where variable name left contains"
:sub\var\--unset [variable_prefix_name]
:sub\var\-u
if "%~1"=="" exit /b 10 @REM The parameter is empty
for /f "usebackq delims==" %%a in (`
2^>nul set %1
`) do set %%a=
exit /b 0
::: " --env, -e [key] [value] Set environment variable"
:sub\var\--env
:sub\var\-e
if "%~1"=="" exit /b 22 @REM key is empty
if "%~2"=="" exit /b 23 @REM value is empty
@REM for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 24 @REM 'wmic' is not in path
for %%a in (setx.exe) do if "%%~$path:a" neq "" >nul setx.exe %~1 %2 /m & exit /b 0
@REM if defined %~1 wmic.exe environment where 'name="%~1" and username="<system>"' set VariableValue="%~2"
@REM if not defined %~1 wmic.exe environment create name="%~1",username="<system>",VariableValue="%~2"
exit /b 0
::: " --set-errorlevel, -sel [num] Set errorlevel variable"
:sub\var\--set-errorlevel
:sub\var\-sel
if "%~1"=="" goto :eof
exit /b %1
::: " --in-path, -p [file_name] Test target in $env:path"
:sub\var\--in-path
:sub\var\-p
if "%~1" neq "" if "%~$path:1" neq "" exit /b 0
exit /b -1
::: " --trim-path, -sp [var_name] Path Standardize"
:sub\var\--trim-path
:sub\var\-sp
if "%~1"=="" exit /b 56 @REM variable name is empty
if not defined %~1 exit /b 57 @REM variable name not defined
setlocal enabledelayedexpansion
@REM TODO get var value in path
@REM Trim quotes
set _var=!%~1:"=!
@REM " Trim head/tail semicolons
if "%_var:~0,1%"==";" set _var=%_var:~1%
if "%_var:~-1%"==";" set _var=%_var:~0,-1%
@REM Replace slash end of path
set _var=%_var:\;=;%;
@REM Delete path if not exist
call :this\path\trim "%_var:;=" "%"
endlocal & set %~1=%_var:~0,-1%
exit /b 0
@REM for :sub\var\--trim-path, delete path if not exist
:this\path\trim
if "%~1"=="" exit /b 0
if not exist %1 set _var=!_var:%~1;=!
shift /1
goto %0
::: " --reset-path, -rp Reset default path environment variable"
:sub\var\--reset-path
:sub\var\-rp
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 59 @REM 'wmic' is not in path
for /f "usebackq tokens=2 delims==" %%a in (`
wmic.exe ENVIRONMENT where "Caption='<SYSTEM>\\Path'" get VariableValue /format:list
`) do call set path=%%a
exit /b 0
::: " --install-config, -ic Load the file %USERPROFILE%\.batchrc before running cmd.exe"
:sub\var\--install-config
:sub\var\-ic
setlocal
call :sub\var\--backspace _bs
call :regedit\on
reg.exe ^
add "HKCU\SOFTWARE\Microsoft\Command Processor" ^
/v AutoRun ^
/t REG_SZ ^
/d "if not defined BS set BS=%_bs%&& (for /f \"usebackq delims=\" %%a in (\"%%USERPROFILE%%\.batchrc\") do @call %%a)>nul 2>&1" /f
call :regedit\off
if exist "%USERPROFILE%\.batchrc" endlocal & exit /b 0
> "%USERPROFILE%\.batchrc" (
echo ;use ^>^&3 in script can print
echo set devmgr_show_nonpresent_devices=1
echo doskey.exe which=where.exe $*
echo set path=%%path%%;%~dp0
echo,
echo title Command Prompt
)
endlocal
exit /b 0
::: " --uninstall-config, -uc Remove config loader"
:sub\var\--uninstall-config
:sub\var\-uc
setlocal
call :regedit\on
@REM erase "%USERPROFILE%\.batchrc"
reg.exe delete "HKCU\SOFTWARE\Microsoft\Command Processor" /v AutoRun /f
call :regedit\off
endlocal
exit /b 0
::: "sleep some milliseconds" "" "usage: %~n0 sleep [integer]"
:xlib\sleep
call :sub\is\--integer %~1 || exit /b 22 @REM arg not integer
@REM start /min /w mshta.exe vbscript:setTimeout("window.close()",1200)
start /w MsHta.exe JavaScript:document.write();setTimeout('close()',%~1);
exit /b 0
::: "Run by ..." "" "usage: %~n0 run [option]" ""
:xlib\run
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\run\%*
goto :eof
::: " --admin, -a [...] Run As Administrator"
:sub\run\--admin
:sub\run\-a
@REM net.exe user administrator /active:yes
@REM net.exe user administrator ???
runas.exe /savecred /user:administrator "%*"
exit /b 0
::: " --vbhide, -q [...] Run some command at background"
:sub\run\--vbhide
:sub\run\-q
if "%~1"=="" exit /b 22 @REM The first parameter is empty
@REM mshta.exe VBScript:CreateObject("WScript.Shell").Run("""%~1"" %~2", 0)(Window.close)
@REM see https://docs.microsoft.com/zh-tw/windows/desktop/shell/shell-shellexecute#code-snippet-1
@REM mshta.exe VBScript:CreateObject("Shell.Application").ShellExecute("%~1","%~2","","open",0)(window.close) 'No spaces are allowed in the preceding code 'e.g. "D:\demo.exe" "--config ""D:\demo 2.config"""
call :xlib\vbs vbhide "%~1"
exit /b 0
::: "Test string status" "" "usage: %~n0 is [option] [[string]]" ""
:xlib\is
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\is\%*
goto :eof
::: " --integer, -i [string] Test string if integer"
:sub\is\--integer
:sub\is\-i
if "%~1"=="" exit /b -1
setlocal
set _tmp=
@REM quick return
2>nul set /a _code=-1, _tmp=%~1
if "%~1"=="%_tmp%" set _code=0
endlocal & exit /b %_code%
::: " --pipe Test script run after pipe"
:sub\is\--pipe
::: " --gui Test script start at GUI"
:sub\is\--gui
setlocal
set cmdcmdline=
set _cmdcmdline=%cmdcmdline:"='%
rem "
set _code=0
if /i "%0"==":sub\is\--pipe" if "%_cmdcmdline%"=="%_cmdcmdline: /S /D /c' =%" set _code=-1
if /i "%0"==":sub\is\--gui" if "%_cmdcmdline%"=="%_cmdcmdline: /c ''=%" set _code=-1
@REM if /i "%0"==":sub\is\--gui" for %%a in (%cmdcmdline%) do if /i %%~a==/c set _code=0
endlocal & exit /b %_code%
@REM Test mac address
:sub\is\--MACaddr
:sub\is\-mac
setlocal enabledelayedexpansion
set "_mac_addr=%~1"
for %%z in (
%_mac_addr:|= %
) do for /f "usebackq tokens=1-6 delims=-:" %%a in (
'%%z'
) do (
if "%%z" neq "%%a-%%b-%%c-%%d-%%e-%%f" if "%%z" neq "%%a:%%b:%%c:%%d:%%e:%%f" exit /b -1
for %%g in (
"%%a" "%%b" "%%c" "%%d" "%%e" "%%f"
) do (
2>nul set /a _hx=0x%%~g || exit /b -1
if !_hx! gtr 255 exit /b -1
)
)
endlocal
exit /b 0
::: "Process tools, Use '-h' for a description of the options" "" "usage: %~n0 ps [[option] [...]]" ""
:xlib\ps
call :sub\ps\%*
goto :eof
:sub\ps\
for %%a in (wmic.exe) do if "%%~$path:a"=="" (
PowerShell.exe ^
-NoLogo ^
-NonInteractive ^
-ExecutionPolicy Unrestricted ^
-Command "Get-CimInstance Win32_Process " ^
" | Select-Object ProcessId, CommandLine" ^
" | Format-Table -Wrap"
) else for /f "usebackq tokens=1* delims==" %%a in (`
wmic.exe process get CommandLine^,Description^,ProcessId /value
`) do echo,%%b
goto :eof
::: " -qf [file_path] Queries the file ID of the specified file"
:sub\ps\--qf
:sub\ps\-qf
if not exist "%~f1" exit /b 29 @REM target file not found
fsutil.exe file queryProcessesUsing "%~f1"
goto :eof
::: " --test, -t [exec_name] Test exe if live"
:sub\ps\--test
:sub\ps\-t
@REM tasklist.exe /v /FI "imagename eq %~n1.exe"
if "%~1"=="" exit /b 32 @REM first parameter is empty
for %%a in (wmic.exe) do if "%%~$path:a"=="" (
>nul 2>&1 call PowerShell.exe ^
-NoLogo ^
-NonInteractive ^
-ExecutionPolicy Unrestricted ^
-Command "Get-Process "%~n1" -ErrorAction Stop" && exit /b 0
) else for /f usebackq^ tokens^=2^ delims^=^=^" %%a in (`
2^>nul wmic.exe process where caption^="%~n1.exe" get commandline /value
`) do for %%b in (
%%a
) do if /i "%%~nb"=="%~n1" exit /b 0
@REM "
exit /b -1
::: " --kill, -k [process_name...] kill process"
:sub\ps\--kill
:sub\ps\-k
if "%~1"=="" exit /b 42 @REM first parameter is empty
for %%a in (wmic.exe) do if "%%~$path:a"=="" (
for %%b in (
%*
) do powershell.exe -Command "Stop-Process "%%~na" -Force"
goto :eof
)
for %%a in (
%*
) do wmic.exe process where name="%%~na.exe" delete
@REM for /f "usebackq skip=1" %%a in (`
@REM 2^>nul wmic.exe process where "commandline like '%*'" get processid
@REM `) do for %%b in (%%a) do >nul wmic.exe process where processid="%%b" delete
@REM start "" /b "%~f0" %*
@REM taskkill.exe /f /im %~1.exe
exit /b 0
::: "Time tools, Use '-h' for a description of the options" "" "usage: %~n0 time [[option] [...]]" ""
:xlib\time
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\time\%*
goto :eof
::: " --timestamp, -ts [var_name] Get unix timestamp equals `date +%%s`"
:sub\time\--timestamp
:sub\time\-ts
if "%~1"=="" exit /b 12 @REM variable name is empty
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 13 @REM 'wmic' is not in path
setlocal
:: Win32_UTCTime = Win32_LocalTime - (Win32_TimeZone.Bias * 60)
for /f "usebackq tokens=1* delims==" %%a in (`
wmic.exe /namespace:\\root\cimv2 path Win32_UTCTime GET /value
`) do if "%%~b" neq "" set _%%a=%%b
if %_Month% lss 3 set /a _Year-=1, _Month+=12
set /a _timestamp=^
(^
_Year * 365 + _Year / 4 - _Year / 100 + _Year / 400 + ^
(153 * _Month - 457) / 5 + ^
_Day - 306 - 719163 ^
) * 24 * 60 * 60 + ^
_Hour * 3600 + _Minute * 60 + _Second
endlocal & set /a %~1=%_timestamp%
goto :eof
::: " --now, -n [var_name] [[head_str]] [[tail_str]]" " Display Time at [YYYYMMDDhhmmss]"
:sub\time\--now
:sub\time\-n
if "%~1"=="" exit /b 12 @REM variable name is empty
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 13 @REM 'wmic' is not in path
@REM set date=
@REM set time=
@REM @REM en zh
@REM for /f "tokens=1-8 delims=-/:." %%a in (
@REM "%time: =%.%date: =.%"
@REM ) do if %%e gtr 1970 (
@REM set %~1=%~2%%e%%f%%g%%a%%b%%c%~3
@REM ) else if %%g gtr 1970 set %~1=%~2%%g%%e%%f%%a%%b%%c%~3
setlocal
for /f "usebackq tokens=1,2 delims==+" %%a in (`
wmic.exe /namespace:\\root\cimv2 path Win32_OperatingSystem GET LocalDateTime /value
`) do if "%%~b" neq "" set _%%a=%%b
endlocal & set %~1=%~2%_LocalDateTime:~0,-3%%~3
exit /b 0
::: " --calculate, -c Calculate time intervals, print use time" "" "must be run it before and after function"
:sub\time\--calculate
:sub\time\-c
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 15 @REM 'wmic' is not in path
setlocal
:: 1970/01/01 - 1601/01/01 = 134774 days = 11644473600 seconds
for /f "usebackq tokens=1* delims==" %%a in (`
wmic.exe /namespace:\\root\cimv2 path Win32_TimeZone GET Bias /value ^&
wmic.exe /namespace:\\root\cimv2 path Win32_PerfRawData_PerfOS_System get Timestamp_Sys100NS /value
`) do if "%%~b" neq "" set _%%a=%%b
set _Timestamp_Milliseconds=%_Timestamp_Sys100NS:~0,-5%
set _Timestamp_Seconds=%_Timestamp_Milliseconds:~0,-3%
set _Milliseconds_part=%_Timestamp_Milliseconds:~-3%
for /f "usebackq delims=" %%a in (`
set /a %_Timestamp_Seconds:~0,-2% - 116444736 - _Bias * 60 / 100
`) do set _timestamp=%%a%_Timestamp_Seconds:~-2%.%_Milliseconds_part%
if defined _calculate_time (
set /a "_diff_Timestamp=(%_timestamp:~0,-4% - %_calculate_time:~0,-4%) * 1000 + (%_timestamp:~-3% - %_calculate_time:~-3%)"
set /a _Day=_diff_Timestamp / 86400000, _Hour=_diff_Timestamp %% 86400000 / 3600000, _Minute=_diff_Timestamp %% 3600000 / 60000, _Second=_diff_Timestamp %% 60000 / 1000, _Milliseconds=_diff_Timestamp %% 1000
)
if defined _calculate_time echo %_Day%d %_Hour%h %_Minute%min %_Second%s %_Milliseconds%ms
endlocal & set _calculate_time=%_timestamp%
exit /b 0
::: "Update hosts by ini"
:xlib\hosts
setlocal enabledelayedexpansion
@REM load ini config
call :this\load_ini hosts 1 || exit /b 2 @REM no ini file found
@REM get key array
call :map --keys _keys 1
@REM override mac to ipv4
for /f "usebackq tokens=1*" %%a in (`
call %~nx0 ip --find %_keys%
`) do if not defined _set\%%b (
call :map --put %%b %%a 1 && call :sub\str\--2col-left %%~a %%b
set _set\%%b=-
)
@REM replace hosts in cache
@REM use tokens=2,3 will replace %%a
for /f "usebackq tokens=1* delims=]" %%a in (`
type %windir%\System32\drivers\etc\hosts ^| find.exe /n /v ""
`) do for /f "usebackq tokens=1-3" %%c in (
'. %%b'
) do call :sub\ip\--test %%d && (
call :map --get %%e _value 1 && (
for %%f in (
!_value!
) do call :sub\ip\--test "%%~f" && (
set _line=%%b
call :page --put !_line:%%d=%%f!
) || call :page --put %%b
call :map --remove %%e 1
) || call :page --put %%b
) || call :page --put %%b
call :map --array _arr 1
for %%a in (
%_arr%
) do (
call :sub\ip\--test %%a && (
call :page --put %%~a !_key!
)
set _key=%%~a
)
> %windir%\System32\drivers\etc\hosts call :page --show
endlocal
goto :eof
::: "Wake on LAN" "" "usage: %~n0 wol [MAC_addr] [[broadcast_ipv4]]" ""
:xlib\wol
if "%~1"=="" exit /b 15 @REM MAC address error
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 16 @REM 'wmic' is not in path
setlocal
@REM "
set _broadcast=
if "%~2" neq "" (
set _broadcast=%~2
) else for /f usebackq^ skip^=1^ tokens^=2^ delims^=^" %%a in (`
wmic.exe NicConfig get DefaultIPGateway
`) do set _broadcast=%%~na.255
call :wol %~1 %_broadcast%
endlocal
goto :eof
:wol
PowerShell.exe ^
-NoLogo ^
-NonInteractive ^
-ExecutionPolicy Unrestricted ^
-Command "& {" ^
" $UC = New-Object System.Net.Sockets.UdpClient(\"%~2\", 9);" ^
" $UC.EnableBroadcast = $true;" ^
" $MP = [Byte[]] (, 0xFF * 6) + ((\"%~1\" -split \"[:-]\" | ForEach-Object { [Byte] \"0x$_\"}) * 16);" ^
" $UC.Send($MP, $MP.Length) | Out-Null; $UC.Close();" ^
"}"
goto :eof
::: "Ipv4 tools, Use '-h' for a description of the options" "" "usage: %~n0 ip [option]" ""
:xlib\ip
if "%~1"=="" call :this\annotation %0 & goto :eof
2>nul call :sub\ip\%*
goto :eof
::: " --test, -t [str] Test string if ipv4"
:sub\ip\--test
:sub\ip\-t
if "%~1"=="" exit /b 12 @REM host name is empty
@REM [WARN] use usebackq will set all variable global, by :xlib\hosts
for /f "tokens=1-4 delims=." %%a in (
"%~1"
) do (
if "%~1" neq "%%a.%%b.%%c.%%d" exit /b -1
for %%e in (
"%%a" "%%b" "%%c" "%%d"
) do (
call :sub\is\--integer %%~e || exit /b -1
if %%~e lss 0 exit /b -1
if %%~e gtr 255 exit /b -1
)
)
exit /b 0
::: " --list, -l Show this ipv4"
:sub\ip\--list
:sub\ip\-l
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 15 @REM 'wmic' is not in path
setlocal
@REM Get router ip
call :this\get_route_ip _route
@REM "
for %%a in (
%_route%
) do for /f usebackq^ skip^=1^ tokens^=2^ delims^=^" %%b in (`
wmic.exe NicConfig get IPAddress
`) do if "%%~nb"=="%%~na" echo %%b
endlocal
exit /b 0
::: " --find, -f [MAC] ... Search IPv4 by MAC or Host name" ""
:sub\ip\--find
:sub\ip\-f
if "%~1"=="" exit /b 32 @REM host name is empty
setlocal enabledelayedexpansion
@REM get config
call :this\load_ini sip_setting -f
call :map --get route _routes -f
call :map --get range _range -f
call :map --clear -f
if not defined _range set _range=1-127
call :this\load_ini hosts -f
set _mac_addr=
for %%a in (
%*
) do (
@REM Get value
call :map --get %%a _arg -f && (
set "_mac_addr=!_arg: =!"
) || set _mac_addr=%%a
@REM Format
set "_mac_addr=!_mac_addr::=-!"
call :map --put %%a "!_mac_addr!" -t
)
if not defined _mac_addr exit /b 0
call :map --clear -f
@REM Get router ip
call :this\get_route_ip _get_route_ip
for %%a in (
%_routes% %_get_route_ip%
) do if not defined _tmp\%%~na (
set _tmp\%%~na=-
set "_route=!_route! %%a"
)
call :map --keys _keys -t
@REM Clear arp cache
arp.exe -d
@REM Search MAC
for %%a in (
%_route%
) do for /l %%b in (
%_range:-=,1,%
) do (
call :this\thread_valve 50 cmd.exe --find
start /b %~nx0 \\:ip\--find %%~na.%%b %_keys%
)
@REM print ipv4 for MAC address not catch
for %%a in (
%_keys%
) do call :map --get %%~a _value -t && for %%b in (
!_value:^|^= !
) do call :sub\ip\--test %%b && call :sub\str\--2col-left %%b %%~a
endlocal
exit /b 0
@REM nbtstat
@REM For thread sip
:ip\--find
@REM some ping will fail, but arp success
>nul 2>nul ping.exe -n 1 -w 1 %1
setlocal enabledelayedexpansion
for /f "usebackq skip=3 tokens=1,2" %%a in (`
arp.exe -a %1
`) do for %%c in (
%*
) do call :map --get %%~c _value -t ^
&& if "!_value:%%b=!" neq "!_value!" call :sub\str\--2col-left %%a %%~c
endlocal
exit /b 0
@REM Get router ip
:this\get_route_ip
if "%~1"=="" exit /b 1
for %%a in (wmic.exe) do if "%%~$path:a"=="" exit /b 2 @REM 'wmic' is not in path
setlocal enabledelayedexpansion
set _gateway=
@REM "
for /f usebackq^ skip^=1^ tokens^=2^ delims^=^" %%a in (`
wmic.exe NicConfig get DefaultIPGateway
`) do set _gateway=!_gateway! %%a
endlocal & set %1=%_gateway%
exit /b 0
::: " --forward [[local_ip:]port] [to_ip:port]" " IPv4 forward"
:sub\ip\--forward
call :sub\oset\--vergeq 6.0 || exit /b 41 @REM OS version is too low
:: netsh.exe interface portproxy show all
if "%~2"=="" exit /b 42 @REM args error
setlocal
set _local_ip=%~1
set _to_ip=%~2
for /f "usebackq tokens=1-4" %%a in (
'%_local_ip::= % %_to_ip::= %'
) do if "%%~d"=="" (
netsh.exe interface portproxy add v4tov4 listenport=%%a connectport=%%c connectaddress=%%b
) else netsh.exe interface portproxy add v4tov4 listenport=%%b listenaddress=%%a connectport=%%d connectaddress=%%c
endlocal
exit /b 0
::: "Directory tools, Use '-h' for a description of the options" "" "usage: %~n0 dir [option] [...]" ""
:xlib\dir
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\dir\%*
goto :eof
::: " --isdir, -id [path] Test path is directory"
:sub\dir\--isdir
:sub\dir\-id
setlocal
set _attribute=%~a1-
@REM quick return
set _code=-1
if %_attribute:~0,1%==d set _code=0
endlocal & exit /b %_code%
::: " --islink, -il [file_path] Test path is Symbolic Link"
:sub\dir\--islink
:sub\dir\-il
if not exist "%~f1" exit /b -1
for /f "usebackq delims=" %%a in (`
2^>nul dir /al /b "%~dp1"
`) do if "%%a"=="%~n1" exit /b 0
@REM quick return
exit /b -1
::: " --isfree, -if [dir_path] Test directory is empty"
:sub\dir\--isfree
:sub\dir\-if
call :sub\dir\--isdir %1 || exit /b 32 @REM Not directory
for /f usebackq"" %%a in (`
dir /a /b "%~1"
`) do exit /b -1
exit /b 0
::: " -scs, [dir_path] Set the case sensitive information for a directory"
:sub\dir\--scs
:sub\dir\-scs
fsutil.exe file setCaseSensitiveInfo "%~f1" enable
goto :eof
::: " -qcs [dir_path] [[depth/-r]]" " Query the case sensitive information for a directory" ""
:sub\dir\--qcs
:sub\dir\-qcs
setlocal
set _depth=%~2
if defined _depth (
if /i "%~2"=="-r" (
set _depth=recursive
) else set _depth=recursive %~2
)
fsutil.exe file queryCaseSensitiveInfo "%~f1" %_depth%
endlocal
goto :eof
::: " --mkcsdir, -mcs [dir_path...]" " Creates case sensitive directories" ""
:sub\dir\--mkcsdir
:sub\dir\-mcs
for %%a in (%*) do mkdir "%%~fa" && >nul fsutil.exe file setCaseSensitiveInfo "%%~fa" enable
goto :eof
::: " --clean, [dir_path] Delete empty directory"
:sub\dir\--clean
if not exist "%~1" exit /b 43 @REM target not found
call :sub\dir\--isdir %1 || exit /b 44 @REM target not a directory
if exist %windir%\system32\sort.exe (
call :dir\rdEmptyDirWithSort %1
) else call :dir\rdEmptyDir %1
goto :eof
@REM for :sub\dir\--clean
:dir\rdEmptyDir
if "%~1"=="" exit /b 0
if "%~2"=="" (
call %0 "%~dp1" .
exit /b 0
) else for /d %%a in (
%1*
) do (
2>nul rmdir "%%~a" || call %0 "%%~a\" .
call %0 "%%~a\" .
)
exit /b 0
@REM for :sub\dir\--clean
:dir\rdEmptyDirWithSort
if "%~1"=="" exit /b 2
for /f "usebackq delims=" %%a in (`
dir /ad /b /s %1 ^| sort.exe /r
`) do 2>nul rmdir "%%~a"
exit /b 0
::: " --unique, -u [[dir_path]] Move duplicate files to '.#Trash'"
:sub\dir\--unique
:sub\dir\-u
setlocal enabledelayedexpansion
if exist "%~1" pushd "%cd%"& chdir /d "%~1" || exit /b 54 @REM target not a directory
for /d %%a in (
"%cd%\*"
) do if /i "%%~nxa" neq ".#Trash" call :dir\recursion "%%~a"
for /f "usebackq delims=" %%a in (`
dir /a-d /b "%cd%"
`) do for %%b in (
"%cd%\%%~a"
) do set /a _#\%%~zb += 1 & set _$\%%~zb.!random!!random!=%%~b
if exist "%~1" popd
for /f "usebackq tokens=1* delims==" %%a in (`
2^>nul set _#\
`) do if %%b gtr 1 for /f "usebackq tokens=1* delims==" %%c in (`
2^>nul set _$\%%~na.
`) do for /f "usebackq tokens=1,2 delims=(h" %%e in (`
certutil.exe -hashfile "%%d" sha256
`) do if "%%f"=="" call :dir\hashkey "%%~na" "%%e" "%%d"
for /f "usebackq tokens=1* delims==" %%a in (`
2^>nul set _c\
`) do if %%b gtr 1 (
echo,
echo find '%%b' duplicate file.
for /f "usebackq tokens=1* delims==" %%c in (`
2^>nul set _sh\%%~nxa.
`) do call :dir\pre8 %%~nc %%d
for /f "usebackq skip=1 tokens=1* delims==" %%c in (`
2^>nul set _sh\%%~nxa.
`) do >nul mkdir ".#Trash%%~pd"& echo !_size!,MOVE '%%~d' -^> '.#Trash%%~pd'& >nul move "%%~d" ".#Trash%%~pd"
)
endlocal
goto :eof
:dir\recursion
for /r %1 %%a in (*?*) do set /a _#\%%~za += 1 & set _$\%%~za.!random!!random!=%%~a
goto :eof
:dir\hashkey
setlocal
set _arg=%~2
set _arg=%_arg: =%.%~1
endlocal & set /a _c\%_arg% += 1 & set _sh\%_arg%.%random%%random%=%~3
goto :eof
:dir\pre8
setlocal
set _arg=%~1
echo %_arg:~0,8%,%~2
endlocal & for /f "usebackq delims=." %%a in ('%~x1') do set _size=%%~a
goto :eof
::: "Windows Remote Management, Use '-h' for a description of the options" "" "usage: %~n0 dir [option] [...]" ""
:xlib\wrm
@REM TODO
goto :eof
::: "Operating system setting, Use '-h' for a description of the options" "" "usage: %~n0 oset [option] [...]" ""
:xlib\oset
if "%~1"=="" call :this\annotation %0 & goto :eof
call :sub\oset\%*
goto :eof
:: TODO change mac addr: reg.exe add hklm\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}/0001 /v NetworkAddress /t REG_SZ /d abcdef012345 /f
::: " --vergeq, -vg [version] Test current version is greater than the given value"
:sub\oset\--vergeq
:sub\oset\-vg
if "%~x1"=="" exit /b 12 @REM Parameter is empty or Not a float
setlocal
call :oset\this_version_multiply_10 _this_ver
for /f "usebackq tokens=1,2 delims=." %%a in (
'%~1'
) do set /a _ver=%_this_ver% - %%a * 10 - %%b
endlocal & if %_ver% geq 0 exit /b 0
exit /b -1
:oset\this_version_multiply_10 [variable_name]
if "%~1"=="" exit /b 1
for %%a in (wmic.exe) do if "%%~$path:a"=="" (
for /f "usebackq tokens=1,2 delims=." %%a in (`
PowerShell.exe ^
-NoLogo ^
-NonInteractive ^
-ExecutionPolicy Unrestricted ^
-Command "[System.Environment]::OSVersion.Version.ToString()"
`) do set /a %~1=%%a * 10 + %%b
) else for /f "usebackq skip=1" %%a in (`
wmic.exe os get Version
`) do for %%b in (%%~na) do for /f "usebackq tokens=1,2 delims=." %%c in (
'%%b'
) do set /a %~1=%%c * 10 + %%d
exit /b 0
::: " --cleanup, -c [[path]] Component Cleanup"
:sub\oset\--cleanup
:sub\oset\-c
if "%~1"=="" dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase & exit /b 0
call :sub\dir\--isdir "%~1" || exit /b 23 @REM not a directory
call :sub\dir\--isdir "%~2" || exit /b 20 @REM scratch directory not exist
dism.exe /Image:%1 /Cleanup-Image /StartComponentCleanup /ResetBase /ScratchDir:"%~2"
exit /b 0
@REM OS version
::: " --version, -v [os_path] [[var_name]] Get OS version"
:sub\oset\--version
:sub\oset\-v
if "%~1"=="" (
@REM for /f "usebackq delims==" %%a in (`
@REM ver
@REM `) do for %%b in (
@REM %%a
@REM ) do if "%%~xb" neq "" echo %%~nb
for /f "usebackq tokens=1* delims==" %%a in (`
wmic.exe os get Version /value
`) do if "%%b" neq "" echo %%b
goto :eof
)
if "%~1" neq "" if not exist "%~1" exit /b 33 @REM not a directory
if "%~1"=="%~d1" (
call :oset\version %~1\ %2
) else if "%~dp1"=="%~f1" (
call :oset\version "%~f1" %2
) else call :oset\version "%~f1\" %2
goto :eof
:oset\version
for /f "usebackq" %%a in (`
2^>nul dir /ad /b %~1Windows\servicing\Version\*.*
`) do if exist %~1Windows\servicing\Version\%%a\*_installed if "%~2"=="" (
echo %%a& exit /b 0
) else set %~2=%%a& exit /b 0
exit /b 34 @REM Not OS path or Low OS version
::: " --sid, -s [[user_name]] [[var_name]] Get sid by username. if not set path, will get online info" ""
:sub\oset\--sid
:sub\oset\-s
if "%~1"=="" (
for /f "usebackq skip=1" %%a in (`
wmic.exe useraccount where name^='%username%' get sid
`) do for %%b in (%%a) do echo,%%b
) else for /f "usebackq skip=1" %%a in (`
wmic.exe useraccount where name^='%1' get sid
`) do for %%b in (%%a) do if "%~2"=="" (
echo,%%b
) else set %~2=%%b
exit /b 0
::: " --bit, -b [os_path] [[var_name]] Get OS bit"
:sub\oset\--bit
:sub\oset\-b
if not exist "%~1" exit /b 53 @REM not a directory
if "%~1"=="%~d1" (
call :oset\bit %~1\ %2
) else if "%~dp1"=="%~f1" (
call :oset\bit "%~f1" %2
) else call :oset\bit "%~f1\" %2
goto :eof
:oset\bit
if not exist %~1Windows\servicing\Version exit /b 64 @REM Not OS path or Low OS version
for /d %%a in (
%~1Windows\servicing\Version\*.*
) do if exist %%a\arm64_installed (
if "%~2"=="" (
echo arm64
) else set "%~2=arm64"
exit /b 0
) do if exist %%a\amd64_installed (
if "%~2"=="" (
echo amd64
) else set "%~2=amd64"
exit /b 0
) else if exist %%a\x86_installed (
if "%~2"=="" (
echo x86
) else set "%~2=x86"
exit /b 0
)
exit /b 66 @REM System version is too old
:: https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc287874(v=office.12)
:: https://docs.microsoft.com/en-us/previous-versions/commerce-server/ee825488(v=cs.20)
::: " --language, -lang [var_name] [[os_path]] Get OS current language"
:sub\oset\--language
:sub\oset\-lang
setlocal
call :regedit\on
set _load_point=HKLM\load-point%random%
if "%~2" neq "" (
reg.exe load %_load_point% %~2\Windows\System32\config\DRIVERS || (
call :regedit\off
exit /b 77 @REM not operating system directory
)
for /f "usebackq tokens=1,4 delims=x " %%a in (`
reg.exe query %_load_point%\select
`) do if "%%b"=="Default" call :lang\current %_load_point%\ControlSet00%%b _lang || (
call :regedit\off
exit /b 78 @REM not support
)
reg.exe unload %_load_point%
) else call :lang\current HKLM\SYSTEM\CurrentControlSet _lang || (
call :regedit\off
exit /b 79 @REM not support
)
call :regedit\off
if "%~1"=="" echo,%_lang%
endlocal & if "%~1" neq "" set %~1=%_lang%
goto :eof
@REM for :sub\oset\--language
:lang\current
for /f "usebackq tokens=1,3" %%a in (`
reg.exe query %~1\Control\Nls\Language /v Default
`) do if "%%a"=="Default" for %%c in (
af-ZA.0436 ar-AE.3801 ar-BH.3C01 ar-DZ.1401 ar-EG.0C01 ar-IQ.0801 ar-JO.2C01 ar-KW.3401 ar-LB.3001 ar-LY.1001 ar-MA.1801 ar-OM.2001 ar-QA.4001 ar-SA.0401 ar-SY.2801 ar-TN.1C01 ar-YE.2401
be-BY.0423 bg-BG.0402
ca-ES.0403 cs-CZ.0405 Cy-az-AZ.082C Cy-sr-SP.0C1A Cy-uz-UZ.0843
da-DK.0406 de-AT.0C07 de-CH.0807 de-DE.0407 de-LI.1407 de-LU.1007 div-MV.0465
el-GR.0408 en-AU.0C09 en-BZ.2809 en-CA.1009 en-CB.2409 en-GB.0809 en-IE.1809 en-JM.2009 en-NZ.1409 en-PH.3409 en-TT.2C09 en-US.0409 en-ZA.1C09 en-ZW.3009 es-AR.2C0A es-BO.400A es-CL.340A es-CO.240A
es-CR.140A es-DO.1C0A es-EC.300A es-ES.0C0A es-GT.100A es-HN.480A es-MX.080A es-NI.4C0A es-PA.180A es-PE.280A es-PR.500A es-PY.3C0A es-SV.440A es-UY.380A es-VE.200A et-EE.0425 eu-ES.042D
fa-IR.0429 fi-FI.040B fo-FO.0438 fr-BE.080C fr-CA.0C0C fr-CH.100C fr-FR.040C fr-LU.140C fr-MC.180C
gl-ES.0456 gu-IN.0447
he-IL.040D hi-IN.0439 hr-HR.041A hu-HU.040E hy-AM.042B
id-ID.0421 is-IS.040F it-CH.0810 it-IT.0410
ja-JP.0411
ka-GE.0437 kk-KZ.043F kn-IN.044B kok-IN.0457 ko-KR.0412 ky-KZ.0440
Lt-az-AZ.042C lt-LT.0427 Lt-sr-SP.081A Lt-uz-UZ.0443 lv-LV.0426
mk-MK.042F mn-MN.0450 mr-IN.044E ms-BN.083E ms-MY.043E
nb-NO.0414 nl-BE.0813 nl-NL.0413 nn-NO.0814
pa-IN.0446 pl-PL.0415 pt-BR.0416 pt-PT.0816
ro-RO.0418 ru-RU.0419
sa-IN.044F sk-SK.041B sl-SI.0424 sq-AL.041C sv-FI.081D sv-SE.041D sw-KE.0441 syr-SY.045A
ta-IN.0449 te-IN.044A th-TH.041E tr-TR.041F tt-RU.0444