Reputation: 153
Apologies guy if this question seems to be repetitive . I have 2 requirements .
Upon encountering an error with a particular line , I do not want the following lines of code to be executed .
I want the cmd window to still be open on encountering an error with a command.
I tried with goto :EOF and exit /b . Both of these are closing the cmd prompt .
A part of my code is below :
call apic login --server %server_push% --username %username% --password %user_password%
IF %ERRORLEVEL% NEQ 0 (
ECHO Unable to login to %server_push%
goto :EOF
)
call apic drafts:push %completeProductName% --organization %organization% --server %server_push%
IF %ERRORLEVEL% NEQ 0 (
ECHO unable to push product to %server_push%
goto :EOF
)
if the call apic login does not work , the call apic drafts should not get executed . Also the cmd prompt should not get closed .
Upvotes: 1
Views: 3004
Reputation: 130809
I suspect your goal is not really to keep the console window open indefinitely when there is an error, but rather to keep the window open long enough to allow the user to see the error message before the window closes.
That is easily done by introducing a PAUSE before you goto :EOF
(or exit /b
).
call apic login --server %server_push% --username %username% --password %user_password%
IF %ERRORLEVEL% NEQ 0 (
ECHO Unable to login to %server_push%
pause
goto :EOF
)
call apic drafts:push %completeProductName% --organization %organization% --server %server_push%
IF %ERRORLEVEL% NEQ 0 (
ECHO unable to push product to %server_push%
pause
goto :EOF
)
Now the window will remain open until the user presses a key.
If you really want the console window to remain open after your script terminates, then there are a couple ways to do this.
It is not the batch script that is closing the window. Rather it is a function of how the script is called. I suspect you are launching the script by double clicking the script within Windows Explorer. By design, that action will open the console window, run the script, and then close the console window once the script has finished executing.
It is possible to modify how you call the script so that the window does not close upon completion.
When you double click the script, it is executed via the following command:
C:\Windows\system32\cmd.exe /c ""D:\yourScriptPath\yourScript.bat" "
One option is to create a shortcut for the script, and modify the properties of the shortcut so that the Target looks like:
C:\Windows\system32\cmd.exe /k ""D:\yourScriptPath\yourScript.bat" "
Another possibility is to modify your script to detect whether it was launched by CMD /C, and if so, then relaunch using CMD /K. The following is not fool-proof, but it will likely work for most situations. One limitation is your script path must not contain =
or !
.
Simply place the following at the top of your script:
@echo off
setlocal enableDelayedExpansion
set "cmd=!cmdcmdline!"
if "!cmd:cmd.exe /c ""%~0"^=!^" neq "!cmd!" "%comspec%" /k ^""%~0" %*^" & exit /b
endlocal
Upvotes: 2
Reputation: 57252
May be like this?
call apic login --server %server_push% --username %username% --password %user_password%
IF %ERRORLEVEL% NEQ 0 (
ECHO Unable to login to %server_push%
goto :end
)
call apic drafts:push %completeProductName% --organization %organization% --server %server_push%
IF %ERRORLEVEL% NEQ 0 (
ECHO unable to push product to %server_push%
goto :end
)
exit /b
:end
for /f "tokens=2 delims= " %%a in ("%cmdcmdline%") do if "%%a" equ "/c" pause
Upvotes: 1