How to start an .exe file using the input / output file and set a time limit in the batch?

advertisements

In windows command prompt, when I want to run a program using input/output file, I always use batch command like the following: test.exe < input.in > output.out. (test.exe is the name of program, input.it is the name of input file and output.out is the name of output file)

But if I use this command, I cannot set a time limit for that program (i.e. I cannot force the program to quit after an amount of time). So what command I should use in order to do that? Thank you for helping me.


start "" /b cmd /c "test.exe <input.in >output.out"
timeout /t 10
tasklist | find "test.exe" >nul && taskkill /f /im test.exe

Start the program inside a cmd instance not attached to the current console, wait for 10 seconds and if program still in task list, kill it

EDIT - Updated to handle the case pointed in comments

@echo off
    setlocal enableextensions

    start "" /b cmd /c "test.exe <input.in >output.out"

    call :timeoutProcess "test.exe" 10
    if errorlevel 1 (
        echo Program has been killed
    ) else (
        echo Program has ended in time
    )

    exit /b

:timeoutProcess process timeout
    for /l %%t in (1 1 %~2) do (
        timeout.exe /t 1 >nul
        tasklist | find "%~1" >nul || exit /b 0
    )
    taskkill /f /im "%~1" >nul
    exit /b 1