如何在选择命令中使用特殊字符-批处理文件

3 batch-file choice special-characters

我想将特殊字符添加< |到我的选择命令中,该怎么做?

这是我的旧代码:

CHOICE /C ABCDEFGHIJKLMNOPQRSTUVWXYZ0134567928 /N /M "Press 8 ...."
Run Code Online (Sandbox Code Playgroud)

我希望它是这样的:

CHOICE /C `~,.<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ0134567928 /N /M "Press 8 ...."
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢。

编辑:既然你们中的有些人可能会问为什么我需要这么多选择,所以我在这里回答。这是因为按无效的选择会给您带来我不希望听到的烦人的提示音。因此我问了这个问题。

dbe*_*ham 5

CHOICE命令仅允许使用字母数字字符以及扩展的ASCII字符,其十进制字节码在128-254之间。

我已经基于使用REPLACE的黑客编写了CHOICE的纯批处理替代品。我的:getKey例程可以接受大多数任何字符,并允许您指定允许的字符,并将捕获的字符放入您选择的变量中-比弄错ERRORLEVEL更为方便。

我最初将例程发布在http://www.dostips.com/forum/viewtopic.php?f=3&t=7396上。该链接还有一个更复杂的例程,该例程可以读取绝对任何键值,包括NULL,以及一个允许输入掩码字符串的例程(每个字符显示星号)。

以下是:getKey例程,以及显示用法的示例。/I如果您希望按键区分大小写,请卸下开关。完整的文档嵌入在脚本中。

编辑 -我已经将代码更新为2.0版,可以指定提示,并使有效的密钥列表区分大小写。唯一无法模拟的CHOICE功能是带有默认响应的超时选项。我看不到提供超时选项的任何合理方法。

@echo off
setlocal enableDelayedExpansion

set "valid=0`~,.<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ0134567928"
call :getKey /p "Press 8 ...." key valid /i
echo(
echo You pressed !key!
exit /b

::getKey  [/P "Prompt"]  KeyVar  [ValidVar [/I]]
::
:: Read a keypress representing a character between 0x00 and 0xFF and store the
:: value in variable KeyVar. Null (0x00), LineFeed (0x0A), and Carriage Return
:: (0x0D) will result in an undefined KeyVar. On Windows 10, Ctrl-Z (0x1A) will
:: also result in an undefined KeyVar. The simplest way to get an undefined
:: KeyVar is to press the [Enter] key.
::
:: The optional /P parameter is used to specify a "Prompt" that is written to
:: stdout, without a newline. Also, the accepted character is ECHOed after the
:: prompt if the /P option was used.
::
:: The optional ValidVar parameter defines the values that will be accepted.
:: If the variable is not given or not defined, then all characters are accepted.
:: If given and defined, then only characters within ValidVar are accepted. The
:: first character within ValidVar should either be 0, meaning ignore undefined
:: KeyVar, or 1, meaning accept undefined KeyVar. The remaining characters
:: represent themselves. For example, a ValidVar value of 0YN will only accept
:: uppercase Y or N. A value of 1YN will additionally accept [Enter] etc.
::
:: If ValidVar is followed by the optional /I switch, then case of standard
:: English letters is ignored. The case of the pressed key is preserved in
:: the result, but English letters A-Z and a-z are not rejected due to case
:: differences when the /I switch is added.
::
:: Any value (except null) may be entered by holding the [Alt] key and pressing
:: the appropriate decimal code on the numeric keypad. For example, holding
:: [Alt] and pressing numeric keypad [1] and [0], and then releasing [Alt] will
:: result in a LineFeed.
::
:: The only way to enter a Null is by holding [Ctrl] and pressing the normal [2]
::
:: An alternate way to enter control characters 0x01 through 0x1A is by holding
:: the [Ctrl] key and pressing any one of the letter keys [A] through [Z].
:: However, [Ctrl-A], [Ctrl-F], [Ctrl-M], and [Ctrl-V] will be blocked on Win 10
:: if the console has Ctrl key shortcuts enabled.
::
:: This function works properly regardless whether delayed expansion is enabled
:: or disabled.
::
:: :getKey version 2.0 was written by Dave Benham, and originally posted at
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=7396
::
:: This work was inspired by posts from carlos and others at
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=6382
::
:getKey
setlocal disableDelayedExpansion
if /i "%~1" equ "/P" (
  <nul set /p ^"=%2"
  shift /1
  shift /1
  set "getKey./P=1"
) else (
  set "getKey./P="
)
:getKeyRetry
(
  endlocal&setlocal disableDelayedExpansion
  (for /f skip^=1^ delims^=^ eol^= %%A in ('replace.exe ? . /u /w') do for /f delims^=^ eol^= %%B in ("%%A") do (
    endlocal
    if "%%B" equ "" (set "%1=^!") else set "%1=%%B"
    setlocal enableDelayedExpansion
  )) || (
    endlocal
    set "%1="
    setlocal enableDelayedExpansion
  )
  set "getKey./P=%getKey./P%"
  if defined %1 (set "getKey.key=!%1!") else set "getKey.key=x"
)
(
  if "!%2!" neq "" (
    if defined %1 (
      set "getKey.mask=!%2:~1!"
      if not defined getKey.mask goto :getKeyRetry
      if /i "%~3" equ "/I" (
        if "!%1!" equ "=" (
          set "getKey.mask=a!getKey.mask!"
          for /f "delims=" %%A in ("!getKey.mask!") do if /i "!getKey.mask:%%A=%%A!" equ "!getKey.mask!" goto :getKeyRetry
        ) else for /f delims^=^ eol^= %%A in ("!%1!") do if "!getKey.mask:*%%A=!" equ "!getKey.mask!" goto :getKeyRetry
      ) else (
        for /f tokens^=1*^ eol^=^%getKey.key%^ delims^=^%getKey.key% %%A in ("!getKey.mask!!getKey.mask!") do if "%%B" equ "" goto :getKeyRetry
      )
    ) else if "!%2:~0,1!" equ "0" goto :getKeyRetry
  )
  if defined getKey./P echo(!%1!
  exit /b
)
Run Code Online (Sandbox Code Playgroud)

  • @SteveFest,我想这里缺少一个'=`标记:`&lt;nul set / p =“按一个键[A |&gt;]:” (2认同)