Advertisement
Guest User

Untitled

a guest
May 28th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.50 KB | None | 0 0
  1. @echo off
  2. :setup
  3. call :ClearLocalVariables
  4. Call :GetTime FileTime StartDate StartTime
  5. Call :GetCodePage InitialCodepage
  6. Call :SetTitle "Array manipulation DEMO"
  7. set debug=false
  8. set verbose=100
  9. set silent=false
  10. set logfile=%tmp%\%filetime%
  11. if "[debug]"=="[true]" Call :PrintTime
  12. :: Uncomment line below if this scripts needs to run with admin privileges
  13. REM call :IsAdmin || set args=%* && (call :ElevateAndWait & exit /b)
  14.  
  15. :macro
  16. call :SetMacroPrintTime
  17.  
  18. :main
  19.  
  20. cls
  21. %PrintTime%
  22. if "[%debug%]"=="[true]" echo Debug mode is enabled
  23. if "[%silent%]"=="[true]" echo Silent mode is enabled
  24. if not "[%verbose%]"=="[]" echo Verbose level : %verbose%
  25. if not "[%verbose%]"=="[]" if %verbose% GTR 49 echo Verbose level 50 or more
  26.  
  27. call :ArrayManipulation-DEMO
  28. call :CopyValuesFromArray-DEMO
  29. GoTo :END
  30.  
  31.  
  32.  
  33.  
  34. :END
  35. set FileTime=
  36. set StartDate=
  37. set StartTime=
  38. set InitialCodepage=
  39. set debug=
  40. set verbose=
  41. set silent=
  42. set logfile=
  43.  
  44. GoTo :EOF
  45. REM Exit /b %returnvalue%
  46.  
  47. REM Internal Functions
  48.  
  49. :CopyValuesFromArray-DEMO
  50. Call :ClearVariablesByPrefix _CopyValuesFromArray
  51. Call :ClearVariablesByPrefix mytestarray
  52. Call :ClearVariablesByPrefix mytestoutputarray
  53. Call :CreateTestArray mytestarray 4 NOSUFFIX .suffixA .suffixB .suffixC
  54.  
  55. REM echo.
  56. REM echo Inserted new element 2 (998) and 3 (999)
  57. set "NewElement=999"
  58. set "NewElement.suffixA=999A"
  59. set "NewElement.suffixB=999B"
  60. set "NewElement.suffixC=999C"
  61. Call :AddArrayElement NewElement mytestarray 2
  62. set "NewElement=998"
  63. set "NewElement.suffixA=998A"
  64. set "NewElement.suffixB=998B"
  65. set "NewElement.suffixC=998C"
  66. Call :AddArrayElement NewElement mytestarray 2
  67. REM set mytestarray
  68. Call :ClearVariablesByPrefix NewElement
  69.  
  70. REM GoTo :CopyValuesFromArray-DEMO-skip
  71.  
  72. echo.
  73. echo Contents of mytestarray
  74. echo.
  75. set mytestarray
  76.  
  77. REM GoTo :CopyValuesFromArray-DEMO-skip
  78.  
  79. echo.
  80. echo Searching "NOSUFFIX" for all the search terms, there are no matches
  81. echo.
  82. REM set "_CopyValuesFromArray_SearchSuffix=NOSUFFIX"
  83. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  84. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  85. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  86. echo.
  87. echo Contents of mytestoutputarray
  88. echo.
  89. set mytestoutputarray
  90. Call :ClearVariablesByPrefix mytestoutputarray
  91. Call :ClearVariablesByPrefix _CopyValuesFromArray
  92.  
  93.  
  94. REM :CopyValuesFromArray-DEMO-skip
  95. REM GoTo :EOF
  96.  
  97. echo.
  98. echo Searching "NOSUFFIX" for all the search terms, now including 3-.suffixB, but no match
  99. echo because we are only checking in the suffix "NOSUFFIX", in other words mytestarray[x] straight
  100. echo.
  101. REM set "_CopyValuesFromArray_SearchSuffix=NOSUFFIX"
  102. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  103. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  104. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  105. echo.
  106. echo Contents of mytestoutputarray
  107. echo.
  108. set mytestoutputarray
  109. Call :ClearVariablesByPrefix mytestoutputarray
  110. Call :ClearVariablesByPrefix _CopyValuesFromArray
  111.  
  112.  
  113. REM :CopyValuesFromArray-DEMO-skip
  114. REM GoTo :EOF
  115.  
  116. echo.
  117. echo Searching in ".suffixB" for all the search terms, now including 3-.suffixB,
  118. echo there is a match of element 3
  119. echo.
  120. set "_CopyValuesFromArray_SearchSuffix=.suffixB"
  121. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  122. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  123. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  124. echo.
  125. echo Contents of mytestoutputarray
  126. echo.
  127. set mytestoutputarray
  128. Call :ClearVariablesByPrefix mytestoutputarray
  129. Call :ClearVariablesByPrefix _CopyValuesFromArray
  130.  
  131. REM :CopyValuesFromArray-DEMO-skip
  132. REM GoTo :EOF
  133.  
  134. echo.
  135. echo Searching in ".suffixB" for all the search terms, none of the search terms match
  136. echo also, the searchterm word2 is now an array filled with search terms
  137. echo none of these searchterms have a match in the array, there are no results
  138. echo.
  139. set "word2[3]=bla"
  140. set "word2[4]=bli"
  141. set "word2[5]=blo"
  142. set "word2[6]=blou"
  143. set /a word2.lbound=3
  144. set /a word2.ubound=6
  145. set "_CopyValuesFromArray_SearchSuffix=.suffixB"
  146. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  147. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  148. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  149. echo.
  150. echo Contents of mytestoutputarray
  151. echo.
  152. set mytestoutputarray
  153. Call :ClearVariablesByPrefix mytestoutputarray
  154. Call :ClearVariablesByPrefix _CopyValuesFromArray
  155. Call :ClearVariablesByPrefix word2
  156.  
  157.  
  158. REM :CopyValuesFromArray-DEMO-skip
  159. REM GoTo :EOF
  160.  
  161.  
  162. echo.
  163. echo Searching in ".suffixB" for all the search terms, none of the search terms match
  164. echo also, the searchterm word2 is now an array filled with search terms
  165. echo one of the search terms in array "word2" is a match to element 3 "3-.suffixB"
  166. echo.
  167. set "word2[3]=bla"
  168. set "word2[4]=bli"
  169. set "word2[5]=blo"
  170. set "word2[6]=3-.suffixB"
  171. set "word2[7]=blou"
  172. set /a word2.lbound=3
  173. set /a word2.ubound=7
  174. set "_CopyValuesFromArray_SearchSuffix=.suffixB"
  175. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  176. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  177. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  178. echo.
  179. echo Contents of mytestoutputarray
  180. echo.
  181. set mytestoutputarray
  182. Call :ClearVariablesByPrefix mytestoutputarray
  183. Call :ClearVariablesByPrefix _CopyValuesFromArray
  184. Call :ClearVariablesByPrefix word2
  185.  
  186.  
  187. REM :CopyValuesFromArray-DEMO-skip
  188. REM GoTo :EOF
  189.  
  190.  
  191. echo.
  192. echo Searching in ".suffixA" for all the search terms, none of the search terms match
  193. echo also, the searchterm word2 is now an array filled with search terms
  194. echo none of the search terms match in .suffixA there are no results
  195. echo.
  196. set "word2[3]=bla"
  197. set "word2[4]=bli"
  198. set "word2[5]=blo"
  199. set "word2[6]=3-.suffixB"
  200. set "word2[7]=blou"
  201. set /a word2.lbound=3
  202. set /a word2.ubound=7
  203. set "_CopyValuesFromArray_SearchSuffix=.suffixA"
  204. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  205. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  206. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  207. echo.
  208. echo Contents of mytestoutputarray
  209. echo.
  210. set mytestoutputarray
  211. Call :ClearVariablesByPrefix mytestoutputarray
  212. Call :ClearVariablesByPrefix _CopyValuesFromArray
  213. Call :ClearVariablesByPrefix word2
  214.  
  215.  
  216.  
  217. REM :CopyValuesFromArray-DEMO-skip
  218. REM GoTo :EOF
  219.  
  220.  
  221. echo.
  222. echo Searching in ".suffixA" and in ".suffixB" for all the search terms,
  223. echo also, the searchterm word2 is now an array filled with search terms
  224. echo There are two matches, 3rd element in suffixB and 4th element in suffixA
  225. echo one of the match is from the word2 array, the order is a direct search word
  226. echo.
  227. set "word2[3]=bla"
  228. set "word2[4]=bli"
  229. set "word2[5]=blo"
  230. set "word2[6]=FFFFF"
  231. set "word2[7]=blou"
  232. set "word2[8]=ble"
  233. set "word2[9]=4-.suffixA"
  234. set /a word2.lbound=3
  235. set /a word2.ubound=9
  236. set "_CopyValuesFromArray_SearchSuffix=.suffixA .suffixB"
  237. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  238. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  239. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  240. echo.
  241. echo Contents of mytestoutputarray
  242. echo.
  243. set mytestoutputarray
  244. Call :ClearVariablesByPrefix mytestoutputarray
  245. Call :ClearVariablesByPrefix _CopyValuesFromArray
  246. Call :ClearVariablesByPrefix word2
  247.  
  248.  
  249. REM :CopyValuesFromArray-DEMO-skip
  250. REM GoTo :EOF
  251.  
  252.  
  253. echo.
  254. echo Searching in ".suffixA" and in ".suffixB" for all the search terms,
  255. echo also, the searchterm word2 is now an array filled with search terms
  256. echo There are two matches, 3rd element in suffixB and 4th element in suffixA
  257. echo one of the match is from the word2 array, the order is a direct search word
  258. echo also partial matching is enabled and the search term 998 is added to the list
  259. echo in total this matches element 2 4 and 5
  260. echo.
  261. set "word2[3]=bla"
  262. set "word2[4]=bli"
  263. set "word2[5]=blo"
  264. set "word2[6]=FFFFF"
  265. set "word2[7]=blou"
  266. set "word2[8]=998"
  267. set "word2[9]=ble"
  268. set "word2[10]=4-.suffixA"
  269. set /a word2.lbound=3
  270. set /a word2.ubound=10
  271. set "_CopyValuesFromArray_PartialMatch=true"
  272. set "_CopyValuesFromArray_SearchSuffix=.suffixA .suffixB"
  273. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  274. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  275. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB
  276. echo.
  277. echo Contents of mytestoutputarray
  278. echo.
  279. set mytestoutputarray
  280. Call :ClearVariablesByPrefix mytestoutputarray
  281. Call :ClearVariablesByPrefix _CopyValuesFromArray
  282. Call :ClearVariablesByPrefix word2
  283.  
  284.  
  285. REM :CopyValuesFromArray-DEMO-skip
  286. REM GoTo :EOF
  287.  
  288. echo.
  289. echo Searching in ".suffixA" and in ".suffixB" for all the search terms,
  290. echo also, the searchterm word2 is now an array filled with search terms
  291. echo There are two matches, 3rd element in suffixB and 4th element in suffixA
  292. echo one of the match is from the word2 array, the order is a direct search word
  293. echo also partial matching is enabled and the search term 99 is added to the list
  294. echo in total this matches element 2 3 4 and 5
  295. echo.
  296. set "word2[3]=bla"
  297. set "word2[4]=bli"
  298. set "word2[5]=blo"
  299. set "word2[6]=FFFFF"
  300. set "word2[7]=blou"
  301. set "word2[8]=AAAA"
  302. set "word2[9]=ble"
  303. set "word2[10]=4-.suffixA"
  304. set /a word2.lbound=3
  305. set /a word2.ubound=10
  306. set "_CopyValuesFromArray_PartialMatch=true"
  307. set "_CopyValuesFromArray_SearchSuffix=.suffixA .suffixB"
  308. REM set "_CopyValuesFromArray_OutputSuffix=NOSUFFIX .suffixA .suffixB"
  309. echo Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3
  310. Call :CopyValuesFromArray mytestarray mytestoutputarray word1 word2 word3 3-.suffixB 99
  311. echo.
  312. echo Contents of mytestoutputarray
  313. echo.
  314. set mytestoutputarray
  315. Call :ClearVariablesByPrefix mytestoutputarray
  316. Call :ClearVariablesByPrefix _CopyValuesFromArray
  317. Call :ClearVariablesByPrefix word2
  318.  
  319.  
  320. REM :CopyValuesFromArray-DEMO-skip
  321. REM GoTo :EOF
  322.  
  323.  
  324. GoTo :EOF
  325.  
  326. :ArrayManipulation-DEMO
  327.  
  328. Call :ClearVariablesByPrefix mytestarray
  329. Call :ClearVariablesByPrefix NewElement
  330.  
  331. Call :CreateTestArray mytestarray 4 NOSUFFIX .suffixA .suffixB .suffixC
  332. echo.
  333. echo created a test array with 3 suffixes
  334. set mytestarray
  335.  
  336. echo.
  337. echo Inserted new element 2 (98) and 3 (99)
  338. set "NewElement=99"
  339. set "NewElement.suffixA=99A"
  340. set "NewElement.suffixB=99B"
  341. set "NewElement.suffixC=99C"
  342. Call :AddArrayElement NewElement mytestarray 2
  343. set "NewElement=98"
  344. set "NewElement.suffixA=98A"
  345. set "NewElement.suffixB=98B"
  346. set "NewElement.suffixC=98C"
  347. Call :AddArrayElement NewElement mytestarray 2
  348. set mytestarray
  349. Call :ClearVariablesByPrefix NewElement
  350.  
  351. echo.
  352. echo This overwrote element 1 (and we created a backup in backup.mytestarray[1])
  353. set "NewElement.bla=bla"
  354. set "NewElement.bli=bli"
  355. set "NewElement.blo=blo"
  356. Call :CopyElementAndSuffix mytestarray[1] backup.mytestarray[1]
  357. Call :CopyElementAndSuffix NewElement mytestarray[1]
  358. set mytestarray
  359. Call :ClearVariablesByPrefix NewElement
  360.  
  361. echo.
  362. echo Restored from backup
  363. Call :CopyElementAndSuffix backup.mytestarray[1] mytestarray[1]
  364. set mytestarray
  365.  
  366. echo.
  367. echo Deleted element mytestarray[1], leaving the index in the array empty
  368. Call :ClearVariablesByPrefix mytestarray[1]
  369. set mytestarray
  370.  
  371. echo.
  372. echo Restored from backup and deleted the backup
  373. Call :CopyElementAndSuffix backup.mytestarray[1] mytestarray[1]
  374. Call :ClearVariablesByPrefix backup.mytestarray[1]
  375. set mytestarray
  376.  
  377. echo.
  378. echo Deleted element 1 from array, but closed the gap in the array and updated .ubound
  379. echo also saved a backup of the entire array here for future use
  380. Call :CopyElementAndSuffix mytestarray backup.mytestarray
  381. Call :RemoveArrayElement mytestarray 1
  382. set mytestarray
  383.  
  384. echo.
  385. echo Deleted array index containing the text "98B" but only if it's in suffixA, which it isn't
  386. echo so nothing gets deleted
  387. set "_RemoveValuesFromArray_ArrayToRemoveFrom.suffix=.suffixA"
  388. Call :RemoveValuesFromArray mytestarray 98B
  389. set mytestarray
  390.  
  391.  
  392. echo.
  393. echo Deleted array index containing the text "98B" in suffixB, result is element [1] is deleted
  394. set "_RemoveValuesFromArray_ArrayToRemoveFrom.suffix=.suffixB"
  395. Call :RemoveValuesFromArray mytestarray 98B
  396. set mytestarray
  397.  
  398. echo.
  399. echo Delete the texts 2-.suffixA 3-.suffixB 4-.suffixC from .suffixB
  400. echo this deletes only array element at index 3
  401. set "_RemoveValuesFromArray_ArrayToRemoveFrom.suffix=.suffixB"
  402. Call :RemoveValuesFromArray mytestarray 2-.suffixA 3-.suffixB 4-.suffixC
  403. set mytestarray
  404.  
  405. echo.
  406. echo restored the array from previous backup
  407. Call :ClearVariablesByPrefix mytestarray
  408. Call :CopyElementAndSuffix backup.mytestarray mytestarray
  409. set mytestarray
  410.  
  411. echo.
  412. echo As before
  413. echo Delete the texts 2-.suffixA 3-.suffixB 4-.suffixC from .suffixB
  414. echo this deletes only array element at index 5
  415. echo however in this example, the list of of texts to delete was stored in an array
  416. set mydeletelist[0]=2-.suffixA
  417. set mydeletelist[1]=3-.suffixB
  418. set mydeletelist[2]=4-.suffixC
  419. set mydeletelist.ubound=2
  420. set "_RemoveValuesFromArray_ArrayToRemoveFrom.suffix=.suffixB"
  421. Call :RemoveValuesFromArray mytestarray mydeletelist
  422. set mytestarray
  423.  
  424. echo.
  425. echo restored the array from previous backup
  426. Call :ClearVariablesByPrefix mytestarray
  427. Call :CopyElementAndSuffix backup.mytestarray mytestarray
  428. set mytestarray
  429.  
  430.  
  431. echo.
  432. echo Copying entire array elements 3,2 and 5 from mytestarray to mynewtestarray, then showing contents of mynewtestarray
  433. Call :ClearVariablesByPrefix mynewtestarray
  434. set myelementlist[0]=3
  435. set myelementlist[1]=2
  436. set myelementlist[2]=5
  437. set myelementlist.ubound=2
  438. Call :CopyMultipleArrayElements mytestarray myelementlist mynewtestarray
  439. set mynewtestarray
  440. Call :ClearVariablesByPrefix myelementlist
  441. Call :ClearVariablesByPrefix mynewtestarray
  442.  
  443. echo.
  444. echo Copying array elements 3,2 and 5 from mytestarray to mynewtestarray, then showing contents of mynewtestarray
  445. echo however this time we only copy NOSUFFIX and .suffixC
  446. Call :ClearVariablesByPrefix mynewtestarray
  447. set myelementlist[0]=3
  448. set myelementlist[1]=2
  449. set myelementlist[2]=5
  450. set myelementlist.ubound=2
  451. Call :CopyMultipleArrayElements mytestarray myelementlist mynewtestarray "NOSUFFIX .suffixC"
  452. set mynewtestarray
  453. Call :ClearVariablesByPrefix myelementlist
  454. Call :ClearVariablesByPrefix mynewtestarray
  455.  
  456. echo.
  457. echo Copying array elements from mytestarray to mynewtestarray
  458. echo as previous test, elements 3,2 and 5 from mytestarray are copied
  459. echo but this time we take "suffixless value" and .suffixC and we're putting them in .suffixB and NOSUFFIX
  460. set "_DerefArrayReferenceSuffix=NOSUFFIX .suffixC"
  461. set "_DerefArrayOutputSuffix=.suffixB NOSUFFIX"
  462. set myelementlist[0]=3
  463. set myelementlist[1]=2
  464. set myelementlist[2]=5
  465. set myelementlist.ubound=2
  466. Call :DereferenceArrayToArray myelementlist mytestarray mynewtestarray
  467. set mynewtestarray
  468. Call :ClearVariablesByPrefix myelementlist
  469. Call :ClearVariablesByPrefix mynewtestarray
  470.  
  471. Goto :EOF
  472.  
  473.  
  474. ::Usage Call :CreateTestArray ArrayName Count Suffixes
  475. :: Will create array ArrayName from element 0 to Count, with all listed suffixes
  476. :: Each array value will contain the element number followed by the suffix name
  477. :CreateTestArray
  478. set "_CreateTestArray_ArrayName=%~1"
  479. set /a _CreateTestArray_ArrayCount=%~2
  480. if "[%~3]"=="[]" (
  481. set /a _CreateTestArray_suffix.index=0
  482. set /a _CreateTestArray_suffix.ubound=0
  483. set "_CreateTestArray_suffix[0]=NOSUFFIX"
  484. GoTo :CreateTestArray-arguments-to-suffix-loop-skip
  485. )
  486. set /a _CreateTestArray_suffix.index=0
  487. :CreateTestArray-arguments-to-suffix-loop
  488. set "_CreateTestArray_suffix[%_CreateTestArray_suffix.index%]=%~3"
  489. set _CreateTestArray_suffix.ubound=%_CreateTestArray_suffix.index%
  490. set /a _CreateTestArray_suffix.index+=1
  491. shift
  492. if not "[%~3]"=="[]" GoTo :CreateTestArray-arguments-to-suffix-loop
  493. :CreateTestArray-arguments-to-suffix-loop-skip
  494. set /a _CreateTestArray_ArrayIndex=0
  495. :CreateTestArray-loop-start
  496. set /a _CreateTestArray_suffix.index=0
  497. :CreateTestArray-suffix-loop
  498. set "_CreateTestArray_suffix.current="
  499. call set "_CreateTestArray_suffix.current=%%_CreateTestArray_suffix[%_CreateTestArray_suffix.index%]%%"
  500. if "[%_CreateTestArray_suffix.current%]"=="[NOSUFFIX]" set "_CreateTestArray_suffix.current="
  501. set "_CreateTestArray_suffix.current.content="
  502. if not "[%_CreateTestArray_suffix.current%]"=="[]" set "_CreateTestArray_suffix.current.content=-%_CreateTestArray_suffix.current%"
  503. set %_CreateTestArray_ArrayName%[%_CreateTestArray_ArrayIndex%]%_CreateTestArray_suffix.current%=%_CreateTestArray_ArrayIndex%%_CreateTestArray_suffix.current.content%
  504. set /a _CreateTestArray_suffix.index+=1
  505. if %_CreateTestArray_suffix.index% LEQ %_CreateTestArray_suffix.ubound% Goto :CreateTestArray-suffix-loop
  506. set /a _CreateTestArray_ArrayIndex+=1
  507. if %_CreateTestArray_ArrayIndex% LEQ %_CreateTestArray_ArrayCount% Goto :CreateTestArray-loop-start
  508. set /a %_CreateTestArray_ArrayName%.count=%_CreateTestArray_ArrayIndex%
  509. set /a _CreateTestArray_ArrayIndex-=1
  510. set /a %_CreateTestArray_ArrayName%.lbound=0
  511. set /a %_CreateTestArray_ArrayName%.ubound=%_CreateTestArray_ArrayIndex%
  512. Call :ClearVariablesByPrefix _CreateTestArray
  513. GoTo :EOF
  514.  
  515.  
  516. REM DereferenceArrayToArray and CopyMultipleArrayElements are almost identical, should be rewritten as a unique function that does it all
  517.  
  518. REM TODO MAKE VERSION THAT OUTPUTS TO ARRAY OR FILE
  519. REM This function takes elements numbers from ElementsArray
  520. REM Then it retrieves those elements from the ReferenceArray (with suffix, if provided)
  521. REM And outputs them to OutputArray
  522. REM Elements are appended to OutputArray starting from OutputArray.ubound
  523. REM Suffixes are defined with _DerefArrayReferenceSuffix, space separated for multiple elements
  524. REM The suffixes will be used on the output array unless overriden by _DerefArrayOutputSuffix
  525. REM The suffix NOSUFFIX has the special meaning of an empty suffix
  526. ::Usage Call :DereferenceArrayToArray ElementsArray ReferenceArray OutputArray
  527. :DereferenceArrayToArray
  528. if "[%~1]"=="[]" GoTo :EOF
  529. if "[%~2]"=="[]" GoTo :EOF
  530. if "[%~3]"=="[]" GoTo :EOF
  531. set "_DerefArrayElementsArray=%~1" & set "_DerefArrayReferenceArray=%~2" & set "_DerefArrayOutputArray=%~3"
  532. REM call echo call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
  533. call set /a _DerefArrayElementsArray.ubound=%%%_DerefArrayElementsArray%.ubound%%
  534. if not "[%_DerefArrayReferenceSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayReferenceSuffix%" _DerefArrayReferenceSuffix
  535. if not "[%_DerefArrayOutputSuffix%]"=="[]" Call :Deconcatenate " " "%_DerefArrayOutputSuffix%" _DerefArrayOutputSuffix
  536. if "[%_DerefArrayReferenceSuffix.ubound%]"=="[]" set /a _DerefArrayReferenceSuffix.ubound=0
  537. REM Set _DerefArrayOutput's index to OutputArray's .ubound+1, if any, or else set to zero
  538. set _DerefArrayOutput.index=%_DerefArrayOutputArray.ubound%
  539. if not "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index+=1
  540. if "[%_DerefArrayOutput.index%]"=="[]" set /a _DerefArrayOutput.index=0
  541. set /a _DerefArrayElements.index=0
  542. :DereferenceArrayToArray-output-loop
  543. set /a _DerefArraySuffix.index=0
  544. :DereferenceArrayToArray-suffix-loop
  545. REM if %_DerefArrayElements.index% LEQ 0 call echo %_DerefArrayReferenceSuffix[0]% %_DerefArrayReferenceSuffix% %_DerefArraySuffix.index%
  546. call set "_DerefArrayReferenceSuffix=%%_DerefArrayReferenceSuffix[%_DerefArraySuffix.index%]%%"
  547. call set "_DerefArrayOutputSuffix=%%_DerefArrayOutputSuffix[%_DerefArraySuffix.index%]%%"
  548. if "[%_DerefArrayReferenceSuffix%]"=="[NOSUFFIX]" set _DerefArrayReferenceSuffix=
  549. if "[%_DerefArrayOutputSuffix%]"=="[]" set "_DerefArrayOutputSuffix=%_DerefArrayReferenceSuffix%"
  550. if "[%_DerefArrayOutputSuffix%]"=="[NOSUFFIX]" set _DerefArrayOutputSuffix=
  551. REM if %_DerefArrayElements.index%==0 echo ref-suffix "%_DerefArrayReferenceSuffix%" out-suffix "%_DerefArrayOutputSuffix%"
  552. REM call call echo %%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%% %_DerefArrayReferenceSuffix%
  553. call call set %_DerefArrayOutputArray%[%_DerefArrayOutput.index%]%_DerefArrayOutputSuffix%=%%%%%_DerefArrayReferenceArray%[%%%_DerefArrayElementsArray%[%_DerefArrayElements.index%]%%]%_DerefArrayReferenceSuffix%%%%%
  554. set %_DerefArrayOutputArray%.ubound=%_DerefArrayOutput.index%
  555. set /a _DerefArraySuffix.index+=1
  556. REM echo _DerefArrayReferenceSuffix.ubound is %_DerefArrayReferenceSuffix.ubound%
  557. REM echo if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
  558. REM if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% echo doing another suffix !
  559. if %_DerefArraySuffix.index% LEQ %_DerefArrayReferenceSuffix.ubound% GoTo :DereferenceArrayToArray-suffix-loop
  560. set /a _DerefArrayOutput.index+=1
  561. set /a _DerefArrayElements.index+=1
  562. if %_DerefArrayElements.index% LEQ %_DerefArrayElementsArray.ubound% GoTo :DereferenceArrayToArray-output-loop
  563. REM Call :ClearVariablesByPrefix _DerefArray
  564. REM set _DerefArray
  565. GoTo :EOF
  566.  
  567. :: NOSUFFIX in ApplicableSuffixes refers to copying the array element without a suffix
  568. :: if ApplicableSuffixes is empty, then all variable inside array element will be copied
  569. ::Usage Call :CopyMultipleArrayElements InputArray InputElementList OutputArray ApplicableSuffixes
  570. :CopyMultipleArrayElements
  571. set "_CopyMultipleArrayElements_InputArray=%~1"
  572. set "_CopyMultipleArrayElements_InputElementList=%~2"
  573. call set /a _CopyMultipleArrayElements_InputElementList.ubound=%_CopyMultipleArrayElements_InputElementList%.ubound
  574. set "_CopyMultipleArrayElements_OutputArray=%~3"
  575. call set _CopyMultipleArrayElements_OutputArray.lbound=%%%_CopyMultipleArrayElements_OutputArray%.lbound%%
  576. call set _CopyMultipleArrayElements_OutputArray.ubound=%%%_CopyMultipleArrayElements_OutputArray%.ubound%%
  577. if "[%_CopyMultipleArrayElements_OutputArray.ubound%]"=="[]" set _CopyMultipleArrayElements_OutputArray.ubound=-1
  578. set "_CopyMultipleArrayElements_ApplicableSuffixes=%~4"
  579. if "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" set /a _CopyMultipleArrayElements_ApplicableSuffixes.ubound=0
  580. if not "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" echo calling :Deconcatenate " " "%_CopyMultipleArrayElements_ApplicableSuffixes%" _CopyMultipleArrayElements_ApplicableSuffixes
  581. if not "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" Call :Deconcatenate " " "%_CopyMultipleArrayElements_ApplicableSuffixes%" _CopyMultipleArrayElements_ApplicableSuffixes
  582. set /a _CopyMultipleArrayElements_InputElementList.index=0
  583. :CopyMultipleArrayElements-loop-InputElementList
  584. set /a _CopyMultipleArrayElements_ApplicableSuffixes.index=0
  585. set /a _CopyMultipleArrayElements_OutputArray.ubound+=1
  586. :CopyMultipleArrayElements-loop-ApplicableSuffixes
  587. call set _CopyMultipleArrayElements_ApplicableSuffixes.current=%%_CopyMultipleArrayElements_ApplicableSuffixes[%_CopyMultipleArrayElements_ApplicableSuffixes.index%]%%
  588. if "[%_CopyMultipleArrayElements_ApplicableSuffixes.current%]"=="[NOSUFFIX]" set "_CopyMultipleArrayElements_ApplicableSuffixes.current="
  589. if "[%_CopyMultipleArrayElements_ApplicableSuffixes%]"=="[]" (
  590. Call :CopyElementAndSuffix %_CopyMultipleArrayElements_InputArray%[%_CopyMultipleArrayElements_InputElementList.index%] %_CopyMultipleArrayElements_OutputArray%[%_CopyMultipleArrayElements_OutputArray.ubound%]
  591. ) else (
  592. call call set %_CopyMultipleArrayElements_OutputArray%[%_CopyMultipleArrayElements_OutputArray.ubound%]%_CopyMultipleArrayElements_ApplicableSuffixes.current%=%%%%%_CopyMultipleArrayElements_InputArray%[%_CopyMultipleArrayElements_InputElementList.index%]%_CopyMultipleArrayElements_ApplicableSuffixes.current%%%%%
  593. )
  594. set /a _CopyMultipleArrayElements_ApplicableSuffixes.index+=1
  595. if %_CopyMultipleArrayElements_ApplicableSuffixes.index% LEQ %_CopyMultipleArrayElements_ApplicableSuffixes.ubound% GoTo :CopyMultipleArrayElements-loop-ApplicableSuffixes
  596. set /a _CopyMultipleArrayElements_InputElementList.index+=1
  597. if %_CopyMultipleArrayElements_InputElementList.index% LEQ %_CopyMultipleArrayElements_InputElementList.ubound% GoTo :CopyMultipleArrayElements-loop-InputElementList
  598. if "[%_CopyMultipleArrayElements_OutputArray.lbound%]"=="[]" set /a %_CopyMultipleArrayElements_OutputArray%.lbound=0
  599. set /a %_CopyMultipleArrayElements_OutputArray%.ubound=%_CopyMultipleArrayElements_OutputArray.ubound%
  600. set /a %_CopyMultipleArrayElements_OutputArray%.count=%_CopyMultipleArrayElements_OutputArray.ubound%-1
  601. Call :ClearVariablesByPrefix _CopyMultipleArrayElements
  602. GoTo :EOF
  603.  
  604. ::Usage Call :AddArrayElement InputElement OutputArray OutputIndex
  605. :AddArrayElement
  606. Call :ClearVariablesByPrefix _AddArrayElement
  607. set "_AddArrayElement_InputElement=%~1"
  608. set "_AddArrayElement_OutputArray=%~2"
  609. set "_AddArrayElement_OutputIndex=%~3"
  610. call set /a "_AddArrayElement_OutputUbound=%%%_AddArrayElement_OutputArray%.ubound%%"
  611. REM Start loop at OutputArray ubound and then work my way back until OutputIndex
  612. call set /a _AddArrayElement_OutputArray.index=%%%_AddArrayElement_OutputArray%.ubound%%
  613. set /a _AddArrayElement_OutputArray.nextindex=%_AddArrayElement_OutputArray.index% + 1
  614. set /a %_AddArrayElement_OutputArray%.ubound+=1
  615. set /a %_AddArrayElement_OutputArray%.count+=1
  616. if "[%_AddArrayElement_OutputIndex%]"=="[]" set /a _AddArrayElement_OutputIndex=%_AddArrayElement_OutputUbound% + 1
  617. if %_AddArrayElement_OutputIndex% GEQ %_AddArrayElement_OutputUbound% GoTo :AddArrayElement-OutputArray-loop-skip
  618. :AddArrayElement-OutputArray-loop
  619. REM Move value to the next index, for every position in the array until OutputIndex
  620. REM echo moving %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]
  621. for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%] 2^>NUL') do (
  622. call set "_token=%%a"
  623. REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo "%%_token:%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]%%=%%b"
  624. call set "%%_token:%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.index%]=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputArray.nextindex%]%%=%%b"
  625. )
  626. set /a _AddArrayElement_OutputArray.index-=1
  627. set /a _AddArrayElement_OutputArray.nextindex=%_AddArrayElement_OutputArray.index% + 1
  628. if %_AddArrayElement_OutputArray.index% GEQ %_AddArrayElement_OutputIndex% GoTo :AddArrayElement-OutputArray-loop
  629. :AddArrayElement-OutputArray-loop-skip
  630. REM Clear the destination index of the OutputArray
  631. REM call echo Clearing %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%] %%%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%].PackageFullName%%
  632. for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%] 2^>NUL') do (
  633. set "%%a="
  634. )
  635. REM Copy InputElement into the destination index of the OutputArray
  636. for /F "tokens=1* delims==" %%a in ('set %_AddArrayElement_InputElement% 2^>NUL') do (
  637. call set "_token=%%a"
  638. REM call :FORIF "[%%_token%%]"=="[%%_tokensubst%%]" && echo it was same || echo it was not same
  639. REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo %%_token%%=%%b
  640. REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" || call echo "%%_token:%_AddArrayElement_InputElement%=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%]%%=%%b"
  641. call set "%%_token:%_AddArrayElement_InputElement%=%_AddArrayElement_OutputArray%[%_AddArrayElement_OutputIndex%]%%=%%b"
  642. )
  643. GoTo :EOF
  644.  
  645.  
  646. :: Set _CopyValuesFromArray_PartialMatch=true if you want to allow partial matches
  647. :: Set _CopyValuesFromArray_SearchSuffix 'the suffix that will be searched into
  648. :: Maybe support for _CopyValuesFromArray_OutputSuffix=NOSUFFIX ?
  649. ::Usage Call :CopyValuesFromArray InputArray OutputArray SearchList1 SearchList2 SearchListN
  650. :CopyValuesFromArray
  651. REM @echo on
  652. set "_CopyValuesFromArray_InputArray=%~1"
  653. call set "_CopyValuesFromArray_InputArray.lbound=%%%~1.lbound%%"
  654. call set "_CopyValuesFromArray_InputArray.ubound=%%%~1.ubound%%"
  655. set "_CopyValuesFromArray_OutputArray=%~2"
  656. call set "_CopyValuesFromArray_OutputArray.lbound=%%%~2.lbound%%"
  657. call set "_CopyValuesFromArray_OutputArray.ubound=%%%~2.ubound%%"
  658. if "[%_CopyValuesFromArray_OutputArray.lbound%]"=="[]" set /a "%_CopyValuesFromArray_OutputArray%.lbound=0"
  659. if "[%_CopyValuesFromArray_OutputArray.ubound%]"=="[]" set /a "%_CopyValuesFromArray_OutputArray%.ubound=-1"
  660. if "[%_CopyValuesFromArray_SearchSuffix%]"=="[]" set "_CopyValuesFromArray_SearchSuffix=NOSUFFIX"
  661. if not "[%_CopyValuesFromArray_SearchSuffix%]"=="[]" Call :Deconcatenate " " "%_CopyValuesFromArray_SearchSuffix%" _CopyValuesFromArray_SearchSuffix
  662. set /a _CopyValuesFromArray_SearchList.lbound=0
  663. set /a _CopyValuesFromArray_SearchList.ubound=-1
  664. :CopyValuesFromArray-InputArray-SearchList-loop
  665. set "_CopyValuesFromArray_NewSearchList=%~3"
  666. call set _CopyValuesFromArray_NewSearchList.lbound=%%%_CopyValuesFromArray_NewSearchList%.lbound%%
  667. call set _CopyValuesFromArray_NewSearchList.ubound=%%%_CopyValuesFromArray_NewSearchList%.ubound%%
  668. call :IsNumeric %_CopyValuesFromArray_NewSearchList.lbound% && set "_CopyValuesFromArray_NewSearchList.lbound="
  669. call :IsNumeric %_CopyValuesFromArray_NewSearchList.ubound% && set "_CopyValuesFromArray_NewSearchList.ubound="
  670. if "[%_CopyValuesFromArray_NewSearchList.ubound%]"=="[]" set /a _CopyValuesFromArray_SearchList.ubound+=1
  671. if "[%_CopyValuesFromArray_NewSearchList.ubound%]"=="[]" (
  672. set "_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.ubound%]=%_CopyValuesFromArray_NewSearchList%"
  673. GoTo :CopyValuesFromArray-InputArray-SearchList-subloop-skip
  674. )
  675. set _CopyValuesFromArray_NewSearchList.index=%_CopyValuesFromArray_NewSearchList.lbound%
  676. :CopyValuesFromArray-InputArray-SearchList-subloop
  677. set /a _CopyValuesFromArray_SearchList.ubound+=1
  678. call set "_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.ubound%]=%%%_CopyValuesFromArray_NewSearchList%[%_CopyValuesFromArray_NewSearchList.index%]%%
  679. set /a _CopyValuesFromArray_NewSearchList.index+=1
  680. if %_CopyValuesFromArray_NewSearchList.index% LEQ %_CopyValuesFromArray_NewSearchList.ubound% GoTo :CopyValuesFromArray-InputArray-SearchList-subloop
  681. :CopyValuesFromArray-InputArray-SearchList-subloop-skip
  682. shift
  683. if not "[%~3]"=="[]" GoTo :CopyValuesFromArray-InputArray-SearchList-loop
  684. REM Main loop starts here
  685. REM set _CopyValuesFromArray
  686. set /a _CopyValuesFromArray_InputArray.index=%_CopyValuesFromArray_InputArray.lbound%
  687. :CopyValuesFromArray-InputArray-loop
  688. set /a _CopyValuesFromArray_SearchList.index=%_CopyValuesFromArray_SearchList.lbound%
  689. :CopyValuesFromArray-InputArray-searchterms-loop
  690. REM call echo searchlist call set "_CopyValuesFromArray_SearchList.current=%%_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]%%" _CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]
  691. call set "_CopyValuesFromArray_SearchList.current=%%_CopyValuesFromArray_SearchList[%_CopyValuesFromArray_SearchList.index%]%%"
  692. set /a _CopyValuesFromArray_SearchSuffix.index=%_CopyValuesFromArray_SearchSuffix.lbound%
  693. :CopyValuesFromArray-InputArray-suffix-loop
  694. REM call echo suffixlist call set "_CopyValuesFromArray_SearchSuffix.current=%%_CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]%%" _CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]
  695. call set "_CopyValuesFromArray_SearchSuffix.current=%%_CopyValuesFromArray_SearchSuffix[%_CopyValuesFromArray_SearchSuffix.index%]%%"
  696. if "[%_CopyValuesFromArray_SearchSuffix.current%]"=="[NOSUFFIX]" set "_CopyValuesFromArray_SearchSuffix.current=
  697. call set "_CopyValuesFromArray_InputArray.current=%%%_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%]%_CopyValuesFromArray_SearchSuffix.current%%%"
  698. REM echo InputArray %_CopyValuesFromArray_InputArray.index% SearchList %_CopyValuesFromArray_SearchList.index% SearchSuffix %_CopyValuesFromArray_SearchSuffix.index% compare "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_SearchList.current%]"
  699. if "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_SearchList.current%]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-matchfound
  700. if not "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-nomatch
  701. set "_CopyValuesFromArray_PartialMatch.current="
  702. if "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" call set "_CopyValuesFromArray_PartialMatch.current=%%_CopyValuesFromArray_InputArray.current:%_CopyValuesFromArray_SearchList.current%=%%"
  703. if "[%_CopyValuesFromArray_PartialMatch%]"=="[true]" if not "[%_CopyValuesFromArray_InputArray.current%]"=="[%_CopyValuesFromArray_PartialMatch.current%]" GoTo :CopyValuesFromArray-InputArray-suffix-loop-matchfound
  704. GoTo :CopyValuesFromArray-InputArray-suffix-loop-nomatch
  705. :CopyValuesFromArray-InputArray-suffix-loop-matchfound
  706. REM echo match found
  707. REM echo set /a %_CopyValuesFromArray_OutputArray%.ubound+=1
  708. set /a %_CopyValuesFromArray_OutputArray%.ubound+=1
  709. REM echo *_CopyValuesFromArray_OutputArray*.ubound %_CopyValuesFromArray_OutputArray%.ubound
  710. REM call echo ***_CopyValuesFromArray_OutputArray**.ubound** %%%_CopyValuesFromArray_OutputArray%.ubound%%
  711. REM call echo Call :CopyElementAndSuffix %_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%] %_CopyValuesFromArray_OutputArray%[%%%_CopyValuesFromArray_OutputArray%.ubound%%]
  712. Call :CopyElementAndSuffix %_CopyValuesFromArray_InputArray%[%_CopyValuesFromArray_InputArray.index%] %_CopyValuesFromArray_OutputArray%[%%%_CopyValuesFromArray_OutputArray%.ubound%%]
  713. set /a _CopyValuesFromArray_InputArray.index+=1
  714. if %_CopyValuesFromArray_InputArray.index% LEQ %_CopyValuesFromArray_InputArray.ubound% ( GoTo :CopyValuesFromArray-InputArray-loop ) else ( GoTo :CopyValuesFromArray-end)
  715. :CopyValuesFromArray-InputArray-suffix-loop-nomatch
  716.  
  717. set /a _CopyValuesFromArray_SearchSuffix.index+=1
  718. if %_CopyValuesFromArray_SearchSuffix.index% LEQ %_CopyValuesFromArray_SearchSuffix.ubound% GoTo :CopyValuesFromArray-InputArray-suffix-loop
  719. set /a _CopyValuesFromArray_SearchList.index+=1
  720. if %_CopyValuesFromArray_SearchList.index% LEQ %_CopyValuesFromArray_SearchList.ubound% GoTo :CopyValuesFromArray-InputArray-searchterms-loop
  721. set /a _CopyValuesFromArray_InputArray.index+=1
  722. if %_CopyValuesFromArray_InputArray.index% LEQ %_CopyValuesFromArray_InputArray.ubound% GoTo :CopyValuesFromArray-InputArray-loop
  723. :CopyValuesFromArray-end
  724. Call :ClearVariablesByPrefix _CopyValuesFromArray
  725. GoTo :EOF
  726.  
  727. REM old version, would only handle one suffix at a time, could not insert in the middle of an array, I think
  728. :: Set _AddArrayElementSuffix to insert text after array name
  729. :: Example Call :AddArrayElement mytestvalue1 mytestvalue2 mytestvalue3 mytestvalue4 mytestvalue5 myval
  730. :: Example set _AddArrayElementSuffix=.subvalue NOTE You need to roll back array ubound
  731. :: Example set /a myval.ubound=50
  732. :: Example Call :AddArrayElement mytestvalue1 mytestvalue2 mytestvalue3 mytestvalue4 mytestvalue5 myval
  733. ::Usage Call :AddArrayElement AddedValue1 AddedValue2 ... AddedValueN OutputArray
  734. REM :AddArrayElement
  735. REM if "[%~1]"=="[]" GoTo :EOF
  736. REM if "[%~2]"=="[]" GoTo :EOF
  737. REM set /a _AddArrayElement.count=0
  738. REM set /a _AddArrayElement.lbound=0
  739. REM set /a _AddArrayElement.ubound=0
  740. REM set /a _AddArrayElement.index=0
  741. REM :AddArrayElement-internal-loop-collect
  742. REM set "_AddArrayElement[%_AddArrayElement.index%]=%~1"
  743. REM set /a _AddArrayElement.ubound=%_AddArrayElement.index%
  744. REM set /a _AddArrayElement.count+=1
  745. REM set /a _AddArrayElement.index+=1
  746. REM shift
  747. REM if not "[%~2]"=="[]" GoTo :AddArrayElement-internal-loop-collect
  748. REM set /a _AddArrayElement.index=0
  749. REM :AddArrayElement-internal-loop-store
  750. REM set /a %~1.ubound+=1
  751. REM call set "%~1[%%%~1.ubound%%]%_AddArrayElementSuffix%=%%_AddArrayElement[%_AddArrayElement.index%]%%"
  752. REM set /a _AddArrayElement.index+=1
  753. REM if %_AddArrayElement.index% LEQ %_AddArrayElement.ubound% GoTo :AddArrayElement-internal-loop-store
  754. REM Call :ClearVariablesByPrefix _AddArrayElement
  755. REM if not "[%_AddArrayElementSuffix%]"=="[]" set "_AddArrayElementSuffix="
  756. REM GoTo :EOF
  757.  
  758. REM Example usage inside a for loop
  759. REM call :FORIF "[%%_token%%]"=="[%%_token:.PackageFullName=%%]" && echo it was same || echo it was not
  760. ::Usage Call :FORIF your IF statement
  761. :FORIF
  762. REM echo just before forif %*
  763. if %* (exit /b 0) ELSE (exit /b 1)
  764. GoTo :EOF
  765.  
  766.  
  767.  
  768. ::Usage Call :CopyElementAndSuffix InputElement OutputElement DontClearOutput
  769. :CopyElementAndSuffix
  770. set "_CopyElementAndSuffix_Input=%~1"
  771. set "_CopyElementAndSuffix_Output=%~2"
  772. set "_CopyElementAndSuffix_Directive=%~3"
  773. if not "[%_CopyElementAndSuffix_Directive%]"=="[DontClearOutput]" (
  774. for /F "tokens=1* delims==" %%a in ('set %_CopyElementAndSuffix_Output% 2^>NUL') do ( set "%%a=" )
  775. )
  776. for /F "tokens=1* delims==" %%a in ('set %_CopyElementAndSuffix_Input% 2^>NUL') do (
  777. call set "_token=%%a"
  778. call set "%%_token:%_CopyElementAndSuffix_Input%=%_CopyElementAndSuffix_Output%%%=%%b"
  779. )
  780. GoTo :EOF
  781.  
  782. ::Usage Call :RemoveArrayElement ArrayToRemoveElementFrom ElementNumber
  783. ::This function will remove the Nth element from an array, including all suffixes,
  784. ::and then pull back all values to fill in the empty index
  785. :RemoveArrayElement
  786. Call :ClearVariablesByPrefix _RemoveArrayElement
  787. set "_RemoveArrayElement_array=%~1"
  788. set "_RemoveArrayElement_index=%~2"
  789. REM call echo Call :IsNumeric %_RemoveArrayElement_index% _RemoveArrayElement_ElementIsNumeric
  790. Call :IsNumeric %_RemoveArrayElement_index% _RemoveArrayElement_ElementIsNumeric
  791. REM echo _RemoveArrayElement_ElementIsNumeric %_RemoveArrayElement_ElementIsNumeric%
  792. set "_RemoveArrayElement_ElementDeleted=false"
  793. REM This for loop deleted the specified array element
  794. for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_index%] 2^>NUL') do (
  795. set "_RemoveArrayElement_ElementDeleted=true"
  796. set "%%a="
  797. )
  798. if not "[%_RemoveArrayElement_ElementIsNumeric%]"=="[true]" GoTo :RemoveArrayElement-end
  799. set /a "_RemoveArrayElement_next_index=%_RemoveArrayElement_index%"
  800. REM If the specified element was numeric, move over the next value to empty index
  801. :RemoveArrayElement-internal-loop
  802. set /a _RemoveArrayElement_next_index+=1
  803. set "_token=" & set "_RemoveArrayElement_ElementCopied=false"
  804. for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_next_index%] 2^>NUL') do (
  805. set "_RemoveArrayElement_ElementCopied=true"
  806. call set "_token=%%a"
  807. call set "%%_token:%_RemoveArrayElement_array%[%_RemoveArrayElement_next_index%]=%_RemoveArrayElement_array%[%_RemoveArrayElement_index%]%%=%%b"
  808. )
  809. REM Repeat for every array element until we find an empty index
  810. if "[%_RemoveArrayElement_ElementCopied%]"=="[true]" set /a "_RemoveArrayElement_index=%_RemoveArrayElement_next_index%" & GoTo :RemoveArrayElement-internal-loop
  811. REM Erase last index which is a duplicate of the second to last one
  812. for /F "tokens=1* delims==" %%a in ('set %_RemoveArrayElement_array%[%_RemoveArrayElement_index%] 2^>NUL') do (
  813. set "%%a="
  814. )
  815. set /a _RemoveArrayElement_index-=1
  816. set /a "%_RemoveArrayElement_array%.ubound-=1" 2>NUL
  817. set /a "%_RemoveArrayElement_array%.count-=1" 2>NUL
  818. :RemoveArrayElement-end
  819. Call :ClearVariablesByPrefix _RemoveArrayElement
  820. GoTo :EOF
  821.  
  822. REM _RemoveValuesFromArray_ArrayToRemoveFrom.suffix
  823. REM It should be possible to specify the search suffix for ArrayToModify
  824. REM can we match only array element numbers ? Or any content ?
  825. ::Usage Call :RemoveValuesFromArray ArrayToRemoveFrom DeleteList1 DeleteList2
  826. :: DeleteLists consist of an array with a series of values to delete from ArrayToModify
  827. :RemoveValuesFromArray
  828. REM Call :ClearVariablesByPrefix _RemoveValuesFromArray REM this would wipe out _RemoveValuesFromArray_ArrayToRemoveFrom.suffix
  829. set "_RemoveValuesFromArray_ArrayToRemoveFrom=%~1"
  830. call set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.ubound=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%.ubound%%"
  831. :RemoveValuesFromArray-Next-DeleteList-loop
  832. set "_RemoveValuesFromArray_DeleteList=%~2"
  833. Call :IsNumeric %%%_RemoveValuesFromArray_DeleteList%.ubound%% _RemoveValuesFromArray_DeleteList.IsArray
  834. if not "[%_RemoveValuesFromArray_DeleteList.IsArray%]"=="[true]" (
  835. set "_RemoveValuesFromArray_DeleteList.value=%~2"
  836. set /a _RemoveValuesFromArray_DeleteList.ubound=0
  837. set /a _RemoveValuesFromArray_ArrayToRemoveFrom.index=0
  838. GoTo :RemoveValuesFromArray-ArrayToRemoveFrom-loop
  839. )
  840. call set /a _RemoveValuesFromArray_DeleteList.ubound=%%%_RemoveValuesFromArray_DeleteList%.ubound%%
  841. set /a _RemoveValuesFromArray_DeleteList.index=0
  842. :RemoveValuesFromArray-DeleteList-loop
  843. call set "_RemoveValuesFromArray_DeleteList.value=%%%_RemoveValuesFromArray_DeleteList%[%_RemoveValuesFromArray_DeleteList.index%]%%"
  844. set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.index=0"
  845. :RemoveValuesFromArray-ArrayToRemoveFrom-loop
  846. call set "_RemoveValuesFromArray_ArrayToRemoveFrom.value=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%[%_RemoveValuesFromArray_ArrayToRemoveFrom.index%]%_RemoveValuesFromArray_ArrayToRemoveFrom.suffix%%%"
  847. if "[%_RemoveValuesFromArray_ArrayToRemoveFrom.value%]"=="[%_RemoveValuesFromArray_DeleteList.value%]" (
  848. Call :RemoveArrayElement %_RemoveValuesFromArray_ArrayToRemoveFrom% %_RemoveValuesFromArray_ArrayToRemoveFrom.index%
  849. call set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.ubound=%%%_RemoveValuesFromArray_ArrayToRemoveFrom%.ubound%%"
  850. )
  851. set /a "_RemoveValuesFromArray_ArrayToRemoveFrom.index+=1"
  852. if %_RemoveValuesFromArray_ArrayToRemoveFrom.index% LEQ %_RemoveValuesFromArray_ArrayToRemoveFrom.ubound% GoTo :RemoveValuesFromArray-ArrayToRemoveFrom-loop
  853. set /a _RemoveValuesFromArray_DeleteList.index+=1
  854. if %_RemoveValuesFromArray_DeleteList.index% LEQ %_RemoveValuesFromArray_DeleteList.ubound% GoTo :RemoveValuesFromArray-DeleteList-loop
  855. shift
  856. set "_RemoveValuesFromArray_DeleteList.IsArray="
  857. if not "[%~2]"=="[]" GoTo :RemoveValuesFromArray-Next-DeleteList-loop
  858. Call :ClearVariablesByPrefix _RemoveValuesFromArray
  859. GoTo :EOF
  860.  
  861. REM Macro definitions
  862. :SetMacroPrintTime
  863. set PrintTime=& Call :PrintTime
  864. GoTo :EOF
  865.  
  866. REM Function library
  867.  
  868. REM example function structure, with usage and signature
  869. :FunctionNameHELP
  870. echo Name and usage
  871. echo dependencies
  872. echo Date and URL
  873. echo by YourName
  874. echo examples
  875. echo examples
  876. echo examples
  877. echo credit
  878. :GoTo :EOF
  879. :FunctionName
  880. ::End Functionname SHA256:1234567890 SignDate:
  881.  
  882. ::Usage Call SetTitle MyTitle
  883. :SetTitle
  884. title %*
  885. GoTo:EOF
  886.  
  887. ::Usage Call :GetCodePage ReturnValue
  888. :GetCodePage
  889. for /f "tokens=1,2 delims=:" %%a in ('chcp') do set %1=%%b
  890. call set %1=%%%1:~1%%
  891. GoTo :EOF
  892.  
  893. ::Usage Call :SetCodePage NewCodePage
  894. :SetCodePage
  895. chcp %~1
  896. GoTo :EOF
  897.  
  898. ::Usage Call :PrintTime
  899. :PrintTime
  900. echo %date% %time%
  901. GoTo :EOF
  902.  
  903. ::Usage Call :GetTime
  904. :GetTime
  905. set _GetDate=%date% & set _GetTime=%time%
  906. set _GetTime=%_GetTime: 0=00%
  907. set _GetTime=%_GetTime:~0,2%h%_GetTime:~3,2%m%_GetTime:~6,2%s%_GetTime:~9,2%
  908. set "%~1=%_GetDate% %_GetTime%" & set "%~2=%_GetDate%" & set "%~3=%_GetTime%"
  909. set "_GetDate=" & set "_GetTime="
  910. GoTo :EOF
  911.  
  912. :: Usage Call :ClearLocalVariables
  913. :ClearLocalVariables
  914. for /f "tokens=1,2 delims==" %%a in ('set _ 2^>nul') do set %%a=
  915. GoTo :EOF
  916.  
  917. :: Usage Call :ClearVariablesByPrefix myPrefix
  918. :ClearVariablesByPrefix
  919. for /f "tokens=1,2 delims==" %%a in ('set %~1 2^>nul') do set %%a=
  920. GoTo :EOF
  921.  
  922. ::Usage Call :IsNumeric Value optional Output
  923. :IsNumeric
  924. set "IsNumericInternal=0123456789"
  925. echo.%1| findstr /r "[^%IsNumericInternal%]" >nul && (
  926. if not "[%2]"=="[]" set %2=false
  927. ) || (
  928. if not "[%2]"=="[]" set %2=true
  929. )
  930. GoTo :EOF
  931. REM Call :IsNumeric %var% && echo it is not numeric || echo it is numeric
  932.  
  933. ::Usage Call :Deconcatenate Delimiter Input Output 'Returns array starting at [0], see .lbound and .ubound
  934. :deconcatenate
  935. set "_delimiter=%~1" & set /a _delimiter.len=0
  936. set "_input=%~2" & set /a _input.len=0
  937. call :len _delimiter _delimiter.len
  938. call :len _input _input.len
  939. set /a _cursor=0 & set /a _token=0 & set "_char=" & set "_buffer="
  940. set /a %3.lbound=%_cursor%
  941. REM if "[%debug%]"=="[true]" echo input [%_input%] len %_input.len% delimiter [%_delimiter%] len %_delimiter.len%
  942. :deconcatenate_input_loop
  943. REM if "[%debug%]"=="[true]" pause
  944. call set _char=%%_input:~%_cursor%,1%%
  945. REM if "[%debug%]"=="[true]" echo char %_char%
  946. set /a _cursor+=1
  947. set /a _cursor2=0 & set "_IsDelimiter=false"
  948. :deconcatenate_delimiter_loop
  949. call set _charDelimiter=%%_delimiter:~%_cursor2%,1%%
  950. if "[%_char%]"=="[%_charDelimiter%]" set "_IsDelimiter=true"
  951. if not "[%_IsDelimiter%]"=="[true]" set "_buffer=%_buffer%%_char%"
  952. REM if "[%debug%]"=="[true]" echo _charDelimiter [%_charDelimiter%] _IsDelimiter [%_IsDelimiter%] _cursor %_cursor% _cursor2 %_cursor2% _buffer %_buffer%
  953. set /a _cursor2+=1
  954. if %_cursor2% LSS %_delimiter.len% goto :deconcatenate_delimiter_loop
  955. REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" echo writing set %~3[%_token%] = %_buffer%
  956. if "[%_IsDelimiter%]"=="[true]" call set "%~3[%%_token%%]=%_buffer%" & set "_buffer=" & set /a _token+=1
  957. REM if "[%debug%]"=="[true]" if "[%_IsDelimiter%]"=="[true]" pause
  958. if %_cursor% LSS %_input.len% goto :deconcatenate_input_loop
  959. if %_cursor% EQU %_input.len% call set "%~3[%%_token%%]=%_buffer%" & set "_buffer=" & set /a %3.ubound=%_token%
  960. GoTo :EOF
  961.  
  962. :len <resultVar> <stringVar>
  963. (
  964. setlocal EnableDelayedExpansion
  965. (set^ tmp=!%~1!)
  966. if defined tmp (
  967. set "len=1"
  968. for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  969. if "!tmp:~%%P,1!" NEQ "" (
  970. set /a "len+=%%P"
  971. set "tmp=!tmp:~%%P!"
  972. )
  973. )
  974. ) ELSE (
  975. set len=0
  976. )
  977. )
  978. (
  979. endlocal
  980. set "%~2=%len%"
  981. exit /b
  982. )
  983. REM https://stackoverflow.com/a/5841587
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement