当输入的值错误时,我的批处理会通知用户,并提供两个选项:重新输入值或退出程序。
@echo off
cls
setlocal enabledelayedexpansion
set /p _Value=Enter the value:
for /f skip^=4 %%e in ('echo;prompt $E^|cmd')do set "_$E=%%e"
if "!_Value!" gtr "10" (
echo %_$E%[31mInvalid value! Do you want to re-enter the value? (y=yes/n=exit) %_$E%[0m
IF /i "!r!"=="y" goto RELOADSAMESCREEN
goto :eof
)
The only way I managed to make the message disappear when the retype option is chosen, was to reload the same screen because when the screen is reloaded, the CLS command clears the screen with the message and then loads the screen again, so the message disappears从屏幕上,用户可以重新输入值而不会出现错误消息。
如何清除 echo 命令发送的消息而无需重新加载同一屏幕?
已编辑:根据@iTwasnTme
答案更改了脚本,它好多了,但是输入字段在屏幕上不断重复,颜色不会变回白色。
修改后的脚本:
@echo off
setlocal enabledelayedexpansion
for /f skip^=4 %%e in ('echo;prompt $E^|cmd')do set "_$E=%%e"
:RELOADSAMESCREEN
color & echo\& set /p "_Value=Enter the value: "
if !_Value! gtr 10 =;(
set /p "r=!_$E![31mInvalid value^!! Do you want to re-enter the value? [y=yes |or| n=exit] "
if /i "!r!"=="y" (set /p "'=!_$E![1F!_$E![0J!_$E![1F" <nul & goto RELOADSAMESCREEN)
color & set /p "'=!_$E![1F" <nul & endlocal & goto :eof
);=
试试这个LotPings 对与你的问题非常相似的答案的改编
Tab
,Backspaces
和CRLF
在代码中的位置:其他资源:
Dir /?
Del /?
Echo /?
Set /?
For /?
For /F /?
For /F commands
For /L /?
||
和&&
|
,<
,>
,2>
等Local Environment | Function
Setlocal
&Endlocal
If Condition() Else If Condition() Else()
cmd.exe
] 如何解析脚本