(view source code of isnumber.bat as plain text)
@ECHO OFF
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
IF "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
ECHO "%~1"| FIND "/" >NUL && GOTO Syntax
ECHO "%~1"| FIND "?" >NUL && GOTO Syntax
ECHO "%~1"| FIND "'" >NUL && GOTO Syntax
ECHO "%~1"| FIND "`" >NUL && GOTO Syntax
ECHO "%~1"| FIND " " >NUL && GOTO Syntax
ECHO "%~1"| FIND " " >NUL && GOTO Syntax
IF NOT %1==%~1 GOTO Syntax
:: Localize variables and enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
: Strip quotes and leading and trailing spaces
FOR /F "tokens=*" %%A IN ("%~1") DO SET Value=%%~A
:: Binary
SET RC=2
FOR /F "tokens=1 delims=01" %%A IN ("%Value%") DO SET RC=0
IF %RC% EQU 2 EXIT /B 2
:: Octal
SET RC=8
FOR /F "tokens=1 delims=01234567" %%A IN ("%Value%") DO SET RC=0
IF %RC% EQU 8 EXIT /B 8
:: Positive decimal
SET RC=10
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value%") DO SET RC=0
IF %RC% EQU 10 EXIT /B 10
:: Negative decimal
SET RC=11
IF "%Value:~0,1%"=="-" (
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value:~1%") DO SET RC=0
IF !RC! EQU 11 EXIT /B 11
)
:: Hexadecimal
SET RC=16
FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value%") DO SET RC=0
IF %RC% EQU 16 EXIT /B 16
IF /I "%Value:~-1,1%"=="H" (
FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value:~0,-1%") DO SET RC=0
IF !RC! EQU 16 EXIT /B 16
)
IF /I "%Value:~0,2%"=="0X" (
FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value:~2%") DO SET RC=0
IF !RC! EQU 16 EXIT /B 16
)
:: End localization of variables
ENDLOCAL
:Syntax
:: Set return code to 0
%COMSPEC% /K EXIT >NUL
:: Display help
ECHO.
ECHO IsNumber.bat, Version 1.00 for Windows NT 4 SP6 / 2000 / XP
ECHO Check if a value is numeric
ECHO.
:: Display the next line in MS-DOS
IF NOT "%OS%"=="Windows_NT" ECHO Usage: CALL ISNUMBER.BAT number (may be redirected to NUL)
:: The next line will NOT be displayed in MS-DOS, since MS-DOS does not recognize the
:: caret as an escape character and hence redirects the ECHOed output to the NUL device
ECHO Usage: CALL ISNUMBER.BAT number ^> NUL
ECHO IF %%ErrorLevel%% EQU 16 ECHO Hexadecimal
ECHO IF %%ErrorLevel%% EQU 11 ECHO Negative decimal
ECHO IF %%ErrorLevel%% EQU 10 ECHO Positive decimal
ECHO IF %%ErrorLevel%% EQU 8 ECHO Octal
ECHO IF %%ErrorLevel%% EQU 2 ECHO Binary
ECHO IF %%ErrorLevel%% EQU 1 ECHO Batch file not found
ECHO IF %%ErrorLevel%% EQU 0 ECHO Not a number
ECHO.
ECHO Note: Hexadecimal numbers can be formated as 4A1F, 0x4A1F or 4A1FH.
ECHO Octal or binary numbers should have no prefix, nor suffix.
ECHO Negative numbers will always be considered decimal.
ECHO Negative numbers must be in -12345 format.
ECHO Quotes and spaces are not allowed.
ECHO This help will be displayed unless a valid number is supplied.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
page last modified: 2024-04-16; loaded in 0.0056 seconds