批处理文件“选择"语法 [英] Batch File 'Choice' syntax

查看:135
本文介绍了批处理文件“选择"语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚开始编辑脚本,我看到它使它更加动态和小巧. 我似乎发现了一个我不知道的问题. 对于输入,它有一个Choice脚本,只允许AZ + 0-9,所以我想知道,允许输入什么键,例如是否有键"ENTER"或"SHIFT"或"SPACE"的代码,或者"BACKSPACE",因为我需要所有这些,因此我可以将它们集成.

I have recently just worked on editing a script i saw to make it more dynamic and small. I seem to have found a problem which i can't figure out. For input it has a Choice script which allows only A-Z + 0-9, so i was wondering, What are the keys allowed to be entered, like is there a code for the key "ENTER" or "SHIFT" or "SPACE" or "BACKSPACE" because i need all of those so i can integrate them.

    @echo off
    color 05
    mode con cols=30 lines=8
    echo.
    echo Welcome.
    pause >nul
    cls
  :startover
    set variable=
    set counter=-1
  :password
**choice /c:abcdefghijklmnopqrstuvwxyz0123456789 /n /m "Type password please:%variable%"
    echo.**
    echo %ERRORLEVEL%

call :variable

if errorlevel 255 goto Error
if errorlevel 36 set letter=%letter%9&goto PWcheck
if errorlevel 35 set letter=%letter%8&goto PWcheck
if errorlevel 34 set letter=%letter%7&goto PWcheck
if errorlevel 33 set letter=%letter%6&goto PWcheck
if errorlevel 32 set letter=%letter%5&goto PWcheck
if errorlevel 31 set letter=%letter%4&goto PWcheck
if errorlevel 30 set letter=%letter%3&goto PWcheck
if errorlevel 29 set letter=%letter%2&goto PWcheck
if errorlevel 28 set letter=%letter%1&goto PWcheck
if errorlevel 27 set letter=%letter%0&goto PWcheck
if errorlevel 26 set letter=%letter%z&goto PWcheck
if errorlevel 25 set letter=%letter%y&goto PWcheck
if errorlevel 24 set letter=%letter%x&goto PWcheck
if errorlevel 23 set letter=%letter%w&goto PWcheck
if errorlevel 22 set letter=%letter%v&goto PWcheck
if errorlevel 21 set letter=%letter%u&goto PWcheck
if errorlevel 20 set letter=%letter%t&goto PWcheck
if errorlevel 19 set letter=%letter%s&goto PWcheck
if errorlevel 18 set letter=%letter%r&goto PWcheck
if errorlevel 17 set letter=%letter%q&goto PWcheck
if errorlevel 16 set letter=%letter%p&goto PWcheck
if errorlevel 15 set letter=%letter%o&goto PWcheck
if errorlevel 14 set letter=%letter%n&goto PWcheck
if errorlevel 13 set letter=%letter%m&goto PWcheck
if errorlevel 12 set letter=%letter%l&goto PWcheck
if errorlevel 11 set letter=%letter%k&goto PWcheck
if errorlevel 10 set letter=%letter%j&goto PWcheck
if errorlevel 9 set letter=%letter%i&goto PWcheck
if errorlevel 8 set letter=%letter%h&goto PWcheck
if errorlevel 7 set letter=%letter%g&goto PWcheck
if errorlevel 6 set letter=%letter%f&goto PWcheck
if errorlevel 5 set letter=%letter%e&goto PWcheck
if errorlevel 4 set letter=%letter%d&goto PWcheck
if errorlevel 3 set letter=%letter%c&goto PWcheck
if errorlevel 2 set letter=%letter%b&goto PWcheck
if errorlevel 1 set letter=%letter%a&goto PWcheck
goto Error
:pwcheck
echo.
echo Your letters are %letter%
if %counter%==10 goto finish
goto password
:variable
set /a counter=%counter%+1
if %counter%==0 set variable=
if %counter%==10 goto finish
set variable=%variable%*
goto :eof
cls
:finish
echo Your password is %letter%
pause>nul
cls
choice /t 10 /c yn /d y /m "Start over "
if errorlevel 2 cls&echo CLOSING&exit
if errorlevel 1 goto startover
:Error
  title Test v1.1 *** Error *** %time%
  echo Error: Critical Error.
  pause >nul
goto :eof
exit

如果您有任何想法或建议使它起作用,以便允许任何长度,请告诉您.这似乎是最好的主意.

If you have any ideas or suggestions to make it work so that any length is allowed, please do tell. This seems to be the best idea.

推荐答案

选择非常有限,因此您需要其他解决方案.

Choice is very limited, so you need another solution.

一种方法是使用自己的函数,该函数使用xcopy作为键输入.

One way is to use an own function which uses xcopy for the key input.

批处理-用户输入的颜色

:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
set "key=%key:~-1%"
exit /b

xcopy/L/W尝试将批处理文件复制到其自身. /W是重要的选项,它需要在复制过程开始之前按一下键,然后显示该键.
复制本身总是失败,因为文件无法复制到自身.
因此,错误流使用2>NUL重定向到NUL.

The xcopy /L /W tries to copy the batch-file to itself. The /W is the important option, it requires a key press before the copy process starts, and the key is shown.
The copy itself fails always, as the file can't copy to itself.
Therefor the error stream is redirected to NUL with 2>NUL.

FOR/F捕获带有击键的完整行. 并且set "key=%key:~-1%"仅选择行中的最后一个字符.

The FOR /F captures the complete line with the keystroke. And the set "key=%key:~-1%" picks only the last character from the line.

好处是xcopy可以接受并显示几乎所有键(F1 .. F12和其他键仍然失败),甚至可以通过这种方式检测到Backspace和Enter.

The good thing is that xcopy accepts and shows nearly all keys (F1 .. F12 and some other still fails), even Backspace and Enter can be detected this way.

这篇关于批处理文件“选择"语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆