批处理文件输入验证 - 确保用户输入了一封信

时间:2013-04-09 08:49:21

标签: batch-file user-input

正如标题所示,我如何要求用户输入,如果不是字母转到:这个。 我正在制作批处理文件来搜索指定的网络驱动器并返回.exe文件列表。

I have done this but with to much code, i.e. 
If "%choice%"=="a" goto :scan
If "%choice%"=="b" goto :scan
etc.

谢谢!

3 个答案:

答案 0 :(得分:3)

@echo off
setlocal EnableDelayedExpansion
set letters=abcdefghijklmnopqrstuvwxyz
set /P drive=Enter drive:
rem Be sure that just one character is processed
set drive=%drive:~0,1%
rem Try to remove the character from letters; 
rem if the result is not equal: the character IS a letter
if "!letters:%drive%=!" neq "%letters%" goto scan

答案 1 :(得分:1)

使用VBScript可以更轻松地完成此操作,但这是一个批处理解决方案:

setlocal enabledelayedexpansion
set /p choice=
set map=abcdefghijklmnopqrstuvwxyz
for /l %%i in (0,1,25) do (
    if /i %choice% equ !map:~%%i,1! goto scan
)
exit
:scan

答案 2 :(得分:1)

@ECHO Off
SETLOCAL
SET letter=&SET /p letter="Please choose a single letter "
IF NOT DEFINED letter ECHO No choice made&GOTO :eof
ECHO "%letter%"|FINDSTR /i /b /e /r \"[a-z]\" >NUL
IF ERRORLEVEL 1 (ECHO NOT a single letter) ELSE (ECHO "%letter%" is a single letter)

几乎只有 - 如果条目包含"

,则只会失败