0

I'm creating a simple .bat file to open Android emulator from a new command prompt window and then print a value in the first window. However the function :Display seems doesn't get executed. When I click on .bat file, new prompt is opened and emulator is started but the first window is immediately closed. How can I make first window execute the :Display function?

cd "C:/Users/Pavindu/Downloads/Sdk/emulator"
start cmd /k emulator -avd Nexus_S_API_28
call :Display
:Display
echo.  here the myDosFunc function is executing a group of commands
echo.  it could do a lot of things
Pavindu
  • 2,684
  • 6
  • 44
  • 77

1 Answers1

1
@echo off
cd "C:/Users/Pavindu/Downloads/Sdk/emulator"

start cmd /k emulator -avd Nexus_S_API_28

REM we can also use a goto :Display with a pause as the last command
call :Display

REM insert more commands here after display call
pause
exit /B

:Display
echo here the myDosFunc function is executing a group of commands
echo it could do a lot of things
SNR
  • 712
  • 1
  • 8
  • 22
  • Like this the code is going to execute two times the `:Display` routine! – Compo Sep 25 '19 at 15:23
  • @Compo as far as I can understand no because of the `start cmd`. Can be `call cmd` going to execute it two times? – SNR Sep 25 '19 at 15:34
  • 1
    I see :Display getting called twice. You either need goto :EOF or exit /b after the call :Display line. Am I missing something here? – Señor CMasMas Sep 25 '19 at 15:39
  • @SNR, your example above, changes directory to the location of the emulator executable, despite it using the incorrect path separators, and not specifically designating the drive. Then it opens a new `cmd` instance, (in a separate window, unrelated to the rest of this script), and immediately runs `call :Display`, which then returns to the point directly after that `call`. As there's nothing to tell your script not to continue, it will proceed to run the code under the `:Display` label, exiting at the `exit /b`. As I stated, it is running the `:Display` labelled code twice! – Compo Sep 25 '19 at 15:48
  • @Compo then I am missing something. The test I ran just execute one time `:Display` I understand because of the `exit \b` at the end of the subroutine. When I'll get home I am going to re-do the test. – SNR Sep 25 '19 at 15:58
  • @Compo you are right! I re-done the test and it was executing `:Display` twice. Somehow, I missed the first testing process. Thanks very much. I am going to edit the post so it works properly. – SNR Sep 25 '19 at 19:01
  • @Compo And also re-reading your comment I see how works the what i missed. Therefore a `goto` should work fine. – SNR Sep 25 '19 at 19:07