/
batchsample3.bat
4264 lines (3657 loc) · 187 KB
/
batchsample3.bat
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
@echo off
:setup
Set InitialErrorLevel=%ErrorLevel%
Set InitialWorkingDirectory=%CD%
Set FolderOfBatchFile=%~dp0
Set Pushed=true & Call :GoToFolderOfBatchFile %FolderOfBatchFile%
REM Set ISCONSOLE
REM Set IsAdmin ?
REM Set ISSILENT
REM enable logging / logging errors example logging with macro if /i "%%A"=="-log" (set "_log=>> %~n0.log")
REM Call ELEVATE ?
REM pretend variable set to true will only echo irreversible commands, rather than execute them
Set pretend=false
REM quiet will supress non-essential outputs
Set quiet=false
REM verbose will output extra comments and status messages
Set verbose=false
REM if logging is set to enabled, console output will be written to files, with timestamps using tee
Set logging=true
REM Set logfile=c:\mylogfile.txt
REM Set errlogfile=c:\myerrlogfile.txt
:Constants
:Macros
rem SETUP your variables and commands
REM get into variables ip addresses, subnet, routes, dns server, time server, firewall status, defender status, UAC status
REM list all network devices into an array
REM list all WMI sensors, temperatures voltage and power
REM list all devices in device manager
REM list all usb devices (usbdevview)
REM list bluetooth devices into list array
REM list displays, resolutions, hertzs, relative position, mode (off, extended, duplicate etc) and orientation
rem list ADB android devices and connect to them
rem check for presence of software
REM request user input
REM logging function, error loggin function, other STD ?
rem file to array function
rem array to file function
rem change path in a list of path+files
rem base64 encode decode
rem hex encode decode
rem basic certutil functions
rem hash functions
rem process is running , kill process functions
rem read config file and understand arguments mysetting=value type
rem check for --help requested in argument
:main
GoTo :ElevateDEMO
GoTo :END
GoTo :END
REM Call :TrueFalseDemo
REM Call :EchoCommand "set"
REM GoTo :RegistryHandlingDEMO
GoTo :END
REM GoTo :AppendToPathDEMO
REM BASIC DEMO
:BASICDEMO
REM list all environement varibale
REM demonstration of the all the operators
rem list order of operation
rem demonstrate all conditionnal operations
rem demontrate chained command && || & etc..
rem demonstrate quoting and escaping
rem demonstrate string manipulation left right mid substitution splitting joining
GoTo :END
:END
IF "%Pushed%"=="true" set Pushed= & Call :ReturnToInitialFolder
IF "%IsConsole%"=="false" pause
exit /b 1
:: Internal functions
:: Function library
:: Perform self-elevation, passing all arguments through.
:ElevateAndWait
set _ElevateAndWait=-Wait
:Elevate
if defined args set args=%args:^=^^%
if defined args set args=%args:<=^<%
if defined args set args=%args:>=^>%
if defined args set args=%args:&=^&%
if defined args set args=%args:|=^|%
if defined args set "args=%args:"=\"\"%"
:: Note:
:: * To keep the elevated session open until explicitly exited by the user,
:: use /k instead of /c
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
" Start-Process %_ElevateAndWait% -Verb RunAs -FilePath cmd -ArgumentList \"/c \"\" cd /d \"\"%CD% \"\" ^&^& \"\"%~f0\"\" %args% \"\" \" "
goto :EOF
REM https://stackoverflow.com/questions/76040676/batch-file-auto-elevation-using-powershell-and-not-mangling-the-arguments-but-pr/76055486?noredirect=1#comment134178642_76055486
:: Test if this session is elevated.
:: `net session` only succeeds and therefore reports exit code 0
:: in an elevated session.
:IsAdmin
if not "[%1]"=="[]" set %1=false
net session >NUL 2>&1
if %ERRORLEVEL% == 0 if not "[%1]"=="[]" set %1=true
net session >NUL 2>&1
goto :EOF
REM INCOMPLETE FUNCTION
REM HELP
:HELP
echo helpful help here
GoTo :EOF
REM :Banner
REM :Version
REM not tested ?
:: Usage Call :EchoArguments your arguments here
:EchoArguments
if "[%0]" == "[]" GoTo :EchoArgumentsEND
if "[%_index%]" == "[]" set _index=0
echo.
echo %_index% verbatim %%0=%0
echo %_index% unquoted %%~0=%~0
echo %_index% drive %%~d0=%~d0
echo %_index% path %%~p0=%~p0
echo %_index% filename %%~n0=%~n0
echo %_index% extension %%~x0=%~x0
echo %_index% full path %%~f0=%~f0
echo %_index% drive and path %%~dp0=%~dp0
echo %_index% full path %%~dpnx0=%~dpnx0
echo %_index% short path %%~s0=%~s0
echo %_index% attributes %%~a0=%~a0
echo %_index% file time %%~t0=%~t0
echo %_index% path search %%~$PATH:0=%~$PATH:0
shift
set /a _index+=1
GoTo :EchoArguments
:EchoArgumentsEND
set _index=
GoTo :EOF
REM INCOMPLETE
:: Usage Call :EchoArguments your arguments here
:ArgumentsToArray
if "[%Arguments%]"=="[]" set "Arguments=Arguments"
:ArgumentsToArrayMAIN
if "[%0]" == "[]" GoTo :EchoArgumentsEND
if "[%_index%]" == "[]" set _index=0
set "_buffer=%0"
set "Arguments[%_index%].verbatim=%0"
set "Arguments[%_index%]=%~0"
if not "[%~a0]"=="[]" echo %0 has attributes
if /i not "[%%_buffer:%~d0%=%%]"=="[%0]" echo drive is in variable
REM if /i not "[%_buffer:%~p0%=]"=="[%0]" echo drive is in variable
REM echo.
REM echo %index% verbatim %%0=%0
REM echo %index% unquoted %%~0=%~0
REM echo %index% drive %%~d0=%~d0
REM echo %index% path %%~p0=%~p0
REM echo %index% filename %%~n0=%~n0
REM echo %index% extension %%~x0=%~x0
REM echo %index% full path %%~f0=%~f0
REM echo %index% drive and path %%~dp0=%~dp0
REM echo %index% full path %%~dpnx0=%~dpnx0
REM echo %index% short path %%~s0=%~s0
REM echo %index% attributes %%~a0=%~a0
REM echo %index% file time %%~t0=%~t0
REM echo %index% path search %%~$PATH:0=%~$PATH:0
shift
set /a _index+=1
GoTo :EchoArguments
:ArgumentsToArrayEND
set Arguments=
set _index=
GoTo :EOF
::Usage Call :QUIT 'note this kills the process and does not return to console
:QUIT
title QUITTHISNOW
taskkill /f /fi "WINDOWTITLE eq QUITTHISNOW"
exit /b
:: Usage Call :Sleep 5 ' Sleep time in seconds
:Sleep
timeout /t %~1
GoTo :EOF
:: Usage Call :ClearLocalVariables
:ClearLocalVariables
for /f "tokens=1,2 delims==" %%a in ('set _ 2^>nul') do set %%a=
GoTo :EOF
:: Usage Call :ClearVariablesByPrefix myPrefix
:ClearVariablesByPrefix
for /f "tokens=1,2 delims==" %%a in ('set %~1 2^>nul') do set %%a=
if "[%~1]" NEQ "[]" shift & GoTo :ClearVariablesByPrefix
GoTo :EOF
:GoToFolderOfBatchFile
pushd %1
GoTo :EOF
:ReturnToInitialFolder
popd
GoTo :EOF
::Usage Call :ShowCodepage
:ShowCodepage
chcp
GoTo :EOF
::Usage Call :GetCodePage ReturnValue
:GetCodePage
for /f "tokens=1,2 delims=:" %%a in ('chcp') do set %1=%%b
call set %1=%%%1:~1%%
GoTo :EOF
::Usage Call :SetCodePage NewCodePage
:SetCodePage
chcp %~1
GoTo :EOF
::Usage Call :True ReturnValue 'Always returns true, both return value and errorlevel
:true
if not "[%1]"=="[]" set %1=true
exit /b 0
REM Call :True ReturnValue && echo it is always true (errorlevel 0) || echo it is never false (errorlevel not 0)
::Usage Call :false ReturnValue 'Always returns false, both return value and errorlevel
:false
if not "[%1]"=="[]" set %1=false
exit /b 1
REM Call :false ReturnValue && echo it is never true (errorlevel 0) || echo it is always false (errorlevel not 0)
REM thanks https://www.tutorialspoint.com/batch_script/
:: ------------------------------
:: ----- Array Manipulation -----
:: ------------------------------
::Usage Call :Deconcatenate Delimiter Input Output 'Returns array starting at [0], see .lbound and .ubound
:deconcatenate
set "_delimiter=%~1" & set /a _delimiter.len=0
set "_input=%~2" & set /a _input.len=0
call :len _delimiter _delimiter.len
call :len _input _input.len
set /a _cursor=0 & set /a _token=0 & set "_char=" & set "_buffer="
set /a %3.lbound=%_cursor%
REM if "[%debug%]"=="[true]" echo input [%_input%] len %_input.len% delimiter [%_delimiter%] len %_delimiter.len%
:deconcatenate_input_loop
REM if "[%debug%]"=="[true]" pause
call set _char=%%_input:~%_cursor%,1%%
REM if "[%debug%]"=="[true]" echo char %_char%
set /a _cursor+=1
set /a _cursor2=0 & set "_IsDelimiter=false"
:deconcatenate_delimiter_loop
call set _charDelimiter=%%_delimiter:~%_cursor2%,1%%
if "[%_char%]"=="[%_charDelimiter%]" set "_IsDelimiter=true"
if not "[%_IsDelimiter%]"=="[true]" set "_buffer=%_buffer%%_char%"
REM if "[%debug%]"=="[true]" echo _charDelimiter [%_charDelimiter%] _IsDelimiter [%_IsDelimiter%] _cursor %_cursor% _cursor2 %_cursor2% _buffer %_buffer%
set /a _cursor2+=1
if %_cursor2% LSS %_delimiter.len% goto :deconcatenate_delimiter_loop
REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" echo writing set %~3[%_token%] = %_buffer%
if "[%_IsDelimiter%]"=="[true]" call set "%~3[%%_token%%]=%_buffer%" & set "_buffer=" & set /a _token+=1
REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" pause
if %_cursor% LSS %_input.len% goto :deconcatenate_input_loop
if %_cursor% EQU %_input.len% call set "%~3[%%_token%%]=%_buffer%" & set "_buffer=" & set /a %3.ubound=%_token%
GoTo :EOF
::Usage Call :Deconcatenate Delimiter Input Output 'Returns array starting at [0], see .lbound and .ubound
:DeconcatenateFast
Set "_delimiter=%~1"
Set "_input=%~2"
set /a index=0
set /a %3.lbound=%index%
call set _input="%%_input:%_delimiter%=" "%%"
for %%w in (%_input%) do (
REM echo %%w
call set "%~3[%%index%%]=%%w"
set /a "index+=1"
)
set /a "index-=1"
set %3.ubound=%index%
GoTo :EOF
:: Requires :len (maybe support %ReturnEmpty% for special cases ?)
::Usage Call :GetLastElement Delimiter Input Output 'Returns the last element in string based on specified delimiter
:GetLastElement
set "_delimiter=%~1" & set /a _delimiter.len=0
set "_input=%~2" & set /a _input.len=0
call :len _delimiter _delimiter.len
call :len _input _input.len
set /a _cursor=%_input.len%-1 & set "_char=" & set "_buffer="
REM if "[%debug%]"=="[true]" echo input [%_input%] len %_input.len% delimiter [%_delimiter%] len %_delimiter.len%
:GetLastElement_input_loop
REM if "[%debug%]"=="[true]" pause
call set _char=%%_input:~%_cursor%,1%%
REM if "[%debug%]"=="[true]" echo char %_char%
set /a _cursor-=1
set /a _cursor2=0 & set "_IsDelimiter=false"
:GetLastElement_delimiter_loop
call set _charDelimiter=%%_delimiter:~%_cursor2%,1%%
if "[%_char%]"=="[%_charDelimiter%]" set "_IsDelimiter=true"
if not "[%_IsDelimiter%]"=="[true]" set "_buffer=%_char%%_buffer%"
REM if "[%debug%]"=="[true]" echo _charDelimiter [%_charDelimiter%] _IsDelimiter [%_IsDelimiter%] _cursor %_cursor% _cursor2 %_cursor2% _buffer %_buffer%
set /a _cursor2+=1
if %_cursor2% LSS %_delimiter.len% goto :GetLastElement_delimiter_loop
REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" if not "[%_buffer%]"=="[]" echo is a delimiter and buffer not empty %_buffer%
if "[%_IsDelimiter%]"=="[true]" if not "[%_buffer%]"=="[]" set "%~3=%_buffer%" & GoTo :EOF
if %_cursor% LSS %_input.len% goto :GetLastElement_input_loop
GoTo :EOF
::Usage Call :Deconcatenate Delimiter Input Output 'Returns the last element in string based on specified delimiter
:GetLastElementFast
Set "_delimiter=%~1"
Set "_input=%~2"
call set _input="%%_input:%_delimiter%=" "%%"
for %%w in (%_input%) do (
REM echo %%w
call set "%3=%%~w"
)
GoTo :EOF
::Usage Call :GetNthElement Delimiter Input Output NthCount 'Returns Nth element from string using specified delimiter
:GetNthElement
set "_delimiter=%~1" & set /a _delimiter.len=0
set "_input=%~2" & set /a _input.len=0
call :len _delimiter _delimiter.len
call :len _input _input.len
set /a _cursor=0 & set /a _token=0 & set "_char=" & set "_buffer="
::set /a %3.lbound=%_cursor%
REM if "[%debug%]"=="[true]" echo input [%_input%] len %_input.len% delimiter [%_delimiter%] len %_delimiter.len%
:GetNthElement_input_loop
REM if "[%debug%]"=="[true]" pause
call set _char=%%_input:~%_cursor%,1%%
REM if "[%debug%]"=="[true]" echo char %_char%
set /a _cursor+=1
set /a _cursor2=0 & set "_IsDelimiter=false"
:GetNthElement_delimiter_loop
call set _charDelimiter=%%_delimiter:~%_cursor2%,1%%
if "[%_char%]"=="[%_charDelimiter%]" set "_IsDelimiter=true"
if not "[%_IsDelimiter%]"=="[true]" set "_buffer=%_buffer%%_char%"
REM if "[%debug%]"=="[true]" echo _charDelimiter [%_charDelimiter%] _IsDelimiter [%_IsDelimiter%] _cursor %_cursor% _cursor2 %_cursor2% _buffer %_buffer%
set /a _cursor2+=1
if %_cursor2% LSS %_delimiter.len% goto :GetNthElement_delimiter_loop
REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" echo if %_token% EQU %4
if "[%_IsDelimiter%]"=="[true]" if %_token% EQU %4 set "%~3=%_buffer%" & GoTo :EOF
if "[%_IsDelimiter%]"=="[true]" set "_buffer=" && set /a _token+=1
REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" pause
if %_cursor% LSS %_input.len% goto :GetNthElement_input_loop
::if %_cursor% EQU %_input.len% call set "%~3[%%_token%%]=%_buffer%" & set "_buffer=" & set /a %3.ubound=%_token%
if %_token% EQU %4 set "%~3=%_buffer%"
GoTo :EOF
::Usage Call :GetNthElementFast Delimiter Input Output NthCount 'Returns Nth element from string using specified delimiter
:GetNthElementFast
Set "_delimiter=%~1"
Set "_input=%~2"
set "_buffer="
set /a index=0
call set _input="%%_input:%_delimiter%=" "%%"
setlocal enableDelayedExpansion
for %%w in (%_input%) do (
REM echo %%w
call set "_buffer=%%~w"
if !index! EQU %4 GoTo :GetNthElementFast_ExitFor
set /a "index+=1"
)
REM clearing buffer, index did not reach count
set "_buffer="
:GetNthElementFast_ExitFor
endlocal & set "%3=%_buffer%"
GoTo :EOF
::EchoArguments HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
:: Usage :GetPreviousElement InputArray SelectedElement
:GetPreviousElement
REM Return element on cursor
REM move cursor forward, if reach ubound, loop back to lbound
GoTo :EOF
:: Usage :GetNextElement InputArray SelectedElement
:GetNextElement
REM Return element on cursor
REM move cursor forward, if reach ubound, loop back to lbound
GoTo :EOF
:: Usage :DeleteElement optional ElementIndex
:DeleteElement
REM delete element on cursor
REM delete ElementIndex if specified
REM close gap in array
REM update ubound and count
REM (maybe control direction, but for now, hardcode pulling on ubound)
REM If element is out of bounds, do nothing but exit code with error
GoTo :EOF
:: Usage :GetNextElement DeleteElement optional ElementIndex
:AddElement
REM Add new element after ubound, update ubound and count
REM If element index, add to this index and push all values toward ubound+1
REM update ubound and count
:: Set EchoArrayPrefix EchoArraySuffix to insert text before or after
:: Example set EchoArrayPrefix=InputArray[%%index%%]=
:: EchoArray InputArray optional start optional end
:EchoArray
set /a index=0
set /a limit=2147483647
if not ["%~2"]==[""] set /a index=%~2
if not ["%~3"]==[""] set /a limit=%~3
call set "lbound=%%%~1.lbound%%"
call set "ubound=%%%~1.ubound%%"
call set "len=%%%~1.len%%"
call set "next=%%%~1.next%%"
call set "previous=%%%~1.previous%%"
if not ["%lbound%"]==[""] call echo %~1.lbound %%%~1.lbound%%
if not ["%ubound%"]==[""] call echo %~1.ubound %%%~1.ubound%%
if not ["%len%"]==[""] call echo %~1.len %%%~1.len%%
if not ["%next%"]==[""] call echo %~1.next %%%~1.next%%
if not ["%previous%"]==[""] call echo %~1.previous %%%~1.previous%%
:EchoArray-internal-loop
if not defined %1[%index%] GoTo :EOF
call echo %EchoArrayPrefix%%%%1[%index%]%%%EchoArraySuffix%
set /a index+=1
if %index% GTR %limit% goto :EOF
GoTo :EchoArray-internal-loop
REM TODO MAKE VERSION THAT OUTPUTS TO ARRAY OR FILE
REM This function takes elements numbers from ElementsArray
REM Then it retrieves those elements from the ReferenceArray (with suffix, if provided)
REM And outputs them to OutputArray
REM Elements are appended to OutputArray starting from OutputArray.ubound
REM Suffixes are defined with _DerefArrayReferenceSuffix, space separated for multiple elements
REM The suffixes will be used on the output array unless overriden by _DerefArrayOutputSuffix
REM The suffix NOSUFFIX has the special meaning of an empty suffix
::Usage Call :DereferenceArrayToArray ElementsArray ReferenceArray OutputArray
:DereferenceArrayToArray
if "[%~1]"=="[]" GoTo :EOF
if "[%~2]"=="[]" GoTo :EOF
if "[%~3]"=="[]" GoTo :EOF
set "_DerefArrayElementsArray=%~1" & set "_DerefArrayReferenceArray=%~2" & set "_DerefArrayOutputArray=%~3"
REM call echo call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
if not "[%_DerefArrayReferenceSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayReferenceSuffix%" _DerefArrayReferenceSuffix
if not "[%_DerefArrayOutputSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayOutputSuffix%" _DerefArrayOutputSuffix
if "[%_DerefArrayReferenceSuffix.ubound%]"=="[]" set /a _DerefArrayReferenceSuffix.ubound=0
REM Set _DerefArrayOutput's index to OutputArray's .ubound+1, if any, or else set to zero
set _DerefArrayOutput.index=%_DerefArrayOutputArray.ubound%
if not "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index+=1
if "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index=0
set /a _DerefArrayElements.index=0
:DereferenceArrayToArray-output-loop
set /a _DerefArraySuffix.index=0
:DereferenceArrayToArray-suffix-loop
REM if %_DerefArrayElements.index% LEQ 0 call echo %_DerefArrayReferenceSuffix[0]% %_DerefArrayReferenceSuffix% %_DerefArraySuffix.index%
call set "_DerefArrayReferenceSuffix=%%_DerefArrayReferenceSuffix[%_DerefArraySuffix.index%]%%"
call set "_DerefArrayOutputSuffix=%%_DerefArrayOutputSuffix[%_DerefArraySuffix.index%]%%"
if "[%_DerefArrayReferenceSuffix%]"=="[NOSUFFIX]" set _DerefArrayReferenceSuffix=
if "[%_DerefArrayOutputSuffix%]"=="[]" set "_DerefArrayOutputSuffix=%_DerefArrayReferenceSuffix%"
if "[%_DerefArrayOutputSuffix%]"=="[NOSUFFIX]" set _DerefArrayOutputSuffix=
REM if %_DerefArrayElements.index%==0 echo ref-suffix "%_DerefArrayReferenceSuffix%" out-suffix "%_DerefArrayOutputSuffix%"
REM call call echo %%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%% %_DerefArrayReferenceSuffix%
call call set %_DerefArrayOutputArray%[%_DerefArrayOutput.index%]%_DerefArrayOutputSuffix%=%%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%%
set %_DerefArrayOutputArray%.ubound=%_DerefArrayOutput.index%
set /a _DerefArraySuffix.index+=1
REM echo _DerefArrayReferenceSuffix.ubound is %_DerefArrayReferenceSuffix.ubound%
REM echo if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
REM if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% GoTo :DereferenceArrayToArray-suffix-loop
set /a _DerefArrayOutput.index+=1
set /a _DerefArrayElements.index+=1
if %_DerefArrayElements.index% LEQ %_DerefArrayElementsArray.ubound% GoTo :DereferenceArrayToArray-output-loop
REM Call :ClearVariablesByPrefix _DerefArray
REM set _DerefArray
GoTo :EOF
::Usage Call :CreateTestArray ArrayName Count Suffixes
:: Will create array ArrayName from element 0 to Count, with all listed suffixes
:: Each array value will contain the element number followed by the suffix name
:CreateTestArray
set "_CreateTestArray_ArrayName=%~1"
set /a _CreateTestArray_ArrayCount=%~2
if "[%~3]"=="[]" (
set /a _CreateTestArray_suffix.index=0
set /a _CreateTestArray_suffix.ubound=0
set "_CreateTestArray_suffix[0]=NOSUFFIX"
GoTo :CreateTestArray-arguments-to-suffix-loop-skip
)
set /a _CreateTestArray_suffix.index=0
:CreateTestArray-arguments-to-suffix-loop
set "_CreateTestArray_suffix[%_CreateTestArray_suffix.index%]=%~3"
set _CreateTestArray_suffix.ubound=%_CreateTestArray_suffix.index%
set /a _CreateTestArray_suffix.index+=1
shift
if not "[%~3]"=="[]" GoTo :CreateTestArray-arguments-to-suffix-loop
:CreateTestArray-arguments-to-suffix-loop-skip
set /a _CreateTestArray_ArrayIndex=0
:CreateTestArray-loop-start
set /a _CreateTestArray_suffix.index=0
:CreateTestArray-suffix-loop
set "_CreateTestArray_suffix.current="
call set "_CreateTestArray_suffix.current=%%_CreateTestArray_suffix[%_CreateTestArray_suffix.index%]%%"
if "[%_CreateTestArray_suffix.current%]"=="[NOSUFFIX]" set "_CreateTestArray_suffix.current="
set "_CreateTestArray_suffix.current.content="
if not "[%_CreateTestArray_suffix.current%]"=="[]" set "_CreateTestArray_suffix.current.content=-%_CreateTestArray_suffix.current%"
set %_CreateTestArray_ArrayName%[%_CreateTestArray_ArrayIndex%]%_CreateTestArray_suffix.current%=%_CreateTestArray_ArrayIndex%%_CreateTestArray_suffix.current.content%
set /a _CreateTestArray_suffix.index+=1
if %_CreateTestArray_suffix.index% LEQ %_CreateTestArray_suffix.ubound% Goto :CreateTestArray-suffix-loop
set /a _CreateTestArray_ArrayIndex+=1
if %_CreateTestArray_ArrayIndex% LEQ %_CreateTestArray_ArrayCount% Goto :CreateTestArray-loop-start
set /a %_CreateTestArray_ArrayName%.count=%_CreateTestArray_ArrayIndex%
set /a _CreateTestArray_ArrayIndex-=1
set /a %_CreateTestArray_ArrayName%.lbound=0
set /a %_CreateTestArray_ArrayName%.ubound=%_CreateTestArray_ArrayIndex%
Call :ClearVariablesByPrefix _CreateTestArray
GoTo :EOF
REM DereferenceArrayToArray and CopyMultipleArrayElements are almost identical, should be rewritten as a unique function that does it all
REM TODO MAKE VERSION THAT OUTPUTS TO ARRAY OR FILE
REM This function takes elements numbers from ElementsArray
REM Then it retrieves those elements from the ReferenceArray (with suffix, if provided)
REM And outputs them to OutputArray
REM Elements are appended to OutputArray starting from OutputArray.ubound
REM Suffixes are defined with _DerefArrayReferenceSuffix, space separated for multiple elements
REM The suffixes will be used on the output array unless overriden by _DerefArrayOutputSuffix
REM The suffix NOSUFFIX has the special meaning of an empty suffix
::Usage Call :DereferenceArrayToArray ElementsArray ReferenceArray OutputArray
:DereferenceArrayToArray
if "[%~1]"=="[]" GoTo :EOF
if "[%~2]"=="[]" GoTo :EOF
if "[%~3]"=="[]" GoTo :EOF
set "_DerefArrayElementsArray=%~1" & set "_DerefArrayReferenceArray=%~2" & set "_DerefArrayOutputArray=%~3"
REM call echo call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
if not "[%_DerefArrayReferenceSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayReferenceSuffix%" _DerefArrayReferenceSuffix
if not "[%_DerefArrayOutputSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayOutputSuffix%" _DerefArrayOutputSuffix
if "[%_DerefArrayReferenceSuffix.ubound%]"=="[]" set /a _DerefArrayReferenceSuffix.ubound=0
REM Set _DerefArrayOutput's index to OutputArray's .ubound+1, if any, or else set to zero
set _DerefArrayOutput.index=%_DerefArrayOutputArray.ubound%
if not "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index+=1
if "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index=0
set /a _DerefArrayElements.index=0
:DereferenceArrayToArray-output-loop
set /a _DerefArraySuffix.index=0
:DereferenceArrayToArray-suffix-loop
REM if %_DerefArrayElements.index% LEQ 0 call echo %_DerefArrayReferenceSuffix[0]% %_DerefArrayReferenceSuffix% %_DerefArraySuffix.index%
call set "_DerefArrayReferenceSuffix=%%_DerefArrayReferenceSuffix[%_DerefArraySuffix.index%]%%"
call set "_DerefArrayOutputSuffix=%%_DerefArrayOutputSuffix[%_DerefArraySuffix.index%]%%"
if "[%_DerefArrayReferenceSuffix%]"=="[NOSUFFIX]" set _DerefArrayReferenceSuffix=
if "[%_DerefArrayOutputSuffix%]"=="[]" set "_DerefArrayOutputSuffix=%_DerefArrayReferenceSuffix%"
if "[%_DerefArrayOutputSuffix%]"=="[NOSUFFIX]" set _DerefArrayOutputSuffix=
REM if %_DerefArrayElements.index%==0 echo ref-suffix "%_DerefArrayReferenceSuffix%" out-suffix "%_DerefArrayOutputSuffix%"
REM call call echo %%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%% %_DerefArrayReferenceSuffix%
call call set %_DerefArrayOutputArray%[%_DerefArrayOutput.index%]%_DerefArrayOutputSuffix%=%%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%%
set %_DerefArrayOutputArray%.ubound=%_DerefArrayOutput.index%
set /a _DerefArraySuffix.index+=1
REM echo _DerefArrayReferenceSuffix.ubound is %_DerefArrayReferenceSuffix.ubound%
REM echo if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
REM if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% GoTo :DereferenceArrayToArray-suffix-loop
set /a _DerefArrayOutput.index+=1
set /a _DerefArrayElements.index+=1
if %_DerefArrayElements.index% LEQ %_DerefArrayElementsArray.ubound% GoTo :DereferenceArrayToArray-output-loop
REM Call :ClearVariablesByPrefix _DerefArray
REM set _DerefArray
GoTo :EOF
:: NOSUFFIX in ApplicableSuffixes refers to copying the array element without a suffix
:: if ApplicableSuffixes is empty, then all variable inside array element will be copied
::Usage Call :CopyMultipleArrayElements InputArray InputElementList OutputArray ApplicableSuffixes
:CopyMultipleArrayElements
set "_CopyMultipleArrayElements_InputArray=%~1"
set "_CopyMultipleArrayElements_InputElementList=%~2"
call set /a _CopyMultipleArrayElements_InputElementList.ubound=%_CopyMultipleArrayElements_InputElementList%.ubound
set "_CopyMultipleArrayElements_OutputArray=%~3"
call set _CopyMultipleArrayElements_OutputArray.lbound=%%%_CopyMultipleArrayElements_OutputArray%.lbound%%
call set _CopyMultipleArrayElements_OutputArray.ubound=%%%_CopyMultipleArrayElements_OutputArray%.ubound%%
if "[%_CopyMultipleArrayElements_OutputArray.ubound%]"=="[]" set _CopyMultipleArrayElements_OutputArray.ubound=-1
set "_CopyMultipleArrayElements_ApplicableSuffixes=%~4"
if "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" set /a _CopyMultipleArrayElements_ApplicableSuffixes.ubound=0
if not "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" echo calling :Deconcatenate " " "%_CopyMultipleArrayElements_ApplicableSuffixes%" _CopyMultipleArrayElements_ApplicableSuffixes
if not "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" Call :Deconcatenate " " "%_CopyMultipleArrayElements_ApplicableSuffixes%" _CopyMultipleArrayElements_ApplicableSuffixes
set /a _CopyMultipleArrayElements_InputElementList.index=0
:CopyMultipleArrayElements-loop-InputElementList
set /a _CopyMultipleArrayElements_ApplicableSuffixes.index=0
set /a _CopyMultipleArrayElements_OutputArray.ubound+=1
:CopyMultipleArrayElements-loop-ApplicableSuffixes
call set _CopyMultipleArrayElements_ApplicableSuffixes.current=%%_CopyMultipleArrayElements_ApplicableSuffixes[%_CopyMultipleArrayElements_ApplicableSuffixes.index%]%%
if "[%_CopyMultipleArrayElements_ApplicableSuffixes.current%]"=="[NOSUFFIX]" set "_CopyMultipleArrayElements_ApplicableSuffixes.current="
if "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" (
Call :CopyElementAndSuffix %_CopyMultipleArrayElements_InputArray%[%_CopyMultipleArrayElements_InputElementList.index%] %_CopyMultipleArrayElements_OutputArray%[%_CopyMultipleArrayElements_OutputArray.ubound%]
) else (
call call set %_CopyMultipleArrayElements_OutputArray%[%_CopyMultipleArrayElements_OutputArray.ubound%]%_CopyMultipleArrayElements_ApplicableSuffixes.current%=%%%%%_CopyMultipleArrayElements_InputArray%[%_CopyMultipleArrayElements_InputElementList.index%]%_CopyMultipleArrayElements_ApplicableSuffixes.current%%%%%
)
set /a _CopyMultipleArrayElements_ApplicableSuffixes.index+=1
if %_CopyMultipleArrayElements_ApplicableSuffixes.index% LEQ %_CopyMultipleArrayElements_ApplicableSuffixes.ubound% GoTo :CopyMultipleArrayElements-loop-ApplicableSuffixes
set /a _CopyMultipleArrayElements_InputElementList.index+=1
if %_CopyMultipleArrayElements_InputElementList.index% LEQ %_CopyMultipleArrayElements_InputElementList.ubound% GoTo :CopyMultipleArrayElements-loop-InputElementList
if "[%_CopyMultipleArrayElements_OutputArray.lbound%]"=="[]" set /a %_CopyMultipleArrayElements_OutputArray%.lbound=0
set /a %_CopyMultipleArrayElements_OutputArray%.ubound=%_CopyMultipleArrayElements_OutputArray.ubound%
set /a %_CopyMultipleArrayElements_OutputArray%.count=%_CopyMultipleArrayElements_OutputArray.ubound%-1
Call :ClearVariablesByPrefix _CopyMultipleArrayElements
GoTo :EOF
::Usage Call :AddArrayElement InputElement OutputArray OutputIndex
:AddArrayElement
Call :ClearVariablesByPrefix _AddArrayElement
set "_AddArrayElement_InputElement=%~1"
set "_AddArrayElement_OutputArray=%~2"
set "_AddArrayElement_OutputIndex=%~3"
call set /a "_AddArrayElement_OutputUbound=%%%_AddArrayElement_OutputArray%.ubound%%"
REM Start loop at OutputArray ubound and then work my way back until OutputIndex
call set /a _AddArrayElement_OutputArray.index=%%%_AddArrayElement_OutputArray%.ubound%%
set /a _AddArrayElement_OutputArray.nextindex=%_AddArrayElement_OutputArray.index% + 1
set /a %_AddArrayElement_OutputArray%.ubound+=1
set /a %_AddArrayElement_OutputArray%.count+=1
if "[%_AddArrayElement_OutputIndex%]"=="[]" set /a _AddArrayElement_OutputIndex=%_AddArrayElement_OutputUbound% + 1
if %_AddArrayElement_OutputIndex% GEQ %_AddArrayElement_OutputUbound% GoTo :AddArrayElement-OutputArray-loop-skip
:AddArrayElement-OutputArray-loop
REM Move value to the next index, for every position in the array until OutputIndex
REM echo moving %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]
for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%] 2^>NUL') do (
call set "_token=%%a"
REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo "%%_token:%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]%%=%%b"
call set "%%_token:%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]%%=%%b"
)
set /a _AddArrayElement_OutputArray.index-=1
set /a _AddArrayElement_OutputArray.nextindex=%_AddArrayElement_OutputArray.index% + 1
if %_AddArrayElement_OutputArray.index% GEQ %_AddArrayElement_OutputIndex% GoTo :AddArrayElement-OutputArray-loop
:AddArrayElement-OutputArray-loop-skip
REM Clear the destination index of the OutputArray
REM call echo Clearing %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%] %%%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%].PackageFullName%%
for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%] 2^>NUL') do (
set "%%a="
)
REM Copy InputElement into the destination index of the OutputArray
for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_InputElement% 2^>NUL') do (
call set "_token=%%a"
REM call :FORIF "[%%_token%%]"=="[%%_tokensubst%%]" && echo it was same || echo it was not same
REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo %%_token%%=%%b
REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo "%%_token:%_AddArrayElement_InputElement%=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%]%%=%%b"
call set "%%_token:%_AddArrayElement_InputElement%=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%]%%=%%b"
)
GoTo :EOF
::Usage Call :CopyElementAndSuffix InputElement OutputElement DontClearOutput
:CopyElementAndSuffix
set "_CopyElementAndSuffix_Input=%~1"
set "_CopyElementAndSuffix_Output=%~2"
set "_CopyElementAndSuffix_Directive=%~3"
if not "[%_CopyElementAndSuffix_Directive%]"=="[DontClearOutput]" (
for /F "tokens=1* delims==" %%a in ('set %_CopyElementAndSuffix_Output% 2^>NUL') do ( set "%%a=" )
)
for /F "tokens=1* delims==" %%a in ('set %_CopyElementAndSuffix_Input% 2^>NUL') do (
call set "_token=%%a"
call set "%%_token:%_CopyElementAndSuffix_Input%=%_CopyElementAndSuffix_Output%%%=%%b"
)
GoTo :EOF
::Usage Call :RemoveArrayElement ArrayToRemoveElementFrom ElementNumber
::This function will remove the Nth element from an array, including all suffixes,
::and then pull back all values to fill in the empty index
:RemoveArrayElement
Call :ClearVariablesByPrefix _RemoveArrayElement
set "_RemoveArrayElement_array=%~1"
set "_RemoveArrayElement_index=%~2"
REM call echo Call :IsNumeric %_RemoveArrayElement_index% _RemoveArrayElement_ElementIsNumeric
Call :IsNumeric %_RemoveArrayElement_index% _RemoveArrayElement_ElementIsNumeric
REM echo _RemoveArrayElement_ElementIsNumeric %_RemoveArrayElement_ElementIsNumeric%
set "_RemoveArrayElement_ElementDeleted=false"
REM This for loop deleted the specified array element
for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_index%] 2^>NUL') do (
set "_RemoveArrayElement_ElementDeleted=true"
set "%%a="
)
if not "[%_RemoveArrayElement_ElementIsNumeric%]"=="[true]" GoTo :RemoveArrayElement-end
set /a "_RemoveArrayElement_next_index=%_RemoveArrayElement_index%"
REM If the specified element was numeric, move over the next value to empty index
:RemoveArrayElement-internal-loop
set /a _RemoveArrayElement_next_index+=1
set "_token=" & set "_RemoveArrayElement_ElementCopied=false"
for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_next_index%] 2^>NUL') do (
set "_RemoveArrayElement_ElementCopied=true"
call set "_token=%%a"
call set "%%_token:%_RemoveArrayElement_array%[%_RemoveArrayElement_next_index%]=%_RemoveArrayElement_array%[%_RemoveArrayElement_index%]%%=%%b"
)
REM Repeat for every array element until we find an empty index
if "[%_RemoveArrayElement_ElementCopied%]"=="[true]" set /a "_RemoveArrayElement_index=%_RemoveArrayElement_next_index%" & GoTo :RemoveArrayElement-internal-loop
REM Erase last index which is a duplicate of the second to last one
for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_index%] 2^>NUL') do (
set "%%a="
)
set /a _RemoveArrayElement_index-=1
set /a "%_RemoveArrayElement_array%.ubound-=1" 2>NUL
set /a "%_RemoveArrayElement_array%.count-=1" 2>NUL
:RemoveArrayElement-end
Call :ClearVariablesByPrefix _RemoveArrayElement
GoTo :EOF
REM _RemoveValuesFromArray_ArrayToRemoveFrom.suffix
::Usage Call :RemoveValuesFromArray ArrayToRemoveFrom DeleteList1 DeleteList2
:: DeleteLists consist of an array with a series of values to delete from ArrayToModify
:RemoveValuesFromArray
REM Call :ClearVariablesByPrefix _RemoveValuesFromArray REM this would wipe out _RemoveValuesFromArray_ArrayToRemoveFrom.suffix
set "_RemoveValuesFromArray_ArrayToRemoveFrom=%~1"
call set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.ubound=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%.ubound%%"
:RemoveValuesFromArray-Next-DeleteList-loop
set "_RemoveValuesFromArray_DeleteList=%~2"
Call :IsNumeric %%%_RemoveValuesFromArray_DeleteList%.ubound%% _RemoveValuesFromArray_DeleteList.IsArray
if not "[%_RemoveValuesFromArray_DeleteList.IsArray%]"=="[true]" (
set "_RemoveValuesFromArray_DeleteList.value=%~2"
set /a _RemoveValuesFromArray_DeleteList.ubound=0
set /a _RemoveValuesFromArray_ArrayToRemoveFrom.index=0
GoTo :RemoveValuesFromArray-ArrayToRemoveFrom-loop
)
call set /a _RemoveValuesFromArray_DeleteList.ubound=%%%_RemoveValuesFromArray_DeleteList%.ubound%%
set /a _RemoveValuesFromArray_DeleteList.index=0
:RemoveValuesFromArray-DeleteList-loop
call set "_RemoveValuesFromArray_DeleteList.value=%%%_RemoveValuesFromArray_DeleteList%[%_RemoveValuesFromArray_DeleteList.index%]%%"
set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.index=0"
:RemoveValuesFromArray-ArrayToRemoveFrom-loop
call set "_RemoveValuesFromArray_ArrayToRemoveFrom.value=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%[%_RemoveValuesFromArray_ArrayToRemoveFrom.index%]%_RemoveValuesFromArray_ArrayToRemoveFrom.suffix%%%"
if "[%_RemoveValuesFromArray_ArrayToRemoveFrom.value%]"=="[%_RemoveValuesFromArray_DeleteList.value%]" (
Call :RemoveArrayElement %_RemoveValuesFromArray_ArrayToRemoveFrom% %_RemoveValuesFromArray_ArrayToRemoveFrom.index%
call set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.ubound=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%.ubound%%"
)
set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.index+=1"
if %_RemoveValuesFromArray_ArrayToRemoveFrom.index% LEQ %_RemoveValuesFromArray_ArrayToRemoveFrom.ubound% GoTo :RemoveValuesFromArray-ArrayToRemoveFrom-loop
set /a _RemoveValuesFromArray_DeleteList.index+=1
if %_RemoveValuesFromArray_DeleteList.index% LEQ %_RemoveValuesFromArray_DeleteList.ubound% GoTo :RemoveValuesFromArray-DeleteList-loop
shift
set "_RemoveValuesFromArray_DeleteList.IsArray="
if not "[%~2]"=="[]" GoTo :RemoveValuesFromArray-Next-DeleteList-loop
Call :ClearVariablesByPrefix _RemoveValuesFromArray
GoTo :EOF
:: Set _CopyValuesFromArray_PartialMatch=true if you want to allow partial matches
:: Set _CopyValuesFromArray_SearchSuffix 'the suffix that will be searched into
:: Maybe support for _CopyValuesFromArray_OutputSuffix=NOSUFFIX ?
::Usage Call :CopyValuesFromArray InputArray OutputArray SearchList1 SearchList2 SearchListN
:CopyValuesFromArray
REM @echo on
set "_CopyValuesFromArray_InputArray=%~1"
call set "_CopyValuesFromArray_InputArray.lbound=%%%~1.lbound%%"
call set "_CopyValuesFromArray_InputArray.ubound=%%%~1.ubound%%"
set "_CopyValuesFromArray_OutputArray=%~2"
call set "_CopyValuesFromArray_OutputArray.lbound=%%%~2.lbound%%"
call set "_CopyValuesFromArray_OutputArray.ubound=%%%~2.ubound%%"
if "[%_CopyValuesFromArray_OutputArray.lbound%]"=="[]" set /a "%_CopyValuesFromArray_OutputArray%.lbound=0"
if "[%_CopyValuesFromArray_OutputArray.ubound%]"=="[]" set /a "%_CopyValuesFromArray_OutputArray%.ubound=-1"
if "[%_CopyValuesFromArray_SearchSuffix%]"=="[]" set "_CopyValuesFromArray_SearchSuffix=NOSUFFIX"
if not "[%_CopyValuesFromArray_SearchSuffix%]"=="[]" Call :Deconcatenate " " "%_CopyValuesFromArray_SearchSuffix%" _CopyValuesFromArray_SearchSuffix
set /a _CopyValuesFromArray_SearchList.lbound=0
set /a _CopyValuesFromArray_SearchList.ubound=-1
:CopyValuesFromArray-InputArray-SearchList-loop
set "_CopyValuesFromArray_NewSearchList=%~3"
call set _CopyValuesFromArray_NewSearchList.lbound=%%%_CopyValuesFromArray_NewSearchList%.lbound%%
call set _CopyValuesFromArray_NewSearchList.ubound=%%%_CopyValuesFromArray_NewSearchList%.ubound%%
call :IsNumeric %_CopyValuesFromArray_NewSearchList.lbound% && set "_CopyValuesFromArray_NewSearchList.lbound="
call :IsNumeric %_CopyValuesFromArray_NewSearchList.ubound% && set "_CopyValuesFromArray_NewSearchList.ubound="
if "[%_CopyValuesFromArray_NewSearchList.ubound%]"=="[]" set /a _CopyValuesFromArray_SearchList.ubound+=1
if "[%_CopyValuesFromArray_NewSearchList.ubound%]"=="[]" (
set "_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.ubound%]=%_CopyValuesFromArray_NewSearchList%"
GoTo :CopyValuesFromArray-InputArray-SearchList-subloop-skip
)
set _CopyValuesFromArray_NewSearchList.index=%_CopyValuesFromArray_NewSearchList.lbound%
:CopyValuesFromArray-InputArray-SearchList-subloop
set /a _CopyValuesFromArray_SearchList.ubound+=1
call set "_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.ubound%]=%%%_CopyValuesFromArray_NewSearchList%[%_CopyValuesFromArray_NewSearchList.index%]%%
set /a _CopyValuesFromArray_NewSearchList.index+=1
if %_CopyValuesFromArray_NewSearchList.index% LEQ %_CopyValuesFromArray_NewSearchList.ubound% GoTo :CopyValuesFromArray-InputArray-SearchList-subloop
:CopyValuesFromArray-InputArray-SearchList-subloop-skip
shift
if not "[%~3]"=="[]" GoTo :CopyValuesFromArray-InputArray-SearchList-loop
REM Main loop starts here
REM set _CopyValuesFromArray
set /a _CopyValuesFromArray_InputArray.index=%_CopyValuesFromArray_InputArray.lbound%
:CopyValuesFromArray-InputArray-loop
set /a _CopyValuesFromArray_SearchList.index=%_CopyValuesFromArray_SearchList.lbound%
:CopyValuesFromArray-InputArray-searchterms-loop
REM call echo searchlist call set "_CopyValuesFromArray_SearchList.current=%%_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]%%" _CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]
call set "_CopyValuesFromArray_SearchList.current=%%_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]%%"
set /a _CopyValuesFromArray_SearchSuffix.index=%_CopyValuesFromArray_SearchSuffix.lbound%
:CopyValuesFromArray-InputArray-suffix-loop
REM call echo suffixlist call set "_CopyValuesFromArray_SearchSuffix.current=%%_CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]%%" _CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]
call set "_CopyValuesFromArray_SearchSuffix.current=%%_CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]%%"
if "[%_CopyValuesFromArray_SearchSuffix.current%]"=="[NOSUFFIX]" set "_CopyValuesFromArray_SearchSuffix.current=
call set "_CopyValuesFromArray_InputArray.current=%%%_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%]%_CopyValuesFromArray_SearchSuffix.current%%%"
REM echo InputArray %_CopyValuesFromArray_InputArray.index% SearchList %_CopyValuesFromArray_SearchList.index% SearchSuffix %_CopyValuesFromArray_SearchSuffix.index% compare "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_SearchList.current%]"
if "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_SearchList.current%]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-matchfound
if not "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-nomatch
set "_CopyValuesFromArray_PartialMatch.current="
if "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" call set "_CopyValuesFromArray_PartialMatch.current=%%_CopyValuesFromArray_InputArray.current:%_CopyValuesFromArray_SearchList.current%=%%"
if "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" if not "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_PartialMatch.current%]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-matchfound
GoTo :CopyValuesFromArray-InputArray-suffix-loop-nomatch
:CopyValuesFromArray-InputArray-suffix-loop-matchfound
REM echo match found
REM echo set /a %_CopyValuesFromArray_OutputArray%.ubound+=1
set /a %_CopyValuesFromArray_OutputArray%.ubound+=1
REM echo *_CopyValuesFromArray_OutputArray*.ubound %_CopyValuesFromArray_OutputArray%.ubound
REM call echo ***_CopyValuesFromArray_OutputArray**.ubound** %%%_CopyValuesFromArray_OutputArray%.ubound%%
REM call echo Call :CopyElementAndSuffix %_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%] %_CopyValuesFromArray_OutputArray%[%%%_CopyValuesFromArray_OutputArray%.ubound%%]
Call :CopyElementAndSuffix %_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%] %_CopyValuesFromArray_OutputArray%[%%%_CopyValuesFromArray_OutputArray%.ubound%%]
set /a _CopyValuesFromArray_InputArray.index+=1
if %_CopyValuesFromArray_InputArray.index% LEQ %_CopyValuesFromArray_InputArray.ubound% ( GoTo :CopyValuesFromArray-InputArray-loop ) else ( GoTo :CopyValuesFromArray-end)
:CopyValuesFromArray-InputArray-suffix-loop-nomatch
set /a _CopyValuesFromArray_SearchSuffix.index+=1
if %_CopyValuesFromArray_SearchSuffix.index% LEQ %_CopyValuesFromArray_SearchSuffix.ubound% GoTo :CopyValuesFromArray-InputArray-suffix-loop
set /a _CopyValuesFromArray_SearchList.index+=1
if %_CopyValuesFromArray_SearchList.index% LEQ %_CopyValuesFromArray_SearchList.ubound% GoTo :CopyValuesFromArray-InputArray-searchterms-loop
set /a _CopyValuesFromArray_InputArray.index+=1
if %_CopyValuesFromArray_InputArray.index% LEQ %_CopyValuesFromArray_InputArray.ubound% GoTo :CopyValuesFromArray-InputArray-loop
:CopyValuesFromArray-end
Call :ClearVariablesByPrefix _CopyValuesFromArray
GoTo :EOF
REM Todo, this should check if the element exists, if it doesn't find the real ubound with set
::Usage Call :Ubound InputArray UboundOutput ' make this better, search "set" for the real ubound
:Ubound
set %2=%~1.ubound
GoTo :EOF
::Usage Call :Lbound InputArray LboundOutput' make this better, search "set" for the real ubound
:lbound
set %2=%~1.lbound
GoTo :EOF
REM todo rewrite with for loop on set var[ maybe set var[numeric only] ? what about collections
:: ClearArray InputArray optional start optional end
:ClearArray
set /a index=0
set /a limit=2147483647
if not ["%~2"]==[""] set /a index=%~2
if not ["%~3"]==[""] set /a limit=%~3
if not ["%~1.lbound"]==[""] set %~1.lbound=
if not ["%~1.ubound"]==[""] set %~1.ubound=
if not ["%~1.count"]==[""] set %~1.count=
if not ["%~1.next"]==[""] set %~1.next=
if not ["%~1.previous"]==[""] set %~1.previous=
:ClearArray-internal-loop
if not defined %~1[%index%] GoTo :ClearArray-end
call set %%%~1[%index%]%=
set /a index+=1
if %index% GTR %limit% goto :ClearArray-end
GoTo :ClearArray-internal-loop
:ClearArray-end
set index=
set limit=
GoTo :EOF
REM not tested
:: EchoArray InputArray optional start optional end
:EchoArrayToFile
set /a index=0
set /a limit=2147483647
if not ["%~2"]==[""] set /a index=%~2
if not ["%~3"]==[""] set /a limit=%~3
:EchoArrayToFile-internal-loop
if not defined %1[%index%] GoTo :EOF
call echo %%%1[%index%]%% >> %~2
set /a index+=1
if %index% GTR %limit% goto :EOF
GoTo :EchoArray-internal-loop
REM Incomplete/untested
::Usage Call :EchoCommand "your command here" 'optional skipcount "delimiters" "eol"
:EchoCommand
set forskip=
set delimiters=
set foreol=
if not "[%~2]"=="[]" set forskip="skip=%~2"
if not "[%~3]"=="[]" set fordelimiters="delims=%~3"
if not "[%~4]"=="[]" set foreol="eol=%~4"
FOR /F "usebackq tokens=* %forskip%%foreol%%fordelimiters%" %%a IN (`%~1`) DO echo %%a
Goto :EOF
::Usage Call :ReadString "Your string here"
:ReadString
for /f %i in ("%~1") do echo %%i
GoTo :EOF
::Usage Call :TestFunction 'Will print information about all parameters
:TestFunction
echo tested function first parameter : %1
GoTo :EOF
:FORIF
REM echo just before forif %*
if %* (exit /b 0) ELSE (exit /b 1)
GoTo :EOF
::Usage Call :PrintTime 'just prints the time
:PrintTime
echo %date% %time%
GoTo :EOF
::Usage Call :ClearScreen
:ClearScreen
cls
GoTo :EOF
::Usage SetTitle MyTitle
:SetTitle
title %~1
GoTo :EOF
REM TODO
::Usage SetTitle MyTitle
:SetSubTitle
title %~1
GoTo:EOF
::Usage Call :IsNumeric "%Value%" optional Output
:IsNumeric
if "[%~1]"=="[]" exit /b 0
set "IsNumericInternal=0123456789"
echo.%~1| findstr /r "[^%IsNumericInternal%]" >nul && (
if not "[%2]"=="[]" set %2=false
) || (
if not "[%2]"=="[]" set %2=true
)
GoTo :EOF
REM Call :IsNumeric "%var%" && echo it is not numeric || echo it is numeric
REM echo isnumeric with a number
REM call :isnumeric "1" && echo is was not numeric || ( echo it was numeric & echo also you smell )
REM echo isnumeric with a letter
REM call :isnumeric "a" && echo is was not numeric || ( echo it was numeric & echo also you smell )
REM echo isnumeric with a double quote empty
REM call :isnumeric "" && echo is was not numeric || ( echo it was numeric & echo also you smell )
REM echo isnumeric with no input
REM call :isnumeric && echo is was not numeric || ( echo it was numeric & echo also you smell )
::IsNumeric-END
::Usage Call :HexPrefixStripper InputHex OutputDecimal ' This just strips leading "0x" if present
:HexPrefixStripper
set "_HexPrefixStripper_buffer=%~1"
set "%~2=%~1"
if "[%_HexPrefixStripper_buffer:~0,2%]"=="[0x]" call set "%~2=%_HexPrefixStripper_buffer:~2%"
set "_HexPrefixStripper_buffer="
GoTo :EOF
:: -----------------------------
:: ----- File Manipulation -----
:: -----------------------------
REM INCOMPLETE FUNCTION
REM function to check all argument for certain text (TODO)
REM INCOMPLETE FUNCTION
REM file operation DoesFileExist CreateFolder MoveFolder CopyFile CopyRecursive Copy*.EXT DeleteFile CreateFileFromArray