Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Command Prompt - Windows 11, 10, etc. ► ««« »»»

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.




1. Here is the commented code in English:
2. Summary





1.) Here is the commented code in English:



@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.





FAQ 167: Updated on: 4 December 2024 12:57 Windows
Windows-Console

Keyboard queries command prompt scripts CMD!?


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
Windows-Console

Proper reboot and shutdown via command prompt?


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
Windows-Console

Correct restart and stopping of services via command prompt!?


To correctly start, stop or restart services in Windows from the command prompt, there are several commands you can use. Here are the main steps and commands:
Windows-Console

Fast start for cmd.exe (Windows Console)?


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
Windows-Console

Console command to see IP Address, how to on all Windows OS?


Find Your IP Address Quickly in every Windows OS, the solution is to simple for every User Account on Windows 11, 10, . and MS Server Please start the
Windows-Console

Delete and Overwrite permanently?


The solution is simple to Delete and Overwrite files and folder permanently in  Windows 11, 10, and MS Server OS Please feel free and use cipher for

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: windows, console, check, input, number, command, prompt, script, simple, using, regular, expression, regex, batch, however, there, direct, support, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + NonCompressibleFiles
  + 4ur-Windows-8-Mouse-Balls
  + 12-Ants
  + Q-Dir
  + PaintOkay
  + DirPrintOK
  + FontViewOK
  + MeinPlatz
  + DesktopOK
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | gc24b | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu


► The stopwatch window menu is annoying when the clock is reduced! ◄
► Can you download Chrome for Windows 10! ◄
► What is the best web server among Windows Server solutions? ◄
► Why is there so much hype about Windows 12 when it's just an OS? ◄


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

....