0

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

Gareth Jones
  • 1,241
  • 4
  • 17
  • 38

5 Answers5

3

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  )))
PA.
  • 28,486
  • 9
  • 71
  • 95
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
GaryNg
  • 1,103
  • 9
  • 16
  • 1
    You should switch to delayed expansion(set "str=!str:%%i=%%i!") its ~20 times faster than 'call' and its safe against special characters in the content like "you & me" – jeb Nov 05 '11 at 14:52
2

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

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
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.

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
  • Thanks! However could you make the code smaller with use of the `for` command? instead of having to have `Set Text=%Text:C=c%` for each letter. – Gareth Jones Oct 25 '11 at 05:58
  • 1
    But it doesn't work, as `Set Text=%Text:A=%` removes all `A`s and `a`s from the Text, not only the capitals – jeb Oct 25 '11 at 07:54
  • Is this with the loop? or with the first answer (the one that strips the letters away) because i know that second part, which changes all of the capital letters does work – Gareth Jones Oct 25 '11 at 20:47
  • @jeb is right. I thought it was case sensitive. So `"A=a"` is actually converting every `A` and `a` into `a`, not just the upper-case A's. – Hand-E-Food Oct 25 '11 at 22:05
  • Why does that matter, other than for speed? Unless your using a big word speed wont be effected by this that much. And I cant think why else that would be a problem – Gareth Jones Oct 26 '11 at 22:42
  • 1
    @GarethJones, it doesn't matter in your case. If you wanted to remove all upper-case letters, then this method doesn't work because it sees "A" being the same as "a". I didn't notice it when we converted to lower-case because it's hard to see the change from "a" to "a". If you want to see what I mean, try `"A=" "B=" "C=" ...` and give `a1B2c3` as the input. Ultimately, it doesn't matter in your case of converting to lower-case. – Hand-E-Food Oct 26 '11 at 22:45
  • 1
    @Hand-E-Food K, you should make a note of that in your answer, or remove it from your answer, as it could confuse those that are reading it if they try to use it themself – Gareth Jones Oct 27 '11 at 23:55
1

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

Jeremy Murray
  • 776
  • 7
  • 7