Check if input is a number in command prompt script!?
To check if the input is a number, you can do a simple check using if and a regular expression (RegEx) in a batch script. However, there is no direct support for regular expressions in CMD, but you can use the set /a statement to ensure that only valid numbers are processed.
@echo off
:: Prevents commands from being displayed in the terminal.
:start
:: Defines the starting point of the loop.
chcp 65001 >nul
:: Sets the code page to UTF-8 and suppresses output.
echo Addition
echo Please enter the 2 numbers you want to add. Confirm each number with ENTER.:: Input of the first number
set /p num1=First number:
:: Prompts the user to enter the first number.
call :check_if_number %num1%
:: Calls the subroutine to check if the input is a valid number.
if errorlevel 1 (:: If the input is invalid, an error message is displayed.
echo Error: "%num1%" is not a valid number.
goto start
:: Returns to the starting point.):: Input of the second number
set /p num2=Second number:
:: Prompts the user to enter the second number.
call :check_if_number %num2%
:: Calls the subroutine again to check the second input.
if errorlevel 1 (:: If the second input is invalid, an error message is displayed.
echo Error: "%num2%" is not a valid number.
goto start
:: Returns to the starting point.)
echo %num1% + %num2%?
pause
:: Displays the entered values and pauses to let the user confirm.:: Calculation of the sum
set /a Answer=%num1%+%num2%
:: Adds the two numbers and stores the result in the variable "Answer".
echo Result: %Answer%
:: Outputs the result.
pause
:: Pauses to allow the user to view the result.
goto start
:: Returns to the starting point to perform another addition.
:e
:: Marks the end of the program (currently unused).
echo Done!
pause
exit /b
:: Exits the script.:: Subroutine to check if the input is a number
:check_if_number
:: Begins the subroutine to validate numeric input.
echo %1 | findstr /r "^[0-9]" >nul
:: Checks if the input starts with a digit (regular expression).
if errorlevel 1 (:: If no number is found, the subroutine returns an error status.
exit /b 1)
exit /b 0:: Exits the subroutine with status 0 (success) if the input is valid.
2.) Summary
- Error Handling: The script ensures only valid numbers are processed.
- Subroutine: `:check_if_number` validates input to avoid errors in calculations.
- Loop: After each addition, the script restarts to allow continuous usage.
Ability to process keystrokes in a CMD script or command prompt. There is no direct way to capture keystrokes in the Windows Command Prompt CMD without
A Windows computer can be shut down or restarted quickly and efficiently using the command prompt. This method is especially useful when other control methods
The fastest way to run or start the command prompt in every Windows System, is via RUN-Dialog in Windows 11, 10, and MS Server. Please start via Keyboard-Shortcut
The solution to scan all protected system files and replaces incorrect versions with correct Microsoft versions in all Windows 10, 11, . and Server Systems.
This website does not store personal data. However, third-party providers are used to display ads, which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF). The CMP ID is 300 and can be individually customized at the bottom of the page. more Infos & Privacy Policy ....