Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for isnumber.bat

(view source code of isnumber.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT"       GOTO Syntax
  3. IF      "%~1"==""                 GOTO Syntax
  4. IF NOT  "%~2"==""                 GOTO Syntax
  5. ECHO "%~1"| FIND "/"      >NUL && GOTO Syntax
  6. ECHO "%~1"| FIND "?"      >NUL && GOTO Syntax
  7. ECHO "%~1"| FIND "'"      >NUL && GOTO Syntax
  8. ECHO "%~1"| FIND "`"      >NUL && GOTO Syntax
  9. ECHO "%~1"| FIND " "      >NUL && GOTO Syntax
  10. ECHO "%~1"| FIND "	" >NUL && GOTO Syntax
  11. IF NOT     %1==%~1                GOTO Syntax
  12.  
  13. :: Localize variables and enable delayed variable expansion
  14. SETLOCAL ENABLEDELAYEDEXPANSION
  15.  
  16. : Strip quotes and leading and trailing spaces
  17. FOR /F "tokens=*" %%A IN ("%~1") DO SET Value=%%~A
  18.  
  19. :: Binary
  20. SET RC=2
  21. FOR /F "tokens=1 delims=01" %%A IN ("%Value%") DO SET RC=0
  22. IF %RC% EQU 2 EXIT /B 2
  23.  
  24. :: Octal
  25. SET RC=8
  26. FOR /F "tokens=1 delims=01234567" %%A IN ("%Value%") DO SET RC=0
  27. IF %RC% EQU 8 EXIT /B 8
  28.  
  29. :: Positive decimal
  30. SET RC=10
  31. FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value%") DO SET RC=0
  32. IF %RC% EQU 10 EXIT /B 10
  33.  
  34. :: Negative decimal
  35. SET RC=11
  36. IF "%Value:~0,1%"=="-" (
  37. 	FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value:~1%") DO SET RC=0
  38. 	IF !RC! EQU 11 EXIT /B 11
  39. )
  40.  
  41. :: Hexadecimal
  42. SET RC=16
  43. FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value%") DO SET RC=0
  44. IF %RC% EQU 16 EXIT /B 16
  45. IF /I "%Value:~-1,1%"=="H" (
  46. 	FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value:~0,-1%") DO SET RC=0
  47. 	IF !RC! EQU 16 EXIT /B 16
  48. )
  49. IF /I "%Value:~0,2%"=="0X" (
  50. 	FOR /F "tokens=1 delims=0123456789ABCDEFabcdef" %%A IN ("%Value:~2%") DO SET RC=0
  51. 	IF !RC! EQU 16 EXIT /B 16
  52. )
  53.  
  54. :: End localization of variables
  55. ENDLOCAL
  56.  
  57.  
  58. :Syntax
  59. :: Set return code to 0
  60. %COMSPEC% /K EXIT >NUL
  61.  
  62. :: Display help
  63. ECHO.
  64. ECHO IsNumber.bat,  Version 1.00 for Windows NT 4 SP6 / 2000 / XP
  65. ECHO Check if a value is numeric
  66. ECHO.
  67. :: Display the next line in MS-DOS
  68. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  CALL  ISNUMBER.BAT  number  (may be redirected to NUL)
  69. :: The next line will NOT be displayed in MS-DOS, since MS-DOS does not recognize the
  70. :: caret as an escape character and hence redirects the ECHOed output to the NUL device
  71. ECHO Usage:  CALL  ISNUMBER.BAT  number  ^> NUL
  72. ECHO         IF %%ErrorLevel%% EQU 16 ECHO Hexadecimal
  73. ECHO         IF %%ErrorLevel%% EQU 11 ECHO Negative decimal
  74. ECHO         IF %%ErrorLevel%% EQU 10 ECHO Positive decimal
  75. ECHO         IF %%ErrorLevel%% EQU  8 ECHO Octal
  76. ECHO         IF %%ErrorLevel%% EQU  2 ECHO Binary
  77. ECHO         IF %%ErrorLevel%% EQU  1 ECHO Batch file not found
  78. ECHO         IF %%ErrorLevel%% EQU  0 ECHO Not a number
  79. ECHO.
  80. ECHO Note:   Hexadecimal numbers can be formated as 4A1F, 0x4A1F or 4A1FH.
  81. ECHO         Octal or binary numbers should have no prefix, nor suffix.
  82. ECHO         Negative numbers will always be considered decimal.
  83. ECHO         Negative numbers must be in -12345 format.
  84. ECHO         Quotes and spaces are not allowed.
  85. ECHO         This help will be displayed unless a valid number is supplied.
  86. ECHO.
  87. ECHO Written by Rob van der Woude
  88. ECHO http://www.robvanderwoude.com
  89.  

page last modified: 2024-04-16; loaded in 0.0056 seconds