Batch File input validation - Make sure user entered an integer

Posted by B2Ben on Stack Overflow See other posts from Stack Overflow or by B2Ben
Published on 2009-03-26T02:15:15Z Indexed on 2010/04/15 9:43 UTC
Read the original article Hit count: 456

Filed under:
|
|
|

I'm experimenting with a DOS batch file to perform a simple operation which requires the user to enter a non-negative integer. I'm using simple batch-file techniques to get user input:

@ECHO OFF
SET /P UserInput=Please Enter a Number:

The user can enter any text they want here, so I would like to add some routine to make sure what the user entered was a valid number. That is... they entered at least one character, and every character is a number from 0 to 9. I'd like something I can feed the UserInput into. At the end of the routine would be like an if/then that would run different statements based on whether or not it was actually a valid number.

I've experimented with loops and substrings and such, but my knowledge and understanding is still slim... so any help would be appreciated.

I could build an executable, and I know there are nicer ways to do things than batch files, but at least for this task I'm trying to keep it simple by using a batch file.

© Stack Overflow or respective owner

Related posts about dos

Related posts about batch