0
@echo off
start cmd /V:ON /k "set /P id="Enter yes or no: "  && echo !id! && if !id!==yes (exit /b 0) else (exit /b 5)"`

Current result:

Enter yes or no: no
echo %errorlevel%
0

I have tried with both yes and no option, but after execution of code its not sending exit code 5.

Any solution for this problem?

1
  • Well, a batch script (run by cmd.exe) that runs the start command to start another cmd.exe instance to run further commands – is this really necessary? Commented Dec 14, 2020 at 15:11

3 Answers 3

1

I'm not sure exactly how you're trying to implement this, but it seems to me as if one of the following methods using the choice command utility should work for you.

Opening a cmd window for it to run inside:

%SystemRoot%\System32\cmd.exe /D/Q/C "%SystemRoot%\System32\choice.exe&If ErrorLevel 2 (Exit 5)Else Exit 0"

Running it in the same cmd window:

@%SystemRoot%\System32\choice.exe&If ErrorLevel 2 (Exit /B 5) Else Exit /B 0
Sign up to request clarification or add additional context in comments.

2 Comments

actually i am running this script from robot framework, i want to open terminal and want to take input, based on that input and status code i will do further operation in robot script, thats why i have used that start cmd. any solution for this?
Please don't repeat comments, I did read this same information under the other answer. I do not care anything about robot framework or whatever else forms part of your project or task. You have posted a very minimal piece of code, and I have provided two options for you to incorporate into your task. What I'd therefore expect, is for you to try them, and if they fail to achieve your goal as intended, edit your question accordingly, so that we can all reproduce the same environment and issue. It's not until then, can we provide focused assistance.
0

You do not need to start a new cmd instance. As the code is right now, you also do not even require delayedexpansion

@echo off
set /P id="Enter yes or no: "
echo %id%
if /i "%id%" == "yes" (
     exit /b 0
   ) else (
     exit /b 5
)

Should you however require using delayedexpansion then use it with setlocal and do not start cmd with /V:on The code will be something like:

@echo off
setlocal enabledelayedexpansion
set /P id="Enter yes or no: "
echo !id!
if /i "!id!" == "yes" (
     exit /b 0
   ) else (
     exit /b 5
)

Note, the second example above is an example, it will work, but delayedexpansion is not required here at all.

Should you want to use conditional operators, which acts similarly to if and else then you can simply run:

@echo off
set /P id="Enter yes or no: "
echo %id% | findstr /i "yes">nul && exit /b 0 || exit /b 5

1 Comment

actually i am running this script from robot framework, i want to open terminal and want to take input, based on that input and status code i will do further operation in robot script, thats why i have used that start cmd. any solution for this?
0

I do not really understand why you are using the start command to run a cmd.exe instance to run further commands – perhaps start could simply be omitted.

Anyway, if you do want to use start you need to be aware that it actually only starts the given command line but it does not wait until the executed command line is finished, unless you specify the /WAIT option. Not waiting implies that the final ErrorLevel state cannot yet be available.

Hence the command line should probably look like this:

start "" /WAIT cmd /V:ON /K "set /P id="Enter yes or no: " & echo(!id!& if /I "!id!"=="yes" (exit 0) else (exit 5)"`

You might have noticed that I changed a few more things:

  • besides adding /WAIT, I also added an empty string "" as the first parameter of the start command to specify a window title; this is necessary to safely handle command lines that could contain quotation marks (otherwise the first quoted part might erroneously be interpreted as the window title);
  • && lets the following command only be executed when the preceding one succeeds (hence it returns an exit code of zero), but I think you want to execute the following ones unconditionally, so I replaced && by & for that purpose;
  • echo !id! returns ECHO is ON. or ECHO is OFF. when !id! expands to an empty string, so I used echo(!id! instead; then I removed the SPACE in front of the following &, which would be output too, unintentionally;
  • an empty value of !id! would cause your if command line to result in a syntax error, so I introduced proper quotation; moreover, I specified a case-insensitive comparison by adding /I;
  • finally, I replaced exit /B by exit, because the former is only needed in batch files to quit them without affecting the hosting cmd.exe instance, but for just quitting cmd.exe the latter is enough;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.