Tiemout in a for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chavannep
Posts: 1
Joined: 08 Mar 2020 13:43

Tiemout in a for loop

#1 Post by chavannep » 08 Mar 2020 14:36

Hi there,

I looked for a solution on many forums but I didn't find any suitable ones...

I have several functions (let's say file_1.req file_2.req...file_100.req) I would like to launch with the associated program (let's say calculate.exe). A normal calculation is done within approx. 10s. For some reasons, a file_i.req may make calculate.exe get stuck and the following functions (from i+1 to n) won't be launched (idle case). To avoid this problem, I want to write a batch file which would call calculate.exe for each file_i.req and which would use a 'timeout' (let's say 15s) to perform calculations in a finite time. Moreover it would continue with a 'taskkill' for file_i.req in case 'timeout' elapsed (without waiting for user's commands). I would like my script to continue with the 'i+1' .req file's calculation.
I can not use 'start' for calculate.exe which would open n windows for each req file.
Here is my pseudo-code in the .bat file :

set enabledelayedexpansion
set beginning=file_
set extension=.req

for /L %%g in (1,1,100) do(
set file_req=%beginning%%%g%extension%
call calculate.exe -f %file_req%
:: timeout /T 15 /nobreak
:: taskkill (calculate.exe -f %file_req%)
:: continue
)
endlocal

Thanks a lot for helping !

Pierre

aGerman
Expert
Posts: 3840
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Tiemout in a for loop

#2 Post by aGerman » 09 Mar 2020 13:58

chavannep wrote:
08 Mar 2020 14:36
I can not use 'start' for calculate.exe which would open n windows for each req file.
At least you have to use the START command (perhaps START /B) to run your tool asynchronously. Otherwise the timeout command would not be called before calculate.exe has finished.

Untested:

Code: Select all

set "beginning=file_"
set "extension=.req"

for /L %%g in (1,1,100) do(
     start /b calculate.exe -f "%beginning%%%g%extension%"
     timeout /t 15 /nobreak
     taskkill /im calculate.exe /f
)
This will of course invoke the timeout command for every req file.

Steffen

Eureka!
Posts: 76
Joined: 25 Jul 2019 18:25

Re: Tiemout in a for loop

#3 Post by Eureka! » 09 Mar 2020 14:42

In your original code, there are some commands that will prevent a correct functioning.
Note: just for a learning purpose; I would go with @aGerman's code.

set enabledelayedexpansion should be:

Code: Select all

setlocal enabledelayedexpansion
(setlocal /? or details)

call calculate.exe -f %file_req% should be:

Code: Select all

call calculate.exe -f !file_req!
( SET /? for details )


Note:
In aGerman's solution, you might want to try

Code: Select all

start /wait "Dummy title" calculate.exe -f "%beginning%%%g%extension%"
This will wait until the calculate.exe process is ended. That way you can skip the timeout command.
(start /? for details)

Post Reply