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 /?