如何清除 echo 命令发送的消息

如何清除 echo 命令发送的消息

当输入的值错误时,我的批处理会通知用户并提供两个选项:重新输入值或退出程序。

@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
)

当选择重新输入选项时,我设法使消息消失的唯一方法是重新加载相同的屏幕,因为当重新加载屏幕时,CLS 命令会清除屏幕上的消息,然后再次加载屏幕,因此消息会从屏幕上消失,用户可以重新输入值而不会出现错误消息。

如何才能清除 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
    );=

屏幕: 在此处输入图片描述

答案1

尝试一下这个改编版LotPings 回答问题和你的很相似

@echo off

setlocal enabledelayedexpansion
for /f skip^=4 %%e in ('echo;prompt $E^|cmd.exe')do set "_$E=%%~e"

%:^(
set /p "_Value=!_$E![37;1mEnter the value:!_$E![0m "

if !_Value! gtr 10 =;(
     set /p "r=!_$E![7;31mInvalid value^! Do you want to re-enter the value? [y=yes/n=exit]!_$E![0m "
     if /i "!r!" == "y" <nul set /p "'=!_$E![1F!_$E![0J!_$E![1F!_$E![0J!_$E![37;1m" & goto %:^(
     color & set /p "'=!_$E![0m "<nul & endlocal & goto :eof
    );=

在此处输入图片描述


  • 查看使用菜单和当前发布的答案的代码改编版(1-40 行)。
@goto %:^/
        ___  _____
      .'/,-Y"     "~-.    
      l.Y             ^.    Script: Manutenção
      /\               _\_     Author: Leticia
     i            ___/"   "\     Create: 26/04/20
     |          /"   "\   o |    Update: 26/06/20
     l         ]     o |__./     XGHv01: 25/05/21
      \ _  _    \.___./    "~\
       X \/ \            ___./
      ( \ ___.   _..--~~"   ~`-.
       ` Z,--   /               \
         \__.  (   /       ______)
           \   l  /-----~~" /      -Row
            Y   \          /
            |    "x______.^
            |           \
            j            Y
     ------------------------------------------------------
     | Menu Principal           // eXtreme Go Horse v.01 d;)
     ------------------------------------------------------
      1 - Limpar Cache
      2 - Limpar Disco
      3 - Escanear Sistema
      4 - Corrigir Sistema Windows >=8
      5 - Verificar Estado do(s) HD(s)
      6 - Reparar Cache Icones/Thumbs.db
      7 - Limpar Fila/Spool de Impressão
      8 - Renovar DHCP/IP e Limpar Cache DNS
      9 - Remover Arquivos Tempórarios/Updates
      0 - Sair

%:^/
@echo off
title<nul & title .\%~nx0 & setlocal EnableDelayedExpansion & %ComSpec% /c color 06
%__AppDir__%chcp.com 65001>nul && %ComSpec% /ccolor 06 & %__AppDir__%mode.com 72,37
for /f usebackq^delims^=^  %%t in =;(`%__AppDir__%robocopy.exe /L .\. .\. /njh /njs
  =;`)do for /f usebackskip^=4 %%b in (`echo\prompt;$h^|%ComSpec%  =;`)do call && ^
for /f tokens^=*useback %%i in (`^<con: findstr /b "%%t.%%b" ^<"%~f0"`)do echo;%%~i

rem./ & cd. & for /f skip^=4 %%e in ('echo;prompt $E ^| cmd.exe ')do set "_$E=%%~e"
%__AppDir__%timeout.exe 02 >nul | echo\

%:^(
color & set /p "_Value=Enter the value: "

if !_Value! gtr 10 =;(
     set /p "r=!_$E![7;31mInvalid value^! Do you want to re-enter the value? [y=yes/n=exit]!_$E![0m "
     if /i "!r!" == "y" <nul set /p "'=!_$E![1F!_$E![0J!_$E![1F!_$E![0J" & goto %:^(
     set /p "'=!_$E![1F" <nul & endlocal & goto :eof
    );=
  • 因此您可以看到代码中的TabBackspaces和:CRLF

在此处输入图片描述


  • 带菜单的输出结果:

在此处输入图片描述



其他资源:

相关内容