设置 /p 批量忽略大写字母

发布于 2024-12-11 17:51:44 字数 58 浏览 0 评论 0原文

有没有办法(通过标签或其他方式)在批处理脚本中设置Set /p忽略大写字母?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜味拾荒者 2024-12-18 17:51:44

或者,根据您计划如何处理用户输入,如果您想在决策中使用它,那么您可以使用 IF/I 开关命令。请参阅如果有帮助

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  )))

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  )))
狼亦尘 2024-12-18 17:51:44

大写到小写:

@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

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
新雨望断虹 2024-12-18 17:51:44

如果我们编写自己的 readline 例程,则 set /P 命令的所有输入限制(包括这一点)都可以得到解决。这是基本版本:

@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!]

例如,如果您想忽略输入中的大写字母,只需在 :ascii 标签后插入这两行:

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

将大写字母转换为小写字母:

: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

我们甚至可以编写一个异步 (并发)readline 例程,允许批处理文件在读取行的同时继续运行。

GETAKEY.COM 和 COLORMSG.COM 程序之前都在这个问题中给出:每行批量颜色

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

溺ぐ爱和你が 2024-12-18 17:51:44

我不知道忽略,但你可以将它们转换为小写。

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

如果您想使用 For 命令转换为小写:

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

或将 "A=a" 替换为 "A=" 等。删除大写字母。

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.

冷情妓 2024-12-18 17:51:44

Rob 的网站可以提供帮助:这里有一些批量大小写转换的充实示例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文