Skip to main content

How to Check if a String is a Valid Number in Batch Script

A common requirement in interactive or data-driven scripts is to validate user input or data from a file to ensure it's a number before performing calculations or other operations. Batch scripting has no built-in IsNumeric() function, which means that variables are just strings of text, and a variable holding "123" is not inherently different from one holding "abc".

This guide will teach you the two primary methods for validating numeric input. We'll start with a simple but flawed trick using the SET /A command, and then cover the far more robust and recommended method using the FINDSTR utility with a regular expression.

The Challenge: No Built-in IsNumeric() Function

In batch, the variable %VAR% is always a string. The command SET /A is the only native tool that attempts to interpret a string as a number to perform arithmetic. We can leverage how SET /A handles non-numeric strings to create a check, but as we'll see, this has a critical flaw.

Method 1: The SET /A Arithmetic Trick (Flawed but Simple)

The SET /A command evaluates strings as numbers. If it encounters a string that is not a valid number, it treats it as a variable name with a value of 0.

The Logic: We can check if SET /A evaluates our variable to 0. If it does, it's probably not a valid number.

@ECHO OFF
SET "INPUT_STRING=123"

SET /A "result=%INPUT_STRING%"
IF %result% EQU 0 (
ECHO The string is NOT a valid number.
) ELSE (
ECHO The string is a valid number.
)

The Critical Flaw

This method has a fatal flaw: it fails for the number 0 itself. If INPUT_STRING is "0", SET /A will correctly evaluate it to 0, and our logic will incorrectly report that it's not a valid number.

Example of the error:

SET "INPUT_STRING=0"
SET /A "result=%INPUT_STRING%"
IF %result% EQU 0 ECHO The string is NOT a valid number.

Output (This is wrong!):

The string is NOT a valid number.

Solution

To make this method work, you must add a separate, explicit check for "0".

SET /A "result=%INPUT_STRING%"
IF "%INPUT_STRING%"=="0" (
ECHO The string is a valid number.
) ELSE IF %result% EQU 0 (
ECHO The string is NOT a valid number.
) ELSE (
ECHO The string is a valid number.
)

This is already becoming complicated, which is why the next method is recommended.

A far more reliable method is to check the content of the string itself. We can use FINDSTR with a simple regular expression to test if the string consists only of digits.

The Logic: We ECHO our string and pipe it to FINDSTR, which searches for a pattern that matches a purely numeric string. FINDSTR sets %ERRORLEVEL% to 0 for a match and 1 for no match.

@ECHO OFF
SET "INPUT_STRING=12345"

ECHO %INPUT_STRING% | FINDSTR /R "^[0-9]*$" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO The string is a valid number.
) ELSE (
ECHO The string is NOT a valid number.
)

How the regular expression works

  • ^: Asserts the start of the line.
  • [0-9]: Matches any single digit from 0 to 9.
  • *: Matches the previous element (a digit) zero or more times.
  • $: Asserts the end of the line.

Together, ^[0-9]*$ means "a string that contains nothing but digits from beginning to end." This method correctly handles "0" and is much more robust.

How to Handle Negative Numbers

The simple FINDSTR regex will fail for negative numbers because of the - sign. We can easily adapt the regex to allow an optional leading hyphen.

Example of script that handles integers (positive or negative):

@ECHO OFF
SET "INPUT_STRING=-500"

REM First, handle the special case of a lone hyphen, which is not a number.
IF "%INPUT_STRING%"=="-" GOTO :NotANumber

REM The regex ^-?[0-9]*$ allows an optional hyphen at the start.
ECHO %INPUT_STRING% | FINDSTR /R "^-?[0-9]*$" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO The string is a valid integer.
GOTO :End
)

:NotANumber
ECHO The string is NOT a valid integer.

:End

The Limitation: What About Decimals?

Neither of these native methods can validate floating-point numbers (e.g., "123.45"). The SET /A command only works with integers, and FINDSTR would require a much more complex regular expression.

Solution: For decimal validation, the best approach is to call out to a more powerful scripting language like PowerShell, which has native support for different number types.

Practical Example: A User Input Validation Loop

This is a classic use case. The script prompts the user to enter their age and will not continue until they have entered a valid, non-negative number. It uses the recommended FINDSTR method.

@ECHO OFF
SETLOCAL

:AskForAge
SET "AGE="
SET /P "AGE=Please enter your age: "

REM Check if the input is numeric
ECHO "%AGE%" | FINDSTR /R "^[0-9]*$" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Invalid input. Please enter only numbers.
ECHO.
GOTO :AskForAge
)

REM Check if the input is empty (the regex above allows empty)
IF NOT DEFINED AGE (
ECHO.
ECHO You must enter a value.
ECHO.
GOTO :AskForAge
)

ECHO.
ECHO Thank you. Your age is %AGE%.

ENDLOCAL

Conclusion

While batch scripting lacks a native IsNumeric function, there are effective workarounds to validate numbers.

  • The SET /A method is a simple trick that is useful for quick checks but is fundamentally flawed because it fails to correctly identify "0" without extra code.
  • The FINDSTR method is the overwhelmingly superior and recommended approach. It is reliable, robust, and can be easily adapted to handle negative numbers.

For any script that requires dependable numeric validation, the FINDSTR technique is the professional and correct choice.