How to Pause Executable if an Error is Encountered? - windows

I googled for a solution for how to pause and executable is an error is encountered. The most promising thing I found was this:
#echo off
"C:\Users\ryans\dist\AS_to_GBQ.exe" %1
if %errorlevel% neq 0 exit /b %errorlevel%
I put that in my Command Prompt, and run it. Now, the exe opens, runs, throws an error, and the exe shuts down with no indication of what actually happened. How can I see the details of the error, or somehow troubleshoot this issue? I'm on Windows 10. I can run this as a batch file, or through PowerShell, if that helps.

Related

Rest of script not running after calling a batch file to run?

I have the following script batch script:
call standalone.bat
"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\Program Files\jboss-eap-6.2\jboss-eap-6.2\standalone\log\server.log"
The first command runs as expected but the script never seems to call notepad to open the server.log file.
What is the issue here?
Edit: ending of standalone.bat is:
if ERRORLEVEL 10 goto RESTART
:END
if "x%NOPAUSE%" == "x" pause
:END_NO_PAUSE
Stephan likely diagnosed the problem in his question comment - standalone.bat probably terminates with an exit command. If that is correct, then you can work around the problem without modifying standalone.bat by changing
call standalone.bat
to
cmd /c standalone.bat.

ERRORLEVEL cannot report regsvr32's process exit code, why?

On Windows 7 SP1, from CMD window, run command regsvr32 none.dll, regsvr32 will fail with exit code 3. This is the expected behavior of regsvr32, as stated in Raymond Chen's blog post.
Procmon verifies this as well.
However, when checking regsvr32's exit code with echo %ERRORLEVEL%, I got zero. WHY?
Thanks to Marks comment. I was just tripped by CMD's sneaky behavior.
To know the exit code of a GUI process like regsvr32, the CMD command line has to be:
start /wait regsvr32.exe none.dll
What is special about regsvr32 is: If we run regsvr /s xxx.dll , /s makes regsvr totally silent, looking very much like a CUI program. So users are more easily get trapped.
But if we execute regsvr32 none.dll inside a .bat/.cmd batch script, start /wait is not required. Such discrepancy (on command prompt vs inside batch script) often causes trouble for the unwary. Sigh.

cmd /c <batch-file> exit code is sometimes 0 even on error?

I have a batch file that I run from an msysgit bash shell script via cmd /c a.bat and then I test the exit code to determine whether or not to continue. When the batch file fails, from wherever it fails, exit /b 1 is called and the batch file exits with code 1.
I recently noticed that if the batch file fails at one point where exit /b 1 is called that the exit code is not returned, and is instead 0. It only happens in an inner block. Here's an example:
#echo off
if foo EQU foo (
if bar EQU bar (
echo Exiting with code 99.
exit /b 99
)
echo this line is necessary to reproduce the issue
)
That should always exit 99, but:
X:\j\tmp>doesnotexist
'doesnotexist' is not recognized as an internal or external command,
operable program or batch file.
X:\j\tmp>echo %ERRORLEVEL%
9009
X:\j\tmp>cmd /c a.bat
Exiting with code 99.
X:\j\tmp>echo %ERRORLEVEL%
0
X:\j\tmp>cmd /c call a.bat
Exiting with code 99.
X:\j\tmp>echo %ERRORLEVEL%
99
If the last echo line is removed then cmd /c a.bat does return exit code 99. And as I mentioned in the actual batch file the exit /b <Code> does work most of the time.
I can reproduce in Windows 7 and 10. My question is why does it not return the exit code in the repro above? Is it a bug or something I did wrong? As you can see I tried CALL on a hunch and it seems to remedy this issue, but I'm not sure why. CALL is supposed to be for calling one batch from another without losing control.
You are starting a new cmd.exe instance which will produce the 99 error in the new window instead of current window.
The reason why you are getting the correct code when running it using call is purely because you are telling it to run the batch file in the current console, hence you get exit code 99
So you do not need to run cmd /c to start the file. Instead run the batch file simply as:
c:\path_to\file\a.bat
or if in the path or current directory:
a.bat
When you call cmd /c, you're creating a new cmd.exe process, and that process is calling your script. So the errorcode variable within that new process is set to 99 but then the cmd process exits successfully, setting the level back to 0 in your starting command prompt.
If you just run a.bat, the error level gets set properly. If you try cmd /k a.bat, the new cmd will be still running, and you'll be able to see that the error level is 99. Then, depending on how you exit, the error level of the original cmd will be set appropriately.

Batch to check if process exists

I'd like a batch that will check if the process firefox.exe exists (after it has been started by the start command).
If the process exists, it will go to the label :fullscreen,
else the batch will go the the label :timeout. Then, it will check again if the process firefox.exe exists and if not, it will go again to the label :fullscreen until the process exists.
Here is my batch:
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
:timeout
timeout /t 5
:fullscreen
nircmd sendkeypress F11
exit
How can I do this check ?
You can also use QUERY PROCESS:
#Echo Off
If Not Exist "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" Exit/B
Start "" "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe"
:Loop
Timeout 5 /NoBreak>Nul
QProcess firefox.exe>Nul 2>&1||GoTo :Loop
NirCmd SendKeyPress F11
I suggest for this task the batch file:
#echo off
start "" /max firefox.exe
if errorlevel 1 goto :EOF
set LoopCount=0
:WaitLoop
%SystemRoot%\System32\timeout.exe /T 5
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq firefox.exe" 2>nul | %SystemRoot%\System32\find.exe /I "firefox.exe" >nul
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
set /A LoopCount+=1
if not %LoopCount% == 6 goto WaitLoop
Let me explain the few command lines used here.
1. Starting Firefox
The command START being an internal command of cmd.exe interprets the first double quoted string as optional title for the console window. Therefore the command line
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
results just in opening a new console window with the window title:
C:\Program Files (x86)\Mozilla Firefox\firefox.exe
For that reason "" is specified as first START argument to define an empty title. Firefox is a GUI application. So no console window is opened which means an empty window title is really enough.
The parameter /max would not be really necessary, but the goal is to get Firefox into full screen mode after starting. So why not starting it already maximized?
32-bit version of Firefox is by default installed in directory %ProgramFiles% on 32-bit Windows and in %ProgramFiles(x86)% on 64-bit Windows. But it is possible during the installation to install Firefox into any other folder. But Firefox installer is well coded and registers firefox.exe in Windows registry under key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
This is recommended by Microsoft as written in MSDN article Application Registration.
The command START searches also in Windows registry under this key for an executable specified as argument without path as explained in answer on Where is “START” searching for executables?
This is the reason for using just firefox.exe on START command line because that starts an installed Firefox independent on installation location.
START displays an appropriate message box if firefox.exe could not be started and exits in this case with a return code greater 0 (9059 in my test on one computer).
The help output on running if /? in a command prompt window explains how to evaluate the exit code of a previous command or application without usage of immediate or delayed environment variable expansion and therefore working anywhere in a batch file from MS-DOS (really!) to currently latest Windows 10.
The command line if errorlevel 1 goto :EOF means IF start failed to start firefox.exe indicated by an exit code greater or equal 1 THEN exit execution of this batch file. For details on exiting batch file execution see answer on Where does GOTO :EOF return to?
2. Checking for running Firefox
The command TASKLIST being an external command, i.e. a console application in system directory of Windows, outputs a list of running processes. This list can be already filtered by TASKLIST itself for a specific process as done in batch file with /FI "IMAGENAME eq firefox.exe".
But TASKLIST is designed for just printing a list of processes. It is not designed for checking if a specific process is running and returning the result to the calling process via exit code. TASKLIST always exits with 0.
But an error message is output to handle STDERR on using a filter and no process can be found in process list matching this filter. For that reason 2>nul is used to suppress this error message by redirecting it to device NUL. Read the Microsoft article about Using Command Redirection Operators for more information about redirection.
A simple method to get a simple false/true respectively 0/1 result on checking for running Firefox is filtering output of TASKLIST with external command FIND which exits with 0 if the string to find was indeed found or with 1 if the searched string could not be found in the text read in this case from STDIN. The output of FIND is of no interest and therefore suppressed with redirection to device NUL using >nul.
Instead of using TASKLIST and FIND it is also possible to use QPROCESS:
%SystemRoot%\System32\qprocess.exe firefox.exe >nul 2>&1
QPROCESS exits with exit code 1 if firefox.exe could not be found in list of running processes. Otherwise the exit code is 0 on firefox.exe is running.
3. Evaluating Firefox process checking result
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
The IF command checks if exit code of FIND is NOT greater or equal 1 which means if exit code is lower than 1. Command FIND exits never with a negative value. So if this condition is true then it is time to execute nircmd.exe to send key press F11 to application in foreground hopefully being Firefox (not guaranteed by this code) and exit batch file processing.
Otherwise the batch file should wait once again 5 seconds and then do the check again. This can very easily result in an endless running batch file in case of started Firefox is immediately closed by the user before the 5 seconds wait timed out. For that reason it is counted how often the wait loop is already executed. After 6 loop runs, or 30 seconds, it is really time to no longer wait for Firefox and exit the batch file.
4. Getting more information about used commands
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 /?
find /?
if /?
qprocess /?
set /?
start /?
tasklist /?
timeout /?
And Single line with multiple commands using Windows batch file should be also read explaining operator & in addition to all other web pages referenced already above.
You can show a list of opened programs like this:
tasklist
To check if firefox exists:
EDIT: Code edited to show a fully working example
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
goto :checkloop
:checkloop
tasklist|find "firefox.exe" > NUL
if %ERRORLEVEL% == 0 (
call :fullscreen
exit
) else (
call :timeout
goto :checkloop
)
:fullscreen
nircmd sendkeypress F11
goto :EOF
:timeout
timeout /t 5
goto :EOF

MS-DOS FTP command in a batch file: raise error

In MS-DOS (Windows 2003 R2 Server), I have a batchfile which has the FTP command in it, eg:-
FTP.CMD
-------
cd d:\extracts\scripts
ftp -i -s:ftp_getfile.ftp
exit
I would like the batch file to raise and return an error level 1 for failure instead of 0,
so that the calling batchfile can deal with it.
The error could be caused by the FTP server being down. Right now, nothing is returned to indicate
an error condition occured.
Please can someone advise?
Thanks! :)
Maybe too late, but it is possible. I'm running the following script to check for errors in the text that's returned by the FTP script. If you know the error text that's returned by FTP, then that's what you look for with the 'find' command.
The ftp commands are in a file called ftp.inp, just check out the help of FTP on how to use '-s'.
ftp -s:ftp.inp > ftp.log
find /I /C "not connected" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
find /I /C "not found" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
find /I /C "failed" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
REM --- no errors found
GOTO :END
:FTPERROR
REM --- error found
:END
As per this question:
How to capture the ftp error code in batch scripts?
The windows FTP command doesn't support this behaviour (or PASV mode) and is basically next to useless.
You might want to try NcFtp instead. It's free, small, portable, and has decent error codes.

Resources