Search code examples
windowsvalidationbatch-fileuser-input

Is there a way to make a one time use command/action in a windows command line? eg. Inputting a license key


The problem is basically in the title above. How do I for example enable a one time action example inputting a license key.

set /p key= "Please input your license key: "
IF [%key%]= [something] goto :start
IF [%key%]= [anything] goto :start

Is there a way to only make the code above pop up only the first time using it? and if they entered the wrong key it will just stay like that until they enter the correct one.


Solution

  • The most common method would be to use a simple GoTo loop:

    :GetKey
    ClS
    Set /P "key=Please input your license key: "
    Set "key=%key:"=%"
    (Set key) 2>NUL | %SystemRoot%\System32\findstr.exe /LX "key=CoRrEcT KeY" >NUL || GoTo GetKey
    
    :Start
    Echo You entered the correct license key [%key%].
    Pause