Open a command prompt window and run if /?
. The output help explains how to correct check for an errorlevel with command IF using the syntax if errorlevel X ...
working since MS-DOS and also within a command block.
if %errorlevel% == 1 goto ShiftFocus
does not work in a command block starting with (
and ending with matching )
as beginners in batch file writing expect because of Windows command processor replaces all occurrences of %variable%
in entire command block by value of referenced environment variable before the command block is executed at all. This can be seen on running a batch file without @echo off
from within a command prompt window.
See also single line with multiple commands using Windows batch file explaining how to evaluate the exit code of a command or application with if errorlevel X ...
or using the operators &&
and ||
.
It is also possible to use if errorlevel 1 if not errorlevel 2 goto ShiftFocus
which means exit code is greater or equal 1
but is not greater or equal 2
, i.e. like if ((errorlevel >= 1) && (errorlevel < 2))
in C, C++, C#, JavaScript, etc., in case of an explicit test on exit code 1
is wanted with excluding exit code 128
.
It would be also possible to enable delayed expansion and use it as explained by help of command SET output on running set /?
in a command prompt window with if !errorlevel! == 1 goto ShiftFocus
. But usage of delayed expansion which can cause other troubles and make batch file processing slower because of double parsing of each command line is really not recommended by me on using it just for evaluation of exit code of a command or application.
However, the real problem is that on Internet Explorer not running the batch file should jump to label ShiftFocus
according to explanation in question which can be simply achieved by using if errorlevel 128 goto ShiftFocus
.
So the entire batch file could be:
:Launch
start "CloseMe" "C:\Program Files\Internet Explorer\iexplore.exe" "file://C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files\Stony Mountain Institute Lift Station.html"
%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK >nul
:ShiftFocus
%SystemRoot%\System32\wscript.exe "C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files\SendAltTab.vbs"
%SystemRoot%\System32\taskkill.exe /IM iexplore.exe /FI "WINDOWTITLE eq CloseMe - Internet Explorer"
if errorlevel 128 %SystemRoot%\System32\timeout.exe /T 1 /NOBREAK >nul & goto ShiftFocus
exit /B
It is better to use the console applications (external commands) with full path and file extension to make the batch file independent on environment variables PATH
and PATHEXT
.
And command exit
should not be used without option /B
as it makes testing a batch file very difficult on running it from within a command prompt window instead of double clicking it. See debugging a batch file for the details.
There is one extra wait for one second added before jumping to ShiftFocus
and running the script once again because rapidly running the script and taskkill
in a loop does not make sense to me.
But this batch file does not work for the reason explained by aschipfl: TASKKILL does not exit with value 128 as written by CatCat on using filter option /FI
and there is not running process matching the filter.
A language independent solution to work around this quirks of TASKKILL was posted by aschipfl.
A language dependent solution would be using this batch file:
:Launch
start "CloseMe" "C:\Program Files\Internet Explorer\iexplore.exe" "file://C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files\Stony Mountain Institute Lift Station.html"
%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK >nul
:ShiftFocus
%SystemRoot%\System32\wscript.exe "C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files\SendAltTab.vbs"
for /F "delims=:" %%I in ('%SystemRoot%\System32\taskkill.exe /IM iexplore.exe /FI "WINDOWTITLE eq CloseMe - Internet Explorer"') do if "%%I" == "INFO" %SystemRoot%\System32\timeout.exe /T 1 /NOBREAK >nul & goto ShiftFocus
exit /B
The TASKKILL command line is executed by FOR in a separate command process started with cmd /C
in background. The line output by TASKKILL to handle STDOUT in this background command process is captured by FOR. Then the captured line is split into substrings using colon as delimiter. The string up to first colon is assigned to loop variable I
. This string is here language dependent either INFO
or SUCCESS
. In case of INFO
no instance of Internet Explorer was found with window title CloseMe - Internet Explorer
resulting in a jump to ShiftFocus
after 1 second and running the VBScript and TASKKILL once again.
This Windows language dependent solution should not be used if the batch file should run on any Windows machine with Windows Vista or a later Windows version independent on language of Windows.