Advertisement
T3RRYT3RR0R

Arrays 4 batch Define,modify,clear,search or sort

Sep 5th, 2020
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.63 KB | None | 0 0
  1. @ECHO OFF
  2. Set "UseErr=Echo/&Echo/Usage Error - Ensure command extensions and Delayed Expansion are enabled with: &Echo/Setlocal EnableExtensions EnableDelayedExpansion&Echo/ or from the command line:&Echo/CMD /V:On /K&Exit /B 1"
  3. If Not "!Comspec!"=="%Comspec%" (%UseErr%)
  4. (Set "GRPNm="&Set "TAB= "&Set "S_Offset="&Set "mode="&Set "#STDOut="&Set "nGRPNm="&Set "#Order="&Set "Inset="&Set "Usage=Echo/###&Exit /B 1") > Nul 2> Nul
  5. (Set "SwParam="&Set "SwFParam="&Set "#ORP#=0"&Set "#FP#=0"&Set "Inset="&Set "#STDOut=0"&Set "GRPNm="&Set "!GRPNm!="&Set "SubEl="&Set "FlNm=%~n0"& Set "Mode="&Set "FindV=") > Nul 2> Nul
  6. If "%~1"=="" (
  7.     Echo/&Echo/Modes:&Echo/ [Def]!TAB!!TAB!!TAB!Define, modify or clear an array.
  8.     Echo/ [Def]!TAB!!TAB!!TAB!Switches:!TAB![/A:Groupname] [/F:Filepath.ext] [/D] [/O:Index#Arg] [/E:Element Sub value]
  9.     Echo/ [Sort-int]!TAB!!TAB!Sorts array by lowest or highest value using /L or /H switches
  10.     Echo/ [Sort-int]!TAB!!TAB!Switches:!TAB![/A:Groupname] [/N:New Groupname] [/L^|/H] [/D]
  11.     Echo/ [Sort-str]!TAB!!TAB!Sorts an array or text files string values using alphanumerical order of sort: [0-9][a-z]
  12.     Echo/ [Sort-str]!TAB!!TAB!Switches:!TAB![/A:Groupname] [/F:Filepath.ext] [/D]
  13.     Echo/ [Find-searchstring]!TAB!Searches an array for the string value supplied.&Echo/
  14.     %Usage:###=/M:Mode required&Echo/[Def][Sort-int^|str][Find:Value]%
  15.    ) Else Call :GetArgs %*
  16. If Errorlevel 1 Exit /B 1
  17. Call :!Mode! %* 2> Nul || (%Usage:###=Invalid Mode /M:!Mode!&Echo/[Def][Sort-int^|str][Find:Value]%)
  18. Exit /B 0
  19. :str
  20.  Set "Usage=Echo/###&Echo/%~n0 ["/F:filepath.ext" ^| "/A:Array Group Name"] & Exit /B 1"
  21.  If "!GRPNm!"=="" (%Usage:###=/A:Groupname required%) Else Set "#!GRPNm!=0"
  22.  If "!#FP#!"=="1" (
  23.   (For /F "UseBackQ Delims=" %%G in (`Type "!FilePath!" ^| Sort`)Do (
  24.    For %%x in ("!GRPNm![!#%GRPNm%!]") Do (
  25.     Setlocal DisableDelayedExpansion
  26.     Endlocal & Set "%%~x=%%~G"
  27.     If "!#STDOut!"=="1" Echo/%%~x=%%~G
  28.    )
  29.    Set /A "#!GRPNm!+=1"
  30.   )) 2> Nul || (%Usage:###:=Echo/Invalid Filepath:"!FilePath!"%)
  31.   Exit /B 0
  32.  )
  33.  If Not "!#FP#!"=="1" (For /F "Tokens=1,2 Delims==" %%G in ('Set !GRPNm![')Do Echo/%%H)>"%TEMP%\__Sort.txt"
  34.  (For /F "UseBackQ Delims=" %%G in (`Type "%TEMP%\__Sort.txt" ^| Sort`)Do (
  35.    For %%x in ("!GRPNm![!#%GRPNm%!]") Do (
  36.     Setlocal DisableDelayedExpansion
  37.     Endlocal & Set "%%~x=%%~G"
  38.     If "!#STDOut!"=="1" Echo/%%~x=%%~G
  39.    )
  40.     Set /A "#!GRPNm!+=1"
  41.   )
  42.  )
  43.  Del /Q "%TEMP%\__Sort.txt"
  44. Exit /B 0
  45. :Find
  46. Set "Usage=Echo/###&Echo/%~n0 [/M:Find-Searchstring] [/A:Group Name]&Exit /B 1"
  47. If "!GRPNm!"=="" %Usage:###=/A:Groupname Required%
  48. If "!FindV!"=="" %Usage:###=/M:Find-Value Required%
  49.  (For /F "Tokens=1,2 Delims==" %%i in ('Set !GRPNm![') Do (Echo/"%%j"|%__AppDir__%Findstr.exe /LIC:"!findV!" > Nul 2> Nul && (Echo/!FindV! found:&Echo/%%~i=%%~j))) || (%Usage:###=Target Array not Defined%)
  50. Exit /B 0
  51. :Int
  52. Set "Usage=Echo/###&Echo/%~n0 [/M:Sort-Int] [/A:Group Name] [/N:New Group Name] [Sort-Int] [/H^|/L]&Exit /B 1"
  53. If "!GRPNm!"=="" %Usage:###=/A:Groupname required%
  54. If "!nGRPNm!"=="" Set "nGRPNm=!GRPNm!"
  55. If Not "%#Order%"=="" (Call :Sort%#Order% !nGRPNm! #!nGRPNm! !Inset!) Else (%Usage:###=Sort Order Required /H or /L%)
  56. Exit /B 0
  57. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Subroutines for Population of Arrays with numeric values in sorted order.
  58. :sortL <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
  59.  Set "%2=0"
  60.  FOR %%P In (%*) DO If Not "%%P"=="%1" If Not "%%P"=="%2" If Not "%%P"=="" (
  61.   Set "%1[!%2!]=!%%P!"
  62.   Set /A "%2+=1"
  63.  )
  64.  For /L %%a In (1,1,!%2!)Do (
  65.   Set /A "S_Offset=%%a - 1"
  66.   For /L %%b IN (0,1,%%a)Do (
  67.    If not %%b==%%a For %%c in (!S_Offset!)Do (
  68.     IF !%1[%%c]! LEQ !%1[%%b]! (
  69.      Set "tmpV=!%1[%%c]!"
  70.      Set "%1[%%c]=!%1[%%b]!"
  71.      Set "%1[%%b]=!tmpV!"
  72.  ))))
  73.  Set /A %2-=1
  74.  If "!#STDOut!"=="1" For /L %%G in (0 1 !%2!)Do Echo/%1[%%G]=!%1[%%G]!
  75. Exit /B 0
  76. :sortH <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
  77.  Set "%2=0"
  78.  FOR %%P In (%*) DO If Not "%%~P"=="%~1" If Not "%%~P"=="%2" If Not "%%P"=="" (
  79.   Set "%1[!%2!]=!%%~P!"
  80.   Set /A "%2+=1"
  81.  )
  82.  For /L %%a In (1,1,!%2!)Do (
  83.   Set /A "S_Offset=%%a - 1"
  84.   For /L %%b IN (0,1,%%a)Do (
  85.    If not %%b==%%a For %%c in (!S_Offset!)Do (
  86.     If Not !%1[%%c]! LSS !%1[%%b]! (
  87.      Set "tmpV=!%1[%%c]!"
  88.      Set "%1[%%c]=!%1[%%b]!"
  89.      Set "%1[%%b]=!tmpV!"
  90.  ))))
  91.  Set /A %2-=1
  92.  If "!#STDOut!"=="1" For /L %%G in (0 1 !%2!)Do Echo/%1[%%G]=!%1[%%G]!
  93. Exit /B 0
  94. :Def
  95. Set "Usage=Echo/###&Echo/Call %~n0 [/M:Def] [/A:Groupname] ["element0" ~ "element#"] [/F:Filepath.ext] [/E:"Element sub value"]&Echo/Call %~n0 [/M:Def] [/A:Groupname] REM : Clears the Array for the given Group Name&Echo/Call %~n0 [/M:Def] [/A:Groupname] [element] [element] [/O:Index#Arg] REM : Overides Elements from the index supplied&Exit /B 1"
  96.  If "!GRPNm!"=="" %Usage:###=/A:Groupname required%
  97.  If "!#ORP#!"=="1" Echo/!SwParam!|findstr /RX [0-9]* > Nul 2> Nul
  98.  If not "!SwParam!"=="" If Errorlevel 1 (%Usage:###=O:!SwParam! #Arg invalid. Only Integers accepted.%)
  99.  If "!#ORP#!"=="1" Set "#!GRPNm!=0"
  100.  If "!#%GRPNm%!"=="" Set "#!GRPNm!=0"
  101.  If "%#FP#%"=="1" (
  102.   If exist "!FilePath!" (
  103.    For /F "Delims=" %%G in (!FilePath!)Do If Not "%%~G"=="" (
  104.     For %%x in ("!GRPNm![!#%GRPNm%!]")Do (
  105.      Setlocal DisableDelayedExpansion
  106.      If "%#STDOut%"=="1" Echo/%%~x=%%~G
  107.      Endlocal & Set "%%~x=%%G"
  108.     )
  109.     Set /A "#!GRPNm!+=1" > Nul
  110.    )
  111.   ) Else (%Usage:###=/F:!FilePath! Invalid path%)
  112.  )
  113.  If not "!Inset!"=="" (
  114.   For %%G in (!Inset!)Do (
  115.    For %%x in ("%GRPNm%[!#%GRPNm%!]")Do (
  116.     Setlocal DisableDelayedExpansion
  117.     If "%#STDOut%"=="1" Echo/%%~x=%%~G
  118.     Endlocal & Set "%%~x=%%~G"
  119.    )
  120.    If Not "!SubEL!"=="" Set "%%~G=!SubEl!"
  121.    Set /A "#!GRPNm!+=1" > Nul
  122.   )
  123.  ) Else (
  124.   If Not "%#FP#%"=="1" (
  125.    For /F "Tokens=1,2 Delims==" %%I in ('Set %GRPNm%')Do Set "%%~I=" > Nul 2> Nul
  126.    Set "#!GRPNm!=" > Nul 2> Nul
  127.   )
  128.  )
  129. Exit /B 0
  130. :GetArgs
  131.  If "%~1" == "" (
  132.   If "!Mode!"=="int" (For /F "Tokens=1,2 Delims==" %%G in ('Set !GRPNm![')Do Set "Inset=!Inset! %%G") > Nul 2> Nul || (%Usage:###=Usage Error - Target Array is not defined%)
  133.   If "!Mode!"=="str" (For /F "Tokens=1,2 Delims==" %%G in ('Set !GRPNm![')Do Set "Inset=!Inset! %%G") > Nul 2> Nul || (%Usage:###=Usage Error - Target Array is not defined%)
  134.   Exit /B 0
  135.  )
  136.  Set "Param=%~1"
  137.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/M:" > Nul 2> Nul && (
  138.   Set "MODE=!Param:*/M:=!"& Echo/"!Mode!"|"%__AppDir__%findstr.exe" /LIC:"Sort-" > Nul 2> Nul && (Set "MODE=!Mode:*Sort-=!")
  139.   If "!Mode!"=="" Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/M:" > Nul 2> Nul && (Set "MODE=!Param:*/M:=!"& Echo/"!Mode!"|"%__AppDir__%findstr.exe" /LIC:"Find-" > Nul 2> Nul && (If Not "!Mode:Find-=!"=="" (Set "FindV=!Param:*Find-=!"&Set "Mode=Find")Else %Usage:###=/M:Find-Value not Supplied%))
  140.    If "!Param:*/M:=!"=="" (
  141.     Echo/&Echo/Modes:&Echo/ [Def]!TAB!!TAB!Define, modify or clear an array.
  142.     Echo/ [Sort-int]!TAB!Sorts array by lowest or highest value using /L or /H switches
  143.     Echo/ [Sort-str]!TAB!Sorts an array or text files string values using alphanumerical order of sort: [0-9][a-z]
  144.     Echo/ [Find:Value]!TAB!Searches an array for the string value supplied.&Echo/
  145.     %Usage:###=/M:Mode required&Echo/[Def][Sort-int^|str][Find:Value]%
  146.    )
  147.   Shift&Goto :GetArgs
  148.  )
  149.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/H"  > Nul 2> Nul && (Set "#Order=H"&Shift&Goto :GetArgs)
  150.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/L"  > Nul 2> Nul && (Set "#Order=L"&Shift&Goto :GetArgs)
  151.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/D"  > Nul 2> Nul && (Set "#STDOut=1"&Shift&Goto :GetArgs)
  152.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/F:" > Nul 2> Nul && ((If Not "!Param:/F:=!"=="" (Set "#FP#=1"&Set "FilePath=!Param:/F:=!")Else %Usage:###=/F:Filepath.ext not Supplied%)&Shift&Goto :GetArgs)
  153.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/N:" > Nul 2> Nul && (Set "nGRPNm=!Param:*/N:=!"&(If "!Param:*/N:=!"=="" %Usage:###=/N:New Group Name required%)&Shift&Goto :GetArgs)
  154.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/A:" > Nul 2> Nul && (Set "GRPNm=!Param:*/A:=!"&(If "!Param:*/A:=!"=="" %Usage:###=/A:Group Name required%)&Shift&Goto :GetArgs)
  155.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/O:" > Nul 2> Nul && (Set "SwParam=!Param:*/O:=!"&(If Not "!Param:/O:=!"=="" (Set "#ORP#=1")Else %Usage:###=/O:#Arg not Supplied%)&Shift&Goto :GetArgs)
  156.  Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/E:" > Nul 2> Nul && (Set "SubEl=!Param:*/E:=!"&(If "!Param:/S:=!"=="" %Usage:###=/E:Sub Element not Supplied%)&Shift&Goto :GetArgs)
  157.  Set Inset=!Inset! %1
  158.  Shift&Goto :GetArgs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement