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
add comment

3 Answers

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
show 3 more comments

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
add comment

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
add comment

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.