Last active
July 22, 2024 07:59
Windows batch file to start an exe and check its result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
::Test pass criteria: Exe has to finish within maxExeRunTime_s and exe output's last line must be equal to expectedLastLine | |
@echo off | |
setlocal enabledelayedexpansion | |
set currentDir=%CD% | |
:: Set variables | |
set secondWindowTitle=My second window | |
set exeName=MyExe.exe | |
set exeFolder=x64/Release | |
set maxExeRunTime_s=5 | |
set tempOutFile=temp_output.txt | |
set expectedLastLine=Simulation completed. | |
cd %exeFolder% | |
:: Start the program in a new window and redirect output to a file | |
start "%secondWindowTitle%" cmd /c %exeName% > %tempOutFile% | |
:: Allow the program some time to run | |
timeout /t %maxExeRunTime_s% | |
:: Check if the program window is still open | |
tasklist /FI "WINDOWTITLE eq %secondWindowTitle%" | find "%exeName%" > NUL | |
:: If task has finished, the above tasklist command will fail and errorlevel will be 1 | |
if %errorlevel%==1 ( | |
echo %exeName% finished within %maxExeRunTime_s% seconds. | |
echo Get the last line from %tempOutFile%... | |
for /F "delims=" %%i in ('type %tempOutFile%) do set lastLine=%%i | |
:: Extract only the text part from the last line | |
set "lastLine=%lastLine:*]=%" | |
echo Last line is "%lastLine%" | |
echo expected: "%expectedLastLine%" | |
if "%lastLine%"=="%expectedLastLine%" ( | |
echo The last line was as expected. | |
) else ( | |
echo The last line was different! | |
) | |
del %tempOutFile% | |
) else ( | |
echo %exeName% exceeded max run time of %maxExeRunTime_s% seconds. | |
taskkill /FI "WINDOWTITLE eq %secondWindowTitle%" | |
) | |
:: Return to the original directory | |
cd %currentDir% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment