Batch File input validation - Make sure user entered an integer

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

    You may use ReadFormattedLine subroutine for all kind of formatted input. For example, the command below read a number of up to 5 digits:

    call :ReadFormattedLine UserInput="#####" /M "Please Enter a Number: "
    

    This subroutine is written in pure Batch so it does not require any additional program, and it allows several formatted input operations, like read passwords. In previous example, the subroutine reads a number of up to 5 digits. You may download ReadFormattedLine subroutine from Read a line with specific format.

    0 讨论(0)
  • 2020-12-03 15:32

    If you want some sort of a loop and default set up for that particular question, then here's my method for doing this.

    Notes on the code within.

    @echo off
    setlocal EnableDelayedExpansion
    set "ans1_Def=2"
    
    :Q1
    set /p "ans1=Opt 1 of 1 [Value 1-5 / Default !ans1_Def!]: "
    
    :: If not defined section. This will use the default once the ENTER key has been
    :: pressed and then go to :Q2.
    if not defined ans1 (
      echo/ & echo ENTER hit and the default used. Default is still: !ans1_Def! & echo/
      set "ans1=!ans1_Def!" && goto :Q2 )
    
    :: This section will check the validity of the answer. The "^[1-5]$" will work
    :: for only numbers between one and five in this example but this can be changed
    :: to pretty much suit the majority of cases. This section will also undefine
    :: the ans1 variable again so that hitting the ENTER key at the question
    :: will work.
    echo %ans1%|findstr /r /c:"^[1-5]$" >nul
    if errorlevel 1 (
      echo/ & echo At errorlevel 1. Wrong format used. Default is still: !ans1_Def! & echo/
      set "ans1=" && goto Q1
      ) else ( echo Correct format has been used. %ans1% is the one. && goto :Q2 )
    
    :Q2
    echo/
    echo -----------------------------
    echo/
    echo Now at the next question
    echo !ans1!
    echo/
    pause
    exit
    
    0 讨论(0)
  • 2020-12-03 15:33

    You might also like this one - it's short and easy. This one use the multiplication trick to set TestVal. Comparing TestVal against UserInput allows all numeric values to get through including zeroes, only non-numerics will trigger the else statement. You could aslo set ErrorLevel or other variables to indicate a failed entry

    @ECHO OFF
    SET TestVal=0
    
    SET /P UserInput=Please Enter a Number:
    SET /A TestVal="%UserInput%"*1
    
    If %TestVal%==%UserInput% (
       ECHO You entered the number %TestVal%
       ) else ECHO UserInput "%UserInput%" is not a number
    
    GOTO EOF
    
    :EOF
    
    0 讨论(0)
  • 2020-12-03 15:35

    This is the same idea as that of Johannes.. SET /A sets a numeric value. If the input is not a number, it changes it to 0. That's what you can exploit here to do your check.

    @ECHO OFF
    SET /P UserInput=Please Enter a Number:
    IF %UserInput% EQU 0 GOTO E_INVALIDINPUT
    
    SET /A UserInputVal="%UserInput%"*1
    IF %UserInputVal% GTR 0 ECHO UserInput "%UserInputVal%" is a number
    IF %UserInputVal% EQU 0 ECHO UserInput "%UserInputVal%" is not a number
    GOTO EOF
    
    :E_INVALIDINPUT
    ECHO Invalid user input
    
    :EOF
    

    As an alternative, you could always create a little javascript file and call it from your batchfile. With parseInt() you could force the input to be an integer, or you could roll your own function to test the input.

    Writing the javascript is just as fast as the batchfile, but it's much more powerful. No IDE or compiler required; notepad will do. Runs on every windows box, just like your batchfiles. So why not make use of it?

    You can even mix batchfiles and javascript. Example:

    contents of sleep.js:

    var SleepSecs=WScript.Arguments.Item(0);
    WScript.Sleep(SleepSecs*1000)
    

    contents of sleep.cmd:

    cscript /nologo sleep.js %1
    

    You can now call this from a batchfile to make your script sleep for 10 seconds. Something like that is difficult to do with just a plain batchfile.

    sleep 10
    
    0 讨论(0)
提交回复
热议问题