Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- 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"
- If Not "!Comspec!"=="%Comspec%" (%UseErr%)
- (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
- (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
- If "%~1"=="" (
- Echo/&Echo/Modes:&Echo/ [Def]!TAB!!TAB!!TAB!Define, modify or clear an array.
- Echo/ [Def]!TAB!!TAB!!TAB!Switches:!TAB![/A:Groupname] [/F:Filepath.ext] [/D] [/O:Index#Arg] [/E:Element Sub value]
- Echo/ [Sort-int]!TAB!!TAB!Sorts array by lowest or highest value using /L or /H switches
- Echo/ [Sort-int]!TAB!!TAB!Switches:!TAB![/A:Groupname] [/N:New Groupname] [/L^|/H] [/D]
- Echo/ [Sort-str]!TAB!!TAB!Sorts an array or text files string values using alphanumerical order of sort: [0-9][a-z]
- Echo/ [Sort-str]!TAB!!TAB!Switches:!TAB![/A:Groupname] [/F:Filepath.ext] [/D]
- Echo/ [Find-searchstring]!TAB!Searches an array for the string value supplied.&Echo/
- %Usage:###=/M:Mode required&Echo/[Def][Sort-int^|str][Find:Value]%
- ) Else Call :GetArgs %*
- If Errorlevel 1 Exit /B 1
- Call :!Mode! %* 2> Nul || (%Usage:###=Invalid Mode /M:!Mode!&Echo/[Def][Sort-int^|str][Find:Value]%)
- Exit /B 0
- :str
- Set "Usage=Echo/###&Echo/%~n0 ["/F:filepath.ext" ^| "/A:Array Group Name"] & Exit /B 1"
- If "!GRPNm!"=="" (%Usage:###=/A:Groupname required%) Else Set "#!GRPNm!=0"
- If "!#FP#!"=="1" (
- (For /F "UseBackQ Delims=" %%G in (`Type "!FilePath!" ^| Sort`)Do (
- For %%x in ("!GRPNm![!#%GRPNm%!]") Do (
- Setlocal DisableDelayedExpansion
- Endlocal & Set "%%~x=%%~G"
- If "!#STDOut!"=="1" Echo/%%~x=%%~G
- )
- Set /A "#!GRPNm!+=1"
- )) 2> Nul || (%Usage:###:=Echo/Invalid Filepath:"!FilePath!"%)
- Exit /B 0
- )
- If Not "!#FP#!"=="1" (For /F "Tokens=1,2 Delims==" %%G in ('Set !GRPNm![')Do Echo/%%H)>"%TEMP%\__Sort.txt"
- (For /F "UseBackQ Delims=" %%G in (`Type "%TEMP%\__Sort.txt" ^| Sort`)Do (
- For %%x in ("!GRPNm![!#%GRPNm%!]") Do (
- Setlocal DisableDelayedExpansion
- Endlocal & Set "%%~x=%%~G"
- If "!#STDOut!"=="1" Echo/%%~x=%%~G
- )
- Set /A "#!GRPNm!+=1"
- )
- )
- Del /Q "%TEMP%\__Sort.txt"
- Exit /B 0
- :Find
- Set "Usage=Echo/###&Echo/%~n0 [/M:Find-Searchstring] [/A:Group Name]&Exit /B 1"
- If "!GRPNm!"=="" %Usage:###=/A:Groupname Required%
- If "!FindV!"=="" %Usage:###=/M:Find-Value Required%
- (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%)
- Exit /B 0
- :Int
- Set "Usage=Echo/###&Echo/%~n0 [/M:Sort-Int] [/A:Group Name] [/N:New Group Name] [Sort-Int] [/H^|/L]&Exit /B 1"
- If "!GRPNm!"=="" %Usage:###=/A:Groupname required%
- If "!nGRPNm!"=="" Set "nGRPNm=!GRPNm!"
- If Not "%#Order%"=="" (Call :Sort%#Order% !nGRPNm! #!nGRPNm! !Inset!) Else (%Usage:###=Sort Order Required /H or /L%)
- Exit /B 0
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Subroutines for Population of Arrays with numeric values in sorted order.
- :sortL <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
- Set "%2=0"
- FOR %%P In (%*) DO If Not "%%P"=="%1" If Not "%%P"=="%2" If Not "%%P"=="" (
- Set "%1[!%2!]=!%%P!"
- Set /A "%2+=1"
- )
- For /L %%a In (1,1,!%2!)Do (
- Set /A "S_Offset=%%a - 1"
- For /L %%b IN (0,1,%%a)Do (
- If not %%b==%%a For %%c in (!S_Offset!)Do (
- IF !%1[%%c]! LEQ !%1[%%b]! (
- Set "tmpV=!%1[%%c]!"
- Set "%1[%%c]=!%1[%%b]!"
- Set "%1[%%b]=!tmpV!"
- ))))
- Set /A %2-=1
- If "!#STDOut!"=="1" For /L %%G in (0 1 !%2!)Do Echo/%1[%%G]=!%1[%%G]!
- Exit /B 0
- :sortH <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
- Set "%2=0"
- FOR %%P In (%*) DO If Not "%%~P"=="%~1" If Not "%%~P"=="%2" If Not "%%P"=="" (
- Set "%1[!%2!]=!%%~P!"
- Set /A "%2+=1"
- )
- For /L %%a In (1,1,!%2!)Do (
- Set /A "S_Offset=%%a - 1"
- For /L %%b IN (0,1,%%a)Do (
- If not %%b==%%a For %%c in (!S_Offset!)Do (
- If Not !%1[%%c]! LSS !%1[%%b]! (
- Set "tmpV=!%1[%%c]!"
- Set "%1[%%c]=!%1[%%b]!"
- Set "%1[%%b]=!tmpV!"
- ))))
- Set /A %2-=1
- If "!#STDOut!"=="1" For /L %%G in (0 1 !%2!)Do Echo/%1[%%G]=!%1[%%G]!
- Exit /B 0
- :Def
- 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"
- If "!GRPNm!"=="" %Usage:###=/A:Groupname required%
- If "!#ORP#!"=="1" Echo/!SwParam!|findstr /RX [0-9]* > Nul 2> Nul
- If not "!SwParam!"=="" If Errorlevel 1 (%Usage:###=O:!SwParam! #Arg invalid. Only Integers accepted.%)
- If "!#ORP#!"=="1" Set "#!GRPNm!=0"
- If "!#%GRPNm%!"=="" Set "#!GRPNm!=0"
- If "%#FP#%"=="1" (
- If exist "!FilePath!" (
- For /F "Delims=" %%G in (!FilePath!)Do If Not "%%~G"=="" (
- For %%x in ("!GRPNm![!#%GRPNm%!]")Do (
- Setlocal DisableDelayedExpansion
- If "%#STDOut%"=="1" Echo/%%~x=%%~G
- Endlocal & Set "%%~x=%%G"
- )
- Set /A "#!GRPNm!+=1" > Nul
- )
- ) Else (%Usage:###=/F:!FilePath! Invalid path%)
- )
- If not "!Inset!"=="" (
- For %%G in (!Inset!)Do (
- For %%x in ("%GRPNm%[!#%GRPNm%!]")Do (
- Setlocal DisableDelayedExpansion
- If "%#STDOut%"=="1" Echo/%%~x=%%~G
- Endlocal & Set "%%~x=%%~G"
- )
- If Not "!SubEL!"=="" Set "%%~G=!SubEl!"
- Set /A "#!GRPNm!+=1" > Nul
- )
- ) Else (
- If Not "%#FP#%"=="1" (
- For /F "Tokens=1,2 Delims==" %%I in ('Set %GRPNm%')Do Set "%%~I=" > Nul 2> Nul
- Set "#!GRPNm!=" > Nul 2> Nul
- )
- )
- Exit /B 0
- :GetArgs
- If "%~1" == "" (
- 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%)
- 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%)
- Exit /B 0
- )
- Set "Param=%~1"
- Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/M:" > Nul 2> Nul && (
- Set "MODE=!Param:*/M:=!"& Echo/"!Mode!"|"%__AppDir__%findstr.exe" /LIC:"Sort-" > Nul 2> Nul && (Set "MODE=!Mode:*Sort-=!")
- 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%))
- If "!Param:*/M:=!"=="" (
- Echo/&Echo/Modes:&Echo/ [Def]!TAB!!TAB!Define, modify or clear an array.
- Echo/ [Sort-int]!TAB!Sorts array by lowest or highest value using /L or /H switches
- Echo/ [Sort-str]!TAB!Sorts an array or text files string values using alphanumerical order of sort: [0-9][a-z]
- Echo/ [Find:Value]!TAB!Searches an array for the string value supplied.&Echo/
- %Usage:###=/M:Mode required&Echo/[Def][Sort-int^|str][Find:Value]%
- )
- Shift&Goto :GetArgs
- )
- Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/H" > Nul 2> Nul && (Set "#Order=H"&Shift&Goto :GetArgs)
- Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/L" > Nul 2> Nul && (Set "#Order=L"&Shift&Goto :GetArgs)
- Echo/"%~1"|"%__AppDir__%findstr.exe" /LIC:"/D" > Nul 2> Nul && (Set "#STDOut=1"&Shift&Goto :GetArgs)
- 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)
- 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)
- 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)
- 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)
- 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)
- Set Inset=!Inset! %1
- Shift&Goto :GetArgs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement