Batch File input validation - Make sure user entered an integer

前端 未结 16 2343
抹茶落季
抹茶落季 2020-12-03 14:33

I\'m experimenting with a Windows batch file to perform a simple operation which requires the user to enter a non-negative integer. I\'m using simple batch-file techniques t

16条回答
  •  [愿得一人]
    2020-12-03 15:29

    You can also use a quite simple trick:

    echo %userinput%|findstr /r /c:"^[0-9][0-9]*$" >nul
    if errorlevel 1 (echo not a number) else (echo number)
    

    This uses findstr's regular expression matching capabilities. They aren't very impressive but useful at times.

提交回复
热议问题