Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off & Setlocal
- Rem Usage Examples:
- Call:ValidNum /V:08 && Echo True || Echo false
- Call:ValidNum /V:8 $Rv1 && Echo True || Echo false
- Call:ValidNum 200 400 $Rv2
- Set $
- Pause
- Call:ValidNum -?
- Pause
- Endlocal & Goto:Eof
- :######################################################################################
- :ValidNum
- :# :ValidNum v2.0 by T3RRY
- :# Usage: Call:ValidNum [returnvar] [max] [min] [/V:Integer] [-?]
- :#
- :# - Forces positive numeric input within min ~ max range
- :# - Or: Returns Errorlevel 0 / 1 to to verify if a string
- :# supplied as value to /V: switch is a positive integer within the min / max
- :# range, rejecting leading zeros - Note if true, the default Return var $Num
- :# will be assigned with value unless a return var is provided.
- :# - Retruns errorlevel 1 if no value provided to switch.
- :#
- :# - the help switch: -? ignores other arguments.
- :# - All Arguments + Switch are optional, order irrelevant.
- :#
- :# - Default Return var = $Num
- :# - Default Min = 1
- :# - Default max = 2147483647
- :#
- :# - Return var if used must have at least one NON-integer character.
- :# - max / min will be assigned in accordance with the highest integer value provided.
- :# If only a single integer is provided, it will be assigned as max, default min of 1.
- :# - /V:Integer is treated as the input to test, and never assigned as max / min.
- :#
- :# - Examples:
- :# Call:ValidNum 1 31 $Rv1
- :# Call:ValidNum $Rv2 100 1
- :# Call:ValidNum 1024
- :# Call:ValidNum /V:08 && Echo True || Echo false
- :# Call:ValidNum /V:8 $Rv1 && Echo True || Echo false
- :#
- :######################################################################################
- SETLOCAL EnableDelayedExpansion
- For /l %%n in (1 1 3) Do ( Set "tArg[%%n]=" & Set "Arg[%%n]=" )
- Set "sign=-1" & Set "Max=" & Set "Min="
- Set "IsTest=" & Set "Params=" & Set "Rvar=$Num"
- If not "%~1"=="" ( Set Params=%*
- Set ^"Params=!Params:"=!"
- If defined Params ( Set "Params=!Params:,= !"
- Set "Params=!Params:;= !"
- Set "Params=!Params:/V: =/V:!"
- If not "!Params:-?=!"=="!Params!" (
- %Systemroot%\System32\Findstr.exe /blic:":#" "%~f0"
- Endlocal & Exit /b 0
- )))
- If Defined Params ( Set "i=1"
- Set "Arg[!i!]=%Params: ="& Set /A i+=1 &Set "Arg[!i!]=%"
- For /f "Tokens=2,3 Delims=[]=" %%G in ('Set Arg[')Do (
- Set ^"Arg[%%G]=!Arg[%%G]:"=!"
- Set "tArg[%%G]=!Arg[%%G]!"
- If defined tArg[%%G] Set ^"tArg[%%G]=!tArg[%%G]:"=!"
- For /l %%i in (0 1 9)Do If defined tArg[%%G] Set "tArg[%%G]=!tArg[%%G]:%%i=!"
- )
- For /l %%i in (1 1 3)Do (
- If defined tArg[%%i] (
- If "!Arg[%%i]:/V:=!"=="!Arg[%%i]!" ( Set "Rvar=!Arg[%%i]!" )Else (
- Set "Input=!Arg[%%i]:/V:=!"
- Set "IsTest=true"
- )))
- 2> nul (
- If not defined tArg[1] If Defined Arg[1] Set /A Max=Arg[1]
- If not defined tArg[2] If Defined Arg[2] If defined Max (
- Set /A Min=Arg[2]
- )Else Set /A Max=Arg[2]
- If not defined tArg[3] If Defined Arg[3] If defined Max (
- Set /A Min=Arg[3]
- )Else Set /A Max=Arg[3]
- ))
- If not Defined IsTest (
- If 1!Min! GTR 1!Max! (
- 2> nul Set /A "Max=%Min%" || Set "Max="
- 2> nul Set /A "Min=%Max%" || Set "Min="
- ))
- 2> nul Set /a "max=%Max%" || Set "max=2147483647"
- 2> nul Set /a "min=%Min%" || Set "min=1"
- :VNumIn
- If Not Defined IsTest ( Set "input="
- Set /p "input=Enter a number GEQ %min% LEQ %max%: "
- )
- Rem Any Input surviving the Below Set /A testing is safe for expansion
- Rem Input Testing. input +/- , input > min , input < max , Hex/Octal for comparison
- If Not Defined IsTest Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" 2>nul || Goto:VNumIn
- If Defined IsTest Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" 2>nul || Set "IsTest=false"
- Rem compare assignments to block input of hex, octal or numbers with leading 0's
- If Not Defined IsTest If not "%Input%"=="%HexOct%" Goto:VNumIn
- If Defined IsTest If not "%Input%"=="%HexOct%" Set "IsTest=false"
- If /I "!IsTest!"=="false" ( Endlocal & Exit /b 1 )
- If /I "!IsTest!"=="true" ( Endlocal & Set "%Rvar%=%Input%" 2> nul || Set "$VNum=%Input%"
- Exit /b 0
- )
- ( %= return value in ReturnVar string if used ; else return in $Num =%
- ENDLOCAL & Set "%Rvar%=%input%" 2> nul || Set "$Num=%input%"
- Exit /B 0
- )
Add Comment
Please, Sign In to add comment