Batch File input validation - Make sure user entered an integer

前端 未结 16 2415
抹茶落季
抹茶落季 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:26

    I know this is years old, but just to share my solution.

    set /p inp=Int Only : 
    :: Check for multiple zeros eg : 00000 ::
    set ch2=%inp%-0
    if %inp% EQU 0 goto :pass
    if [%inp%]==[] echo Missing value && goto :eof
    if %inp:~0,1%==- echo No negative integers! && goto :eof
    set /a chk=%inp%-10>nul
    if %chk%==-10 echo Integers only! && goto :eof
    :pass
    echo You shall pass
    :eof
    

    Tested and working on Windows 8.

提交回复
热议问题