Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In the Windows NT-based command line (mostly for XP or higher), is there a way to verify if a switch being provided is a number only? Depending on the number, I want it to loop through the code x number of times

share|improve this question

6 Answers 6

up vote 8 down vote accepted

Edited to fix the regex as per debham's comment. Turns out that adding a space before the pipe after echo adds a space to the piped string, which was what broke the start/end of line matching previously. The regex could be further improved by discarding whitespace at the beginning and end.


There's the findstr command. It can search files with regular expressions, much like grep in Linux. It can also search piped input.

@echo off

set param=%1

echo %param%| findstr /r "^[1-9][0-9]*$">nul

if errorlevel 0 (
    echo Valid number
)

Explanation

The parameter used is set into the param variable. However, there is nothing stopping you using the parameter directly (%1 for the first paramater).

findstr is used to search the piped input with the /r flag for regex.

The pattern:

  • ^ means beginning of line.

  • [0-9] means a digit. The * means the previous repeated zero or more times. So [0-9][0-9]* means one digit, plus zero or more digits. In other words, at least one digit. The + one or more times does not seem to be supported by findstr. Note that [1-9] is used for the first digit to disallow leading zeroes - see the comments.

  • $ means end of line.


Now, a for loop in batch for x number of times... if x is not a valid number, the loop actually does not run at all - it just gets skipped for the next line. So there is no need to check if the input is a valid number!

@echo off

set param=%1

for /l %%a in (1,1,%param%) do (
    echo %%a
)

The loop is done using for /l, where (x,y,z) means start at x, increment by y until z is reached. And it sets %%a to the current number/iteration.

Note: this actually fails if there is a leading zero, causing the command processor to treat it as an octal number. See dbenham's answer for a better solution.

share|improve this answer
    
Excellent explanation! And easy to use –  Canadian Luke Mar 24 '12 at 18:28
1  
As written, this solution will fail with a parameter like "this 1 fails" The FINDSTR should do an exact match. It should not do a word match. –  dbenham Jul 11 '13 at 11:56
    
@dbenham Thanks. I tried beginning/end of line matching before, but that failed due to echo passing an extra space. Fixed now. –  Bob Jul 11 '13 at 14:19
    
@CanadianLuke You may wish to take a look at the edit. –  Bob Jul 11 '13 at 14:19
    
+1 for the loop; that's very smart! –  Jeroen Wiert Pluimers Dec 4 '13 at 10:02

This will detect if the first parameter is a valid natural number (non-negative integer).

@echo off
echo %1|findstr /xr "[1-9][0-9]* 0" >nul && (
  echo %1 is a valid number
) || (
  echo %1 is NOT a valid number
)

If you want to allow quotes around the number, then

@echo off
echo "%~1"|findstr /xr /c:\"[1-9][0-9]*\" /c:\"0\" >nul && (
  echo %~1 is a valid number
) || (
  echo %~1 is NOT a valid number
)

Note - leading zeros are disallowed because batch treats them as octal, so a value like 09 is invalid, and 010 has a value of 8.

share|improve this answer

The following works very well for me. SET /a param=%1+0 always returns 0 if %1 is empty or not numeric. Otherwise, it provides the given number.

SET /a param=%1+0
IF NOT %param%==0 ECHO Valid number
share|improve this answer
2  
This will fail if parameter = 0. Also will treat a parameter like "1+1" as a number, which might cause problems, depending on requirements. –  dbenham Jul 11 '13 at 11:59

You can validate any variable if its number:

SET "var="&for /f "delims=0123456789" %i in ("%a") do set var=%i
if defined var (echo."NIC">nul) else (echo."number")
share|improve this answer
set xx=33
echo %xx%
SET /a %xx%+0 2>nul >nul && echo all Digits


set xx=3x3
echo %xx%
SET /a %xx%+0 2>nul >nul || echo No Digits
share|improve this answer
2  
Can you add some explanation too, please? –  slhck Jul 25 at 9:23
:main 
set /p input=text
if %input% equ 0 goto valid
set /a inputval="%input%"*1
if %inputval% equ 0 goto invalid
goto valid

:invalid
echo Input is not an integer.

:valid
echo Input is an integer.

Here is a game that uses this function.

@echo off
:main
set /a guessmin=1
set /a guessmax=100
set /a guessrand=%random% %%100 +1
echo.
:check
if %guessmin% equ %guessmax% goto fail
set /p input=- Pick a number %guessmin% - %guessmax%: 
set /a inputval="%input%"*1
if %inputval% equ 0 goto invalid
if %inputval% gtr %guessmax% goto invalid
if %inputval% lss %guessmin% goto invalid
if %inputval% gtr %guessrand% goto high
if %inputval% lss %guessrand% goto low
if %inputval% equ %guessrand% goto mid
:invalid
echo   Please enter a valid number.
echo.
goto check
:high
echo   Your guess was too high.
echo.
set /a guessmax=%inputval%-1
goto check
:low
echo   Your guess was too low.
echo.
set /a guessmin=%inputval%+1
goto check
:mid
echo   Your guess was correct. The game will now reset.
set /p input=- Press enter to play again.
cls
goto main
:fail
echo   You actually managed to lose because there is only
echo   one number remaining. That number is %guessrand%.
set /p input=- Press enter to lose again.
cls
goto main
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.