T3RRYT3RR0R

Foolproof Integer only Input

Dec 3rd, 2021 (edited)
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.38 KB | None | 0 0
  1. @Echo off & Setlocal
  2.  
  3. Rem Usage Examples:
  4.  
  5.  Call:ValidNum /V:08 && Echo True || Echo false
  6.  Call:ValidNum /V:8 $Rv1 && Echo True || Echo false
  7.  Call:ValidNum 200 400 $Rv2
  8.  Set $
  9.  
  10.  Pause
  11.  Call:ValidNum -?
  12.  Pause
  13. Endlocal & Goto:Eof
  14.  
  15. :######################################################################################
  16. :ValidNum
  17. :# :ValidNum                                                             v2.0 by T3RRY
  18. :# Usage: Call:ValidNum [returnvar] [max] [min] [/V:Integer] [-?]
  19. :#
  20. :# - Forces positive numeric input within min ~ max range
  21. :# - Or: Returns Errorlevel 0 / 1 to to verify if a string
  22. :#        supplied as value to /V: switch is a positive integer within the min / max
  23. :#        range, rejecting leading zeros - Note if true, the default Return var $Num
  24. :#        will be assigned with value unless a return var is provided.
  25. :#        - Retruns errorlevel 1 if no value provided to switch.
  26. :#
  27. :# - the help switch: -? ignores other arguments.
  28. :# - All Arguments + Switch are optional, order irrelevant.
  29. :#
  30. :# - Default Return var = $Num
  31. :# - Default Min        = 1
  32. :# - Default max        = 2147483647
  33. :#
  34. :# - Return var if used must have at least one NON-integer character.
  35. :# - max / min will be assigned in accordance with the highest integer value provided.
  36. :#   If only a single integer is provided, it will be assigned as max, default min of 1.
  37. :# - /V:Integer is treated as the input to test, and never assigned as max / min.
  38. :#
  39. :# - Examples:
  40. :#  Call:ValidNum 1 31 $Rv1
  41. :#  Call:ValidNum $Rv2 100 1
  42. :#  Call:ValidNum 1024
  43. :#  Call:ValidNum /V:08 && Echo True || Echo false
  44. :#  Call:ValidNum /V:8 $Rv1 && Echo True || Echo false
  45. :#
  46. :######################################################################################
  47.  
  48. SETLOCAL EnableDelayedExpansion
  49.  For /l %%n in (1 1 3) Do ( Set "tArg[%%n]=" & Set "Arg[%%n]=" )
  50.  Set "sign=-1" & Set "Max=" & Set "Min="
  51.  Set "IsTest=" & Set "Params=" & Set "Rvar=$Num"
  52.  
  53.  If not "%~1"=="" ( Set Params=%*
  54.   Set ^"Params=!Params:"=!"
  55.   If defined Params ( Set "Params=!Params:,= !"
  56.    Set "Params=!Params:;= !"
  57.    Set "Params=!Params:/V: =/V:!"
  58.    If not "!Params:-?=!"=="!Params!" (
  59.     %Systemroot%\System32\Findstr.exe /blic:":#" "%~f0"
  60.     Endlocal & Exit /b 0
  61.  )))
  62.  
  63.  If Defined Params ( Set "i=1"
  64.   Set "Arg[!i!]=%Params: ="& Set /A i+=1 &Set "Arg[!i!]=%"
  65.   For /f "Tokens=2,3 Delims=[]=" %%G in ('Set Arg[')Do (
  66.    Set ^"Arg[%%G]=!Arg[%%G]:"=!"
  67.    Set "tArg[%%G]=!Arg[%%G]!"
  68.    If defined tArg[%%G] Set ^"tArg[%%G]=!tArg[%%G]:"=!"
  69.    For /l %%i in (0 1 9)Do If defined tArg[%%G] Set "tArg[%%G]=!tArg[%%G]:%%i=!"
  70.   )
  71.   For /l %%i in (1 1 3)Do (
  72.    If defined tArg[%%i] (
  73.     If "!Arg[%%i]:/V:=!"=="!Arg[%%i]!" ( Set "Rvar=!Arg[%%i]!" )Else (
  74.      Set "Input=!Arg[%%i]:/V:=!"
  75.      Set "IsTest=true"
  76.   )))
  77.   2> nul (
  78.    If not defined tArg[1] If Defined Arg[1] Set /A Max=Arg[1]
  79.    If not defined tArg[2] If Defined Arg[2] If defined Max (
  80.     Set /A Min=Arg[2]
  81.    )Else Set /A Max=Arg[2]
  82.    If not defined tArg[3] If Defined Arg[3] If defined Max (
  83.     Set /A Min=Arg[3]
  84.    )Else Set /A Max=Arg[3]
  85.  ))
  86.  
  87.  If not Defined IsTest (
  88.   If 1!Min! GTR 1!Max! (
  89.    2> nul Set /A "Max=%Min%" || Set "Max="
  90.    2> nul Set /A "Min=%Max%" || Set "Min="
  91.  ))
  92.  2> nul Set /a "max=%Max%" || Set "max=2147483647"
  93.  2> nul Set /a "min=%Min%" || Set "min=1"
  94.  
  95. :VNumIn
  96.  If Not Defined IsTest ( Set "input="
  97.   Set /p "input=Enter a number GEQ %min% LEQ %max%: "
  98.  )
  99.  
  100. Rem Any Input surviving the Below Set /A testing is safe for expansion
  101. Rem Input Testing.                   input +/-           , input > min      , input < max   , Hex/Octal for comparison
  102.  If Not Defined IsTest Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" 2>nul || Goto:VNumIn
  103.  If Defined IsTest  Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" 2>nul || Set "IsTest=false"
  104. Rem compare assignments to block input of hex, octal or numbers with leading 0's
  105.  If Not Defined IsTest If not "%Input%"=="%HexOct%" Goto:VNumIn
  106.  If Defined IsTest If not "%Input%"=="%HexOct%" Set "IsTest=false"
  107.  
  108.  If /I "!IsTest!"=="false" ( Endlocal & Exit /b 1 )
  109.  If /I "!IsTest!"=="true"  ( Endlocal & Set "%Rvar%=%Input%" 2> nul || Set "$VNum=%Input%"
  110.   Exit /b 0
  111.  )
  112.  
  113. ( %= return value in ReturnVar string if used ; else return in $Num =%
  114.  ENDLOCAL & Set "%Rvar%=%input%" 2> nul || Set "$Num=%input%"
  115.  Exit /B 0
  116. )
Add Comment
Please, Sign In to add comment