2017-01-03 2 views
1

Dies ist eine einfache Frage, auf die ich keine Antwort finden konnte. Ich führe ein Skript aus, das Benutzereingabe/-auswahl erfordert. Hier ist der problematische Bereich:Bat User Selection

echo W -- WATCH_SCALE 
echo E -- EARRING_SCALE 
echo R -- RING_SCALE 
echo B -- BOX 

set /P rnFunc="choose a script: " 
for %%I in (W E R B x) do if #%rnFunc%==#%%I goto assign%%I 

Das Skript funktioniert, wenn der Benutzer die richtigen Buchstaben eingibt, aber wenn Benutzer geben einen undefinierten Buchstaben wie ‚T‘ das Skript weiter auf die erste Option anstatt zu brechen. Ich möchte, dass dies nur funktioniert, wenn der Benutzer ein W, E, R oder B eingibt. Was wäre meine beste Option?

Vielen Dank.

+1

Wie wäre es mit 'goo noassign' oder' goto: eof' direkt nach der 'for' Schleife? – aschipfl

Antwort

2

Hinzufügen einer goto end direkt nach der for Anweisung. Dann machen Sie ein Etikett namens end, nachdem alle anderen Etiketten Ihr Problem lösen sollten. Grundsätzlich wird das erste gefundene Label ausgeführt, da es die for-Anweisung nicht bestanden hat.

+0

Am Ende jeder Batchdatei befindet sich eine integrierte Bezeichnung namens ': eof'; Sie müssen es nicht definieren. Sie können auch 'exit/b' anstelle von' goto eof' eingeben. –

+0

@KlitosKyriacou ist, dass ich nicht die ganze Datei sehe Ich weiß nicht, ob OP enden möchte oder etwas anderes tun. Ich meinte nur das Ende der Optionen. – MotKohn

+0

Danke für diese einfache Antwort. In diesem Fall möchte ich zu anderen Eingabeaufforderungen usw. übergehen. Anstatt also auf "Ende" zu gehen, muss das Skript in einen relevanteren Teil des Skripts verschoben werden. – Garrett

4

Sie können stattdessen einen Blick auf den Befehl CHOICE werfen.

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text] 

Description: 
    This tool allows users to select one item from a list 
    of choices and returns the index of the selected choice. 

Parameter List: /C choices  Specifies the list of choices to be created. 
         Default list is "YN". 

    /N     Hides the list of choices in the prompt. 
         The message before the prompt is displayed 
         and the choices are still enabled. 

    /CS     Enables case-sensitive choices to be selected. 
         By default, the utility is case-insensitive. 

    /T timeout  The number of seconds to pause before a default 
         choice is made. Acceptable values are from 0 to 
         9999. If 0 is specified, there will be no pause 
         and the default choice is selected. 

    /D choice  Specifies the default choice after nnnn seconds. 
         Character must be in the set of choices specified 
         by /C option and must also specify nnnn with /T. 

    /M text   Specifies the message to be displayed before 
         the prompt. If not specified, the utility 
         displays only a prompt. 

    /?     Displays this help message. 

    NOTE: The ERRORLEVEL environment variable is set to the index of the 
    key that was selected from the set of choices. The first choice listed 
    returns a value of 1, the second a value of 2, and so on. If the user 
    presses a key that is not a valid choice, the tool sounds a warning 
    beep. If tool detects an error condition, it returns an ERRORLEVEL 
    value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool 
    returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters 
    in a batch program, list them in decreasing order. 

Examples:  
CHOICE /?  
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."  
CHOICE /T 10 /C ync /CS /D y  
CHOICE /C ab /M "Select a for option 1 and b for option 2."  
CHOICE /C ab /N /M "Select a for option 1 and b for option 2." 
+0

Danke dafür, diese Lösung würde funktionieren, aber in diesem Fall werde ich mit der Antwort von @MotKohn gehen, da es einfacher zu implementieren wäre. Will in Erwägung ziehen, "Wahl" in der Zukunft zu verwenden. – Garrett