Skip to content

Instantly share code, notes, and snippets.

@samilkorkmaz
Last active December 29, 2023 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samilkorkmaz/620c7825dacee5b0b71420b5133ae5aa to your computer and use it in GitHub Desktop.
Save samilkorkmaz/620c7825dacee5b0b71420b5133ae5aa to your computer and use it in GitHub Desktop.
Windows batch file to start an exe and check its result
::Test pass criteria: Exe has to finish within maxExeRunTime_s and exe output's last line must be equal to expectedLastLine
@echo off
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