1

My batch informs the user when the value entered is wrong and offers two options: retype the value or exit the program.

@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 from the screen and the user can re-enter the value without the error message.

How can I clear the message sent by the echo command without having to reload the same screen?

EDITED:
Script changed according to @iTwasnTme answer, it's much better, but the input field keeps duplicating on the screen and the color does not return to white.

Changed script:

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

Screen: enter image description here

Clamarc
  • 663

1 Answers1

2

Try this adaptation of this LotPings answer to a question very similar to yours

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

enter image description here


  • See an adaptation of a code (lines 1-40) that uses menu using the currently posted answer..
@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 (^&lt;con: findstr /b &quot;%%t.%%b&quot; ^&lt;&quot;%~f0&quot;)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 );=

  • And so you can see where the Tab, Backspaces and CRLF in code:

enter image description here


  • Output result with menu:

enter image description here



Additional resources:

Io-oI
  • 9,237