JSI Tip 9692. How can a batch script determine if a variable or parameter is an integer?


An integer is a whole number, like 0, or 9, or 1234.

The basic syntax used to determine if a variable or parameter is an integer is:

if VariableOrParameter EQU +VariableOrParameter CommandToExecuteIfInteger

Samples:
if %1 EQU +%1 @echo %1 is an integer.

if %1 NEQ +%1 goto error

if %var% EQU +%var% @echo Variable var contains an integer.

For /f "Tokens=*" %%a in ('type %1') do (
 if %%a NEQ +%%a @echo Line containing \{%%a\} in file %1 is NOT an integer.
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish