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
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.
The parameter used is set into the
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!
The loop is done using
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.
|show 3 more comments|
This will detect if the first parameter is a valid natural number (non-negative integer).
If you want to allow quotes around the number, then
Note - leading zeros are disallowed because batch treats them as octal, so a value like
The following works very well for me.