0

Hello im currently trying to making a little program to practice and currently have this small issue. everything else works fine but in the case that the user makes a wrong choice the program gives the error message but it still opens one of the programs either way here's the code:

@echo off
echo Hola podrias decirme cual es tu nombre?
SET /p USERNAME=Entra tu nombre:

echo Hola %username% que programa te gustaria usar?:
echo.
echo 1. Disk Cleanup Manager
echo 2. Microsoft Paint
echo.
echo Autor:
echo.`your text`
set /p choice=Escribe el numero para abrir la aplicacion:
if %choice%== set choice=%choice:~0,1%
if %choice%==1 goto diskclean
if %choice%==2 goto paint
echo %choice% selecion no validad trata de nuevo
pause


:diskclean
start cleanmgr
goto end

:paint
start mspaint
goto end

searched the web but found no help

  • 1
    Don't overwrite the system's environment variable UserName. Also do not use the set command with /p instead of using the choice command utility. – Compo Feb 09 '23 at 21:54
  • 1
    Please consider `choice` which is designed for this task. Use the search facility for [batch-file] choice eg Gerhard's example https://stackoverflow.com/a/58370914/2128947 or see the documentation - `choice /?` from the prompt. Since `choice` is a batch keyword, it's not a good idea to use it as a variable-name. – Magoo Feb 09 '23 at 21:58
  • 2
    Batch has no concept of "sections", "functions", "procedures" or "paragraphs". A label is simply a reference point. Execution does not stop when a label is reached, it simply continues through, line by line, until it reaches end-of-file, a **CALL** , a **GOTO** or an **EXIT**. Hence, when the `pause` is reached, pressing a key will cause the batch to step on to the next command, `start cleanmgr` after the `:diskclean` label. You would need a `goto somewhere` after the `pause` to have it go elsewhere (like to a label before `echo Hola...` for instance). – Magoo Feb 09 '23 at 22:02

1 Answers1

0

A fail-safe and secure batch file for this task would be:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Hola podrias decirme cual es tu nombre?
set "NameUser="
:UserNamePrompt
set /P "NameUser=Entra tu nombre: " || goto UserNamePrompt
set "NameUser=%NameUser:"=%"
if not defined NameUser goto UserNamePrompt

setlocal EnableDelayedExpansion & echo Hola !NameUser! que programa te gustaria usar?& endlocal
echo(
echo    1. Disk Cleanup Manager
echo    2. Microsoft Paint
echo(
if not exist %SystemRoot%\System32\choice.exe goto UseSetPrompt
:ChoicePrompt
%SystemRoot%\System32\choice.exe /C 12 /N /M "Escribe el numero para abrir la aplicacion:"
if errorlevel 2 goto Paint
if errorlevel 1 goto DiskCleanup
goto ChoicePrompt

:UseSetPrompt
set "UserChoice="
set /P "UserChoice=Escribe el numero para abrir la aplicacion: " || goto UseSetPrompt
set "UserChoice=%UserChoice:"=%"
if not defined UserChoice goto UseSetPrompt
if "%UserChoice%" == "2" goto Paint
if "%UserChoice%" == "1" goto DiskCleanup
goto UseSetPrompt

:DiskCleanup
start "" %SystemRoot%\System32\cleanmgr.exe
goto End

:Paint
start "" %SystemRoot%\System32\mspaint.exe

:End
endlocal

There should be read first the following chapters in this answer:

  • Issue 3: ECHO. could result in unwanted behavior
  • Issue 6: Batch file depends on environment defined outside

There is first defined the required execution environment completely with the first two command lines as every batch file written for usage by others should do.

There is next cleared the screen in case of a user runs this batch file from within a command prompt window instead of double clicking on the batch file in Windows File Explorer.

Please read How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? It explains how to code a set /P user prompt safe and secure and how to use the Windows command CHOICE for a choice prompt on being available as on Windows Server 2003 and Windows Vista and all newer Windows versions (and on also my Windows XP computer). The code above contains also the code for the choice prompt done with set /P in case of this batch file is run by a user on Windows XP which does not have by default the executable choice.exe.

A user of the batch file could enter as name Asterix & Obelix which should be output exactly as entered by the user and should not result in interpreting & as unconditional command operator which is the reason for using delayed variable expansion just to output the text with the user input name. There is no space between usar? and the unconditional command operator & as that space would be otherwise output by ECHO as trailing space, too.

USERNAME is a predefined Windows environment variable which should not be redefined in a batch file.

choice is the name of a Windows command. The string choice should not be used for that reason as name of an environment variable or a label although that would be possible.

Please read How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The word batch means one command line or command block after the other.

The Windows Command Processor cmd.exe processes a batch file therefore by

  • opening the batch file,
  • seeking to byte offset in batch file on which stopped reading the last time,
  • reading a line from the batch file,
  • parsing/interpreting/processing the read line,
  • finding out if more lines must be read and parsed or not and if no more lines to read,
  • remembering current byte offset in batch file,
  • closing the batch file and
  • executing the command line or series of commands on a single command line or in a command block.

There are the cmd internal commands CALL, EXIT, FOR, GOTO and IF to change the execution order from one command line after the other to something different. A line with a label does not stop processing a batch file line by line.

Every executable called by cmd.exe during processing of the batch file is referenced with its fully qualified file name. That makes batch file execution faster as cmd.exe does not need to search in file system for the executable files using the local environment variables PATH and PATHEXT. That makes the batch file also more fail-safe as Windows users sometimes corrupt their system and/or user environment variable Path which results in batch files depending on PATH to fail running correct.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • choice /?
  • cls /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?
  • start /?
Mofi
  • 46,139
  • 17
  • 80
  • 143