0

I have a long list and i want to split it up in parts that will wait for an external trigger to go on. At first I tried a for loop that did 100 rows per run. But because of differences in workload on the pc the amount of time it takes varies.

So i was wondering, is it possible to loop the FOR statement for a set time. But could not find any example. After adding the suggestion stephan made to use a for loop with delayed expansion, it now works.

How to include a maximum run time in the for loop?

'''

@ECHO OFF
SETLOCAL enabledelayedexpansion
CLS

set ENDTIME=!TIME!
set function_endtime=!ENDTIME!
for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
    set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
:: add 4 seconds to current time as end time
set /A function_endtime=start+(4*100)
:: Format the result for use in if loop
set /A hh=function_endtime/(60*60*100),rest=function_endtime%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
set function_endtime=%hh%:%mm%:%ss%.%cc%
echo Finish: %function_endtime% now: %time%


    FOR /F "tokens=1,2 delims=," %%A IN (C:\test.txt) DO (
        echo %%A %%B
        set "last_value=%%B"
        if "!time!" geq "%function_endtime%" goto :done
    )
:done
echo %time%

'''

user74341
  • 3
  • 3
  • 1
    calculate the end-time and within the loop `if "!time!" geq "%endtime%" goto :done`, where the label `done` is outside the loop. Needs [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) and may depend on the local format of `%time%` (although that shouldn't matter if you need a range of seconds or minutes) – Stephan Dec 01 '21 at 14:29
  • i've tried your code but must have something wrong because it still runs endless, can you elaborate? – user74341 Dec 01 '21 at 19:12

1 Answers1

0

you can't put code between goto and the :label I guess you completely misinterpreted my comment. Your code should look like:

...
FOR /F "tokens=1,2 delims=," %%A IN (C:\test.txt) DO (
    echo %%A %%B
    set "last_value=%%B"
    if "!time!" geq "%function_endtime%" goto :done
)
:done
...

(without any testing or checking your logic)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • i was confused because you wrote where :done is outside the loop...i thought it such strange language: goto for :) and i must say, calculating a time was more code than i thought, but i guess worth it. – user74341 Dec 01 '21 at 21:11
  • `goto :label` is one command and jumps to the `:label` - but you figured it out already. And yes - math with time or date is pure pain in batch. – Stephan Dec 01 '21 at 22:25
  • that's why i started this thread, i was really wondering if all this pain was needed. my script is now 4-6x longer because of the time and date calculations. if I had known this upfront i might have chosen a less elegant solution. – user74341 Dec 03 '21 at 07:00