카테고리 없음

choice 예제

쓰타킹 2022. 10. 13. 11:06

CHOICE--Examples

What the user sees when you use CHOICE in a batch file

If you use the following syntax in a batch file,

    choice /c:ync

the user sees the following when CHOICE is started:

    [Y,N,C]?

If you add text to the syntax,

    choice /c:ync Yes, No, or Continue

the user sees the following when CHOICE is started:

    Yes, No, or Continue [Y,N,C]?

What the user sees if you leave out a prompt

If, as in the following example, you use the /N switch to leave out the     
prompt in a batch program,

    choice /n Yes, No, or Continue?

the user sees only the text you specified when CHOICE is started:

    Yes, No, or Continue?

What the user sees if you use the T switch

If you use the following syntax in a batch program,

    choice /c:ync /t:n,5

the user sees the following when CHOICE is started:

    [Y,N,C]?

If, after 5 seconds, the user hasn't pressed a key, CHOICE chooses N and
returns an ERRORLEVEL value of 2. If the user presses a key before 5
seconds, CHOICE returns the value corresponding to the user's choice.

To have the option of defragmenting drive C when you start your computer,
you could add the following lines to your AUTOEXEC.BAT file:

    choice Defrag drive /ty,5
    if errorlevel 2 goto SkipDefrag
    defrag c:
    :SkipDefrag

If you press N within 5 seconds, DEFRAG will not run and CHOICE returns an
ERRORLEVEL value of 2. If you do not press N within 5 seconds, or if you
choose Y, DEFRAG is run on drive C.

Using CHOICE in a batch program

The following batch program demonstrates using the CHOICE option to select
one of three programs: MS-DOS Editor, Microsoft Anti-Virus, or Microsoft
Backup.

Notice that the IF ERRORLEVEL statements are listed in decreasing order.
MS-DOS will consider the IF statement true if the ERRORLEVEL parameter
returned by CHOICE is greater than or equal to the parameter specified in
the IF command.

    @echo off
    cls
    echo.
    echo A   Microsoft Editor
    echo B   Microsoft Anti-Virus
    echo C   Microsoft Backup
    echo.
    choice /c:abc Choose an option
    if errorlevel 3 goto MSBackup
    if errorlevel 2 goto Msav
    if errorlevel 1 goto Edit

    :Edit
    edit
    goto End

    :Msav
    msav
    goto End

    :Msbackup
    msbackup
    goto End

    :End


Input any number using choice.com:

@echo off
:Choose_i
    cls
    choice/n/c1234567890- [%i%]. Choose an integer, Press Esc or - (minus:)
    if errorlevel 12 if '=='%i% for %%v in (cls goto:Escaped) do %%v
    if not errorlevel 10 for %%i in ( 1 2 3 4 5 6 7 8 9 ) do if errorlevel %
%i set i=%i%%%i
    if not errorlevel 11 for %%v in ( if goto:Choose_i ) do %%v errorlevel 1
0 set i=%i%0
    if errorlevel 12 set i=
    if '==%i%' goto Choose_i
    for %%v in (echo.[%i%]. pause) do %%v..
    for %%v in (set: goto:Choose_) do %%vi=
:Escaped

  Here's a better example,
  (two lines got wrapped:)

    @echo off
    echo. Input the password or press the [F1] key twice:
    choice /n/c:;0123456789'''''''ABCDEF > nul
    for %%v in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do if errorlevel }%%v set X
=%%v
    choice /n/c;0123456789'''''''ABCDEF %X%
    for %%v in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do if errorlevel }%%v set X
=%X%%%v
    ECHO.
    ECHO. (Your input) = (%X%)




Bennylevel error checking

Examples:

@echo off
:test!  (21 = ASCII()), (27 = ASCII())
    set drive=
    choice /n/cABCDEFGHIJKLMNOPQRSTUVWXYZ Choose a drive letter:
    if not errorlevel 002 for %%v in ( cls goto:ESCaped! ) do %%v
    set drive=Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
    for %%v in (%drive%) do if not errorlevel %%v set drive=%%v:
    echo ------------------- %drive%
    goto test!
:ESCaped!


@echo off
    choice /n/c'01234567899999999ABCDEF Input a hex number. Range(00;FF):
    cls
    for %%v in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do if errorlevel }%%v set v
=%%v
    choice /n/c'01234567899999999ABCDEF %v%
    for %%v in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do if errorlevel }%%v set v
=%v%%%v

    for %%v in (echo echo.%v% echo pause cls REM:%0) do %%v.


@echo off
    choice /n/c'01234 Input a 2 digit number in range of 00 and 49:
    for %%v in (cls set) do %%v v=0
    for %%v in (1 2 3 4) do if errorlevel }%%v set v=%%v
    choice /n/c'0123456789 %v%
    for %%v in (0 1 2 3 4 5 6 7 8 9) do if errorlevel }%%v set v=%v%%%v
    for %%v in (echo echo.%v% echo pause cls) do %%v.


:: Returns ASCII values:
:: For example, to test using an ASCII 5 char, hold [Alt] key and press [5]
::
@echo off
    echo Input character: ASCII(Range(240;35)), press Ctrl+Z (or F6), Enter:
    @   copy con A.tmp >nul
    if not exist A.tmp for %%v in ( cls goto ) do %%v eof

    find: " "  < A.tmp >nul
    if not errorlevel 1 for %%v in (set:v goto:end rem:TAB) do %%v=009
    find: "þ"  < A.tmp >nul
    if not errorlevel 1 for %%v in (set:v goto:end rem:þ  ) do %%v=254
    echo þ>> A.tmp  %[-- avoiding freeze of choice.com --]%
    :: ASCII 254 char is also used to substitute "trouble-characters":

    :: 240;35   240       250     0        10        20        30
    ::            0123456789012345012345678901234567890123456789012345
    choice /s/n/cþðñòóôõö÷øùúûüýþÿþþþþþþ þþþþþþ!"#<A.tmp
    if not errorlevel 2 goto msg
    for %%v in (0 1 2 3 4 5 6 7 8 9) do if errorlevel }%%v set v=24%%v
    for %%v in (0 1 2 3 4 5        ) do if errorlevel ~%%v set v=25%%v
    for %%v in (0 1 2 3 4 5 6 7 8 9) do if errorlevel e%%v set v=00%%v
    for %%v in (0 1 2 3 4 5 6 7 8 9) do if errorlevel f%%v set v=01%%v
    for %%v in (0 1 2 3 4 5 6 7 8 9) do if errorlevel g%%v set v=02%%v
    for %%v in (0 1 2 3 4 5        ) do if errorlevel h%%v set v=03%%v
:end
    for %%v in (echo echo.Result:ÿ%v% echo pause erase:A.tmp ) do %%v.
    for %%v in (set:v cls goto:eof) do %%v=
:msg
    echo Out of range
    echo or the ASCII value are one of those: 0 3 6 8 10 13 16 19 26 27 32
    for %%v in (echo. pause cls %0) do %%v
:eof


For more examples, please follow this link.
    Definition of BENNYLEVEL

@echo off
    for %%v in (cls echo echo set:n) do %%v=
    set v=if errorlevel greater than
    strings e=ask ÿErrorlevel value to comparison: "%v%/equal VALUE": ,3
    if %e%'==' %0
    strings t=ask ÿASCII value of a second char T: "%v% or equal XT": ,3
    if not %t%'==' set n= :: ::
%n%strings t=ask ÿOK! Input a second character T: "%v% or equal XT": ,1
%n%strings t=val %t%
    if %t%'==' %0
    strings ten[x]=add 272, %e%
    strings ten[x]=sub %ten[x]%, %t%
    strings v=right %ten[x]%, 1
    strings v=parse . 768 . 256 . 1024 . 512 .,%v%,
    if %v%'==.' goto e
    strings ten[x]=add %ten[x]%, %v%
    strings v=div %ten[x]%, 1280
    if not %v%==0 strings ten[x]=sub %ten[x]%, 1280
    strings ASCII[X1]=div %ten[x]%, 10
    strings ASCII[X2]=add %ASCII[X1]%, 128
    strings X1=char %ASCII[X1]%
    strings X2=char %ASCII[X2]%
    echo.
    strings n=char 32,32,32,32,32,32,32,32,32,32
    echo %n% Solution:
    echo %n% x1: [ ASCII(%X1%) ]  =  [ %ASCII[X1]% ]
    echo %n% x2: [ ASCII(%X2%) ]  =  [ %ASCII[X2]% ]
    echo.
    echo %n% 10(x -48) +(t -48) = 256n +e
    echo %n% 10(%ASCII[X1]% -48) +%t% -48 = 256n +%e%
    echo %n% 10(%ASCII[X2]% -48) +%t% -48 = 256n +%e%
    echo.
    echo %n% %n% IF ERRORLEVEL %e%
    strings T=char %t%
    for %%v in (%X1% %X2%) do echo %n% %n%=IF ERRORLEVEL %%v%T%
:e msg
    if %v%==. for %%v in (echo echo.ÿNoÿSolution) do %%v.
:end
    for %%v in (e t v ten[x] n ASCII[X1] X1 ASCII[X2] X2) do set %%v=
    for %%v in (echo. echo. pause cls) do %%v

Using CHOICE with special keys

Example:  CHOICE /n/s/c:qQ  Hit the [Page Down] key or press [q] to quit:

The following is the list of those special keys:

   27 [Esc]            08 [Back Space]

    R [Insert]          G [Home]            I [Page Up]
    S [Delete]          O [End]             Q [Page Down]

                        H [UP Arrow]
    K [Left Arrow]      P [Down Arrow]      M [Right Arrow]

    ; [F1]              = [F3]
    ? [F5]   @ [F6]     A [F7]     B [F8]
    C [F9]   D [F10]  133 [F11]  134 [F12]

Note: The numbers; 27, 08, 133 and 134 are ASCII values.

The following batch file creates this syntax for CHOICE:

    @echo off
    echo e0100'@echo'20'off'D''A'choice'20''2F'n'2F's'2F> %temp%.\~test.bat
    echo f117L16'c'1B''8'RSGOIQPKMH'3B''3D''3F'@ABCD'85>> %temp%.\~test.bat
    for %%v in (e012D''86''D''A rcx 30 w q) do echo %%v>> %temp%.\~test.bat
    type %temp%.\~test.bat | debug %temp%.\~test.bat
    echo if not errorlevel 2 goto:Esc (that key press.)>> %temp%.\~test.bat
::for %%v in ( 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ) do
::if errorlevel %%v %comspec%/cECHO %%v
    for %%v in (echo. pause cls goto:Begin) do echo %%v>> %temp%.\~test.bat
    echo :: Create a menu along with the missing labels>> %temp%.\~test.bat
    edit %temp%.\~test.bat