How to check if a parameter (or variable) is a number in a Batch script

后端 未结 10 498
南方客
南方客 2020-12-01 08:02

I need to check if a parameter that is passed to a Windows Batch file is a numeric value or not. It would be good for the check to also works for variables.

I found a

相关标签:
10条回答
  • 2020-12-01 08:38
    SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%i
    if defined var (echo %1 NOT numeric) else (echo %1 numeric)
    

    Replace %1 with %yourvarname% as appropriate

    0 讨论(0)
  • 2020-12-01 08:44

    You could try this. The variable passed is for example var and %var% is equal to 500.

    set /a varCheck=%var%
    if %varCheck% == %var% (goto :confirmed) else (exit /B)
    exit /B
    
    :confirmed
    :: You can use %var% here, and it should only be executed if it is numerical!
    

    if %var% is equal to e.g. a3453d, then it would set varCheck to be 0, and because 0 is not equal to a3453d, then it will exit batch processing.

    (The exit on line 3 is just in case the if statement decides not to execute for some reason ... XD.)

    0 讨论(0)
  • 2020-12-01 08:47

    Does the same thing as above. Have problems with special characters. However, the solution is of a different approach.

    @echo off
    cls
    setlocal enabledelayedexpansion
    
    if "%1"=="" (goto:eof) else (set _VAR2CHECK=%1)
    
    for /l %%a in (0,1,9) do (
       if defined _VAR2CHECK (set "_VAR2CHECK=!_VAR2CHECK:%%a=!") else (goto :end)
    )
    
    :end
    if defined _VAR2CHECK (echo %1 #NOT A VALID NUMBER) else (echo %1 #A VALID NUMBER)
    
    set "_VAR2CHECK="
    endlocal
    

    Test scenarions:

    C:\>ISNUM 1A
    1A  #NOT A VALID NUMBER
    
    C:\>ISNUM 11
    11  #A VALID NUMBER
    
    0 讨论(0)
  • 2020-12-01 08:48

    The easiest for positive integers should be:

    IF 1%1 NEQ +1%1 echo Notnumeric!
    

    If negative numbers (hyphen) are also to be considered, this will work

    SET number=%1
    if 1%1 EQU +1%1 echo positive number
    if %1==-%number:-=% echo negative number
    

    Learned from this article here

    0 讨论(0)
  • 2020-12-01 08:49

    I use this function instead, it seems to work for me without any issues...

    The Script (mytestscript.bat)

    REM == Script variables =============================================
    SET "NUMCODE=%1"
    SET "NAME=%2"
    
    REM == Main script ==================================================
    CALL :Validate "NUMCODE"
    IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
    CALL :Validate "NAME"
    IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
    
    ECHO Validation Complete and all variables passed validation.
    
    REM == Functions ====================================================
    :IsNumeric [string-value] [return-val]
        SET "stringVal=%~1"
        SET /A numericVal=%stringVal% > nul 2> nul
        IF "%stringVal%"=="%numericVal%" (SET "%~2=1") ELSE (SET "%~2=0")
        EXIT /B 0
    
    :Validate
        SET PASSEDVALIDATION=0
    
        ECHO Validating %~1...
    
        IF "%~1"=="NUMCODE" (
            ECHO - Value: %NUMCODE%
            IF NOT "%NUMCODE%"=="" SET PASSEDVALIDATION=1
            IF "!PASSEDVALIDATION!"=="1" (
                CALL :IsNumeric %NUMCODE% RETVAL
                SET PASSEDVALIDATION=!RETVAL!
            )
            IF "!PASSEDVALIDATION!"=="1" (
                IF NOT %NUMCODE% GEQ 1 SET PASSEDVALIDATION=0
            )
            IF "!PASSEDVALIDATION!"=="1" (
                IF NOT %NUMCODE% LEQ 526 SET PASSEDVALIDATION=0
            )
            IF "!PASSEDVALIDATION!"=="0" (
                ECHO - The first parameter is invalid, it can not be blank.
                ECHO - Valid value is a number from 1 to 526.
                ECHO - Leading zeros are not valid.
            )
        )
    
        IF "%~1"=="NAME" (
            ECHO - Value: %NAME%
            IF NOT "%NAME%"=="" SET PASSEDVALIDATION=1
            IF "!PASSEDVALIDATION!"=="0" (
                ECHO - The second parameter is invalid, it can not be blank.
            )
        )
    
        IF "!PASSEDVALIDATION!"=="0" (
            ECHO - Failed validation.
            EXIT /B 1
        ) ELSE (
            ECHO - Passed validation.
        )
        EXIT /B 0
    

    Usage Examples:

    C:\mytestscript.bat 10 MrTest
    Validating NUMCODE...
    - Value: 10
    - Passed validation.
    
    Validating NAME...
    - Value: MrTest
    - Passed validation.
    
    C:\mytestscript.bat 600
    Validating NUMCODE...
    - Value: 600
    - The first parameter is invalid, it can not be blank.
    - Valid value is a number from 1 to 526.
    - Leading zeros are not valid.
    - Failed validation.
    
    C:\mytestscript.bat 10
    Validating NUMCODE...
    - Value: 10
    - Passed validation.
    
    Validating NAME...
    - Value: 
    - The second parameter is invalid, it can not be blank.
    - Failed validation.
    
    0 讨论(0)
  • 2020-12-01 08:50

    Just try to divide per 1. But it doesn't work with numbers out of Z (non relative integer or decimal).

    set var=29
    set /a number=%var% >nul 2>&1
    set /a number=%number% / 1 >nul 2>&1
    if "%var%" == "%number%" (echo numeric) else (echo NOT numeric)
    
    0 讨论(0)
提交回复
热议问题