SELECT FOR FORM - Windows package

I have this loop that repeats for every line in an external file. I would suggest the user choose in each pass, although this does not work. I believe the problem is that the GOTO team breaks the loop somehow. Any thoughts on this?

FOR /F %%i IN (%WORKDIR%\grunt-packages.ini) DO (
    CHOICE /C AN /M "Odinstalovat plugin"
    IF %ERRORLEVEL%==1 GOTO UNINSTALL
    IF %ERRORLEVEL%==2 GOTO SKIP

    :UNINSTALL
        ECHO Odstranuji %%i 
        CALL npm uninstall %%i

    :SKIP
        ECHO Preskakuji %%i
)
+5
source share
3 answers

Your calculation is correct. gotoinside loop cycles will stop the cycle. Therefore, it should be used instead call. However, the first issue with your script is the need for a delayed extension for the variable ERRORLEVEL. Whenever the variables specified in parentheses expand, use the delayed extension to get the last value.

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /F %%i IN (%WORKDIR%\grunt-packages.ini) DO (
    CHOICE /C AN /M "Odinstalovat plugin"
    IF !ERRORLEVEL!==1 CALL :UNINSTALL
    IF !ERRORLEVEL!==2 CALL :SKIP
)
ENDLOCAL
GOTO :EOF

:UNINSTALL
    ECHO Odstranuji %%i 
    CALL npm uninstall %%i
    GOTO :EOF

:SKIP
    ECHO Preskakuji %%i
    GOTO :EOF
  • goto for.
  • , , . ! %. .
+8

@ Metzger ( ), . , CALLs. , , :

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR %%i IN (A B C D) DO (
    CHOICE /C AN /M "Uninstall plugin %%i"
    IF !ERRORLEVEL!==1 (
        ECHO Uninstall %%i
    ) ELSE IF !ERRORLEVEL!==2 (
        ECHO Skip %%i
    )
)

@Metzger Windows XP :

  • GOTO :EOF ( )
  • Windows XP %%i
  • ( ) ERRORLEVEL, SKIP

:

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR %%i IN (A B C D) DO (
    CHOICE /C AN /M "Uninstall plugin %%i"
    SET OERRORLEVEL=!ERRORLEVEL!
    IF !ERRORLEVEL!==1 CALL :UNINSTALL %%i
    IF !OERRORLEVEL!==2 CALL :SKIP %%i
)
ENDLOCAL
GOTO :EOF

:UNINSTALL
    ECHO Uninstall %1
    GOTO :EOF

:SKIP
    ECHO Skip %1
    GOTO :EOF
+5

DELAYEDEXPANSION

@ECHO OFF
SETLOCAL
FOR %%i IN (A B C D) DO (
SET destcall=BADCHOICE
choice /c QJ /M "%%i - choose Q or J"
IF ERRORLEVEL 1 SET destcall=CHOSEQ
IF ERRORLEVEL 2 SET destcall=CHOSEJ
CALL CALL :%%destcall%%
)
GOTO :eof

:badchoice
ECHO bad choice
GOTO :eof

:choseq
ECHO You chose Q
GOTO :eof

:chosej
ECHO You chose J
GOTO :eof
+1
source

All Articles