我有一个批处理文件来帮助客户自动备份他们的文件,但是它们不能对它们编辑友好,所以我打算让客户输入一个字母作为备份的目标驱动器。
问题是,我不知道如何将他们的输入限制为只有一个字符而只有a-z
我已阅读此资源> Batch File input validation - Make sure user entered an integer。但仍然无法找出任何解决方案
问题是,我们真的可以限制输入长度和验证吗?是否有任何参考可以用于此事?
这是我的代码
:init
@echo off
color 02
REM -- set vars
set tgl=%DATE:~0,2%
set bln=%DATE:~-7,2%
set thn=%DATE:~-4,4%
set jam=%TIME:~0,2%
if "%time:~0,1%"==" " set jam=0%TIME:~1,1%
set menit=%TIME:~3,2%
set detik=%TIME:~6,2%
echo Under which drive will you save the backup? ( one letter only, a-z )
set /p drive=
pause
cls
:stopsvc
net stop svc01
cls
echo stop service [OK]
echo copy files [ ]
echo start service [ ]
echo archiving [ ]
echo.
REM -- start backup
echo.
:copy
echo copying ...
xcopy /e /y c:\docs %drive%:\data\docs\
cls
echo stop service [OK]
echo copy files [OK]
echo start service [ ]
echo archiving [ ]
echo.
echo.
:startsvc
echo starting MySQL service...
net start svc01
if errorlevel = 1 goto svcfail
echo.
echo done
cls
echo stop service [OK]
echo copy files [OK]
echo start service [OK]
echo archiving [ ]
echo.
echo.
echo backup successfully created
echo now archiving ...
echo.
:archive
cd "%drive%:\data"
ren "docs" "BACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%"
echo archiving - OK
cls
echo stop service [OK] > "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo copy files [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo start service [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo archiving [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo.
echo.
echo buhbye
echo copyright(c) [SC] 2010-2011
pause
goto eof
:svcfail
echo cannot start svc01 service > "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo start it manually through services.msc >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo.
echo.
echo stop service [OK]
echo copy files [OK]
echo start service [FAIL]
echo archiving [ ]
pause
goto archive1
:archive1
cd "%drive%:\data"
ren "docs" "BACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%"
echo archiving - OK
cls
echo stop service [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo copy files [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo start service [FAIL] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo archiving [OK] >> "logBACKUP-[%jam%.%menit%]-%tgl%-%bln%-%thn%.txt"
echo.
echo.
echo all done~!
pause
goto eof
:eof
exit
答案 0 :(得分:4)
以下使用基本cmd语法。它将循环,直到用户输入一个小写字符。
:choice
set /p choice=Under which drive will you save the backup? ( one letter only, a-z )
if not "%choice:~1,1%"=="" goto choice
if "%choice%" lss "a" goto choice
if "%choice%" gtr "z" goto choice
set drive=%choice%
答案 1 :(得分:1)
使用原始BATCH命令无法做到这一点;但是,您可以使用第三方程序。例如,下面的批处理文件创建GETKEY.COM文件:
@ECHO OFF
REM CREATE THE GETKEY.COM AUXILIARY FILE
(
ECHO E100
ECHO B4 08 CD 21 B4 4C CD 21
ECHO RCX
ECHO 8
ECHO W
ECHO Q
) | DEBUG GETKEY.COM > NUL
您必须拥有DEBUG.COM才能实现此目的。 GETKEY读取一个键并在ERRORLEVEL中返回其ASCII码,键读取不会回显到屏幕上。您必须操纵返回的值以检查是否在所需范围内,然后将其转换为等效字母:
set aLetter=97
set zLetter=122
set lowcaseLetters=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
REM Read a key and check that it is in range
:readKey
getkey
set key=%errorlevel%
if %key% LSS %aLetter% goto readKey
if %key% GTR %zLetter% goto readKey
REM Convert ASCII code to an index between 1 and 26
set /A index=key-aLetter+1
REM Get the letter and echo it in the screen
for /F "tokens=%index%" %%a in ("%lowcaseLetters%") do set letter=%%a
echo %letter%
你甚至可以用这种方式处理功能和特殊键......
答案 2 :(得分:1)
如果你使用的是Vista或更高版本,你可以使用CHOICE命令(输入'CHOICE /?'作为文档)默认情况下,CHOICE不区分大小写,我觉得这对你的情况是好事。
setlocal enableDelayedExpansion
set "choices=abcdefghijklmnopqrstuvwxyz"
echo Under which drive will you save the backup? ( one letter only, a-z )
choice /c %choices% /n
set /a "n=%errorlevel%-1"
set "drive=!choices:~%n%,1!"
以下是Giles Arcas策略的后置验证,允许大写和小写。它也更健壮,因为它可以正确处理空字符串。这应该适用于XP及更高版本。
setlocal enableDelayedExpansion
set "choices=abcdefghijklmnopqrstuvwxyz"
:select
echo Under which drive will you save the backup? ( one letter only, a-z )
set "drive="
set /p "drive="
for /l %%N in (0 1 25) do if /i "!choices:~%%N,1!"=="%drive%" goto :continue
goto :select
:continue
以下是使用FINDSTR正则表达式验证条目
的另一种变体setlocal enableDelayedExpansion
:select
echo Under which drive will you save the backup? ( one letter only, a-z )
set "drive="
set /p "drive="
echo !drive!|findstr /r /i "^[abcdefghijklmnopqrstuvwxyz]$" >nul || goto :select
如果用户输入"&"&
答案 3 :(得分:0)
您可以对所有类型的格式化输入使用ReadFormattedLine子例程。例如,下面的命令只读了一个字母:
call :ReadFormattedLine drive="L" /M "Under which drive will you save the backup? "
这个子程序是用纯批处理编写的,所以它不需要任何额外的程序,它允许几个格式化的输入操作,如读取密码。在前面的示例中,子程序在用户按下字母后自动继续,因此不需要Enter键。您可以从Read a line with specific format下载ReadFormattedLine子例程。