BATCH_ Test a String for Number
Testing a String for a number that can only contain the ciphers 0 to 9 and optionally a prefixed - character can be done with the following subroutine.
However this test two limits I can think of:
:: Checks if the first parameter is a number, that is it only contains the :: characters 0 to 9 and additionally the minus as the first character. :: PARAM :: 1 string to test for number :: RETURN String "true" if string is a valid number, "false" otherwise :IS_NUMBER SETLOCAL FOR /F "tokens=*" %%A IN ('SET /A %~1 2^>^&1') DO ( IF "%~1" == "%%A" ( SET RESULT=true ) ELSE ( SET RESULT=false ) ) ENDLOCAL & SET RESULT=%RESULT% GOTO :EOFThis tests the input string using the SET /A arithmetic extension.
However this test two limits I can think of:
- As SET /A echoes the error Missing operand. on invalid input, a test against that very string will return true.
- A number with leading zeros like 007 will not be recognized as a number.
FOR EACH CHARACTER ( FOR %%A IN (0-9) ( IF (%%A == CHAR) ( GOTO :END_INNER ) ) GOTO:EOF & FAIL :END_INNER )
cypressor - 28. Nov, 21:58