2013-06-27 1 views
2

Ici, ça devient une question stupide mais aidez s'il vous plaît ou mon cerveau va exploser! ;)) Quelque chose de stupide que je ne comprends pas ...Fichier batch Windows - La commande GOTO est ignorée

En résumé: pourquoi après avoir choisi le numéro 7 (sortie) le ERRORLEVEL retourné est correct mais l'étiquette spécifiée par GOTO est ignorée et passe à l'étiquette allTasksReboot. ..

Ceci est mon "code" ...

@echo off 
mode con: cols=150 lines=65 
@echo ------------------------------------------------------------------------------------------------ 
@echo INSTALLATION MENU 
@echo ------------------------------------------------------------------------------------------------ 
@echo [ 1 ].All tasks - Unattended with reboot on finish 
@echo [ 2 ].All tasks - Unattended without reboot on finish 
@echo [ 3 ].All tasks except Software group - Unattended with reboot on finish 
@echo [ 4 ].All tasks except Software group - Unattended without reboot on finish 
@echo [ 5 ].All tasks except Java 7 installation - Unattended without reboot on finish 
@echo [ 6 ].Install only VNC - Unattended 
@echo [ 7 ].Exit 
@echo ------------------------------------------------------------------------------------------------ 
@echo ------------------------------------------------------------------------------------------------ 
CHOICE /C:1234567 /N /M "Choose number for installation type." 
@echo You press: %ERRORLEVEL% 
@pause 
IF ERRORLEVEL 1 GOTO allTasksReboot 
IF ERRORLEVEL 2 GOTO allTasks 
IF ERRORLEVEL 3 GOTO allExceptSoftwareReboot 
IF ERRORLEVEL 4 GOTO allExceptSoftware 
IF ERRORLEVEL 5 GOTO allTasksExceptJava 
IF ERRORLEVEL 6 GOTO onlyVnc 
IF ERRORLEVEL 7 GOTO scriptend 

:allTasksReboot 
@echo ************************************************************************** 
@echo All tasks - Unattended with reboot on finish 
@echo ************************************************************************** 
@pause 
goto scriptend 

:scriptend 
@echo Lets exit... 
@pause 

:goexit 

Répondre

4

Rappelez-vous que IF ERRORLEVEL N command test si le courant ERRORLEVEL v alue est supérieur ou égal au nombre donné, donc cette commande doit toujours être exécutée dans l'ordre décroissant des niveaux d'erreur. Vous pouvez modifier vos IF commandes de cette façon:

IF %ERRORLEVEL% EQU 1 GOTO allTasksReboot 
IF %ERRORLEVEL% EQU 2 GOTO allTasks 
..... 

Ou vous pouvez simplifier votre code beaucoup si vous changez vos étiquettes à un ceux format rigide et utilisez une commande GOTO directe:

CHOICE /C:1234567 /N /M "Choose number for installation type." 
@echo You press: %ERRORLEVEL% 
@pause 
GOTO LABEL-%ERRORLEVEL% 

:LABEL-1 allTasksReboot 
@echo ************************************************************************** 
@echo All tasks - Unattended with reboot on finish 
@echo ************************************************************************** 
@pause 
goto scriptend 

:LABEL-7 scriptend 
@echo Lets exit... 
@pause 

:goexit 
+0

je choisis votre réponse en raison de votre explication pourquoi cette « erreur » se produit. Merci aussi pour l'exemple de code trick avec label-% errorlevel% :) – binball

4

essayez ceci:

IF ERRORLEVEL 7 GOTO scriptend 
IF ERRORLEVEL 6 GOTO onlyVnc 
IF ERRORLEVEL 5 GOTO allTasksExceptJava 
IF ERRORLEVEL 4 GOTO allExceptSoftware 
IF ERRORLEVEL 3 GOTO allExceptSoftwareReboot 
IF ERRORLEVEL 2 GOTO allTasks 
IF ERRORLEVEL 1 GOTO allTasksReboot 
1

I probablement comme en utilisant un seul GOTO avec LABEL-<n> meilleurs noms, mais la solution @ Endoro est la bonne façon d'utiliser IF ERRORLEVEL. Si vous ne voulez pas avoir à commander les chèques IF, vous pouvez également utiliser la valeur de %ERRORLEVEL%:

IF %ERRORLEVEL% EQU 7 GOTO scriptend 
IF %ERRORLEVEL% EQU 5 GOTO allTasksExceptJava 
IF %ERRORLEVEL% EQU 2 GOTO allTasks 
IF %ERRORLEVEL% EQU 6 GOTO onlyVnc 
IF %ERRORLEVEL% EQU 1 GOTO allTasksReboot 
IF %ERRORLEVEL% EQU 3 GOTO allExceptSoftwareReboot 
IF %ERRORLEVEL% EQU 4 GOTO allExceptSoftware 
Questions connexes