Starting demo.exe
without input version string is not possible with the provided code. This would happen only if the posted code is inside a command block starting with (
and ending with matching )
. In this case delayed expansion would be needed as described by help of command SET output on running in a command prompt window set /?
. The Windows command processor cmd.exe
parses the entire command block before executing the command (usually IF or FOR) making use of this command block. Every %variable%
reference in the entire command block is replaced by current value of referenced environment variable during parsing the command block as described by How does the Windows Command Interpreter (CMD.EXE) parse scripts? and as it can be seen on debugging a batch file. For environment variables not defined during parsing the command block the finally executed command lines contain nothing instead of %variable%
.
Let us assume the code is not inside a command block which is usually possible as there is command GOTO to continue execution of a batch file below a line starting with a colon and so use a design which avoids usage of command blocks at least for IF conditions.
Here is an improved version of provided code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Delete environment variable Version before each user prompt. The
rem user is prompted until a valid version string is input by the user.
:EnterVersion
set "Version="
set /P Version="Please enter the version: "
rem Has the user input a string at all?
if not defined Version goto EnterVersion
rem Remove all double quotes from user input string.
set "Version=%Version:"=%"
rem Is there no version string anymore after removing double quotes?
if not defined Version goto EnterVersion
rem Contains the version string any other character than digits and dots?
for /F delims^=0123456789.^ eol^= %%I in ("%Version%") do goto EnterVersion
rem Start demo.exe with the first argument -v and second argument being the
rem input version string as new process with window title Demo in case of
rem demo.exe is a console application in user's documents directory.
start "Demo" /D"%USERPROFILE%\Documents" demo.exe -v %Version%
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
start /?
See also