Having set /p ignore capital letters in batch

别来无恙 提交于 2020-01-17 01:32:06

问题


Is there any way (via tag or other wise) to have Set /p ignore capital letters in a batch script?


回答1:


I don't know about ignore, but you could convert them to lower case.

Set /P Text=Please type something: 
Set Text=%Text:A=a%
Set Text=%Text:B=b%
Set Text=%Text:C=c%
...
Echo %Text%

If you wanted to use a For command to convert to lower case:

Set Text /P=Please type something: 
For %%i In ("A=a" "B=b" "C=c" ...) Do Call Set "Text=%%Text:%%~i%%"
Echo %Text%

Or replace "A=a" with "A=", etc. to remove capitals.




回答2:


Alternatively, and depending on what you plan to do with the user input, if you want to use it in a decission, then you may use the /I switch of the IF command. See HELP IF.

Set /P TEXT=Choose an option: 
IF /I %TEXT%==A ( echo DOA
) ELSE ( IF /I %TEXT%==B ( echo DOB
) ELSE ( IF /I %TEXT%==C ( echo DOC
) ELSE ( echo DONOTHING  )))



回答3:


Upper To Lower Case:

@echo off
:main
cls
set str=
set /p str=    input(Press enter to exit):
if not defined str exit
cls
echo.
echo            Before:"%str%"
for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do call set str=%%str:%%i=%%i%%
echo ____________________________________________
echo.
echo            After:"%str%"
echo.
echo      Press Any Key To Convert again
pause>nul
goto main



回答4:


All input limitations of set /P command, including this one, may be solved if we write our own readline routine. This is the basic version:

@echo off
rem Insert next line if needed:
SETLOCAL DISABLEDELAYEDEXPANSION
set exclam=!
set caret=^^
setlocal EnableDelayedExpansion
set ascii=01234567890123456789012345678901^
 !exclam!^"#$%%^&'()*+,-./0123456789:;^<=^>?^
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]!caret!^_^
`abcdefghijklmnopqrstuvwxyz{^|}~
for /F %%a in ('echo prompt $H ^| cmd') do set BS=%%a
set lineread=
:nextkey
    getakey
    set code=%errorlevel%
    if %code% lss 32 goto checkBS
:ascii
    set char=!ascii:~%code%,1!
    colormsg 7 "!char!"
    set lineread=!lineread!!char!
    goto nextkey
:checkBS
if not %code% == 8 goto checkExtended
    if "!lineread!" == "" goto nextkey
    colormsg 7 "%BS% %BS%"
    set lineread=!lineread:~0,-1!
    goto nextkey
:checkExtended
if not %code% == 0 goto checkCR
    getakey
    goto nextkey
:checkCR
if not %code% == 13 goto nextkey
echo/

ECHO Line Read: [!lineread!]

For example, if you want to ignore capital letters on input just insert these two lines after :ascii label:

:ascii
    rem If key is between A (65) and Z (90): ignore it
    if %code% geq 65 if %code% leq 90 goto nextkey

To convert uppercase letters to lowercase ones:

:ascii
    rem If key is upcase (between A-65 and Z-90): convert it to lowcase (a-97 z-122)
    if %code% geq 65 if %code% leq 90 set /A code+=32

We may even write an asynchronous (concurrent) readline routine that allows the Batch file continue running at the same time it read the line.

Both GETAKEY.COM and COLORMSG.COM programs was previously given in this question: Batch Color per line




回答5:


Rob's site to the rescue: here are some fleshed-out examples of batch case conversion.



来源:https://stackoverflow.com/questions/7885099/having-set-p-ignore-capital-letters-in-batch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!