0

I have created a batch file which accepts a user input and then it starts an exe file with the user input as its run time argument.

@echo off

set /p version= "Please enter the version   "
ECHO version is %version%

cd %USERPROFILE%\Documents
START demo.exe -v %version%

With the above code, it simply does not start the exe. If I replace the START command with below:

START demo.exe -v 2019.1.133

and rerun the batch file, it starts the exe. Can anyone please tell me what is the error here.

Thanks

S Andrew
  • 5,592
  • 27
  • 115
  • 237

1 Answers1

0

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

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • It keeps on asking me "Please enter version" again and again, any reason why.? – S Andrew May 21 '19 at 06:24
  • If you use a batch file with just the code posted by me and you enter `2019.1.133` on prompt the input string passes the verification lines. Please read the comment lines in batch file which are the lines starting with command `rem`. But if you use the code in your batch file with other lines above and below not posted by you, then it may fail because of the reasons explained in first paragraph. Please edit your question and add your real code. See help topic [How to create a Minimal, Complete, and Verifiable example?](https://stackoverflow.com/help/mcve) – Mofi May 21 '19 at 07:54