Jump to content
TUFLOW Forum
par

Looping in batch files

Recommended Posts

If you have a lot of runs to start using the event and scenario options in TUFLOW, you may want to use a batch file with FOR loops.

In the example below, there are two event options:



  • -e1 : Event Magnitude
  • -e2 : Event Duration

If we wish to run 5 event magnitudes for 5 durations the following text in a batch file could be used:

@echo off

:: This sets the variables as local, so you can use another batch file with A and B variables (otherwise they are shared across batch files)
SetLocal

:: set up variables
set A=Q010 Q020 Q050 Q100 Q200
set B=10min 30min 60min 120min 270min

:: Loop Through
FOR %%a in (%A%) do (
FOR %%b in (%B%) DO (
start "TUFLOW" /wait C:\TUFLOW\releases\2011-09\w64\TUFLOW_iSP_w64.exe -e1 %%a -e2 %%b filename.tcf
)
)
pause[/code] In order to test the batch file replace the start "TUFLOW" line with the line below. The addition of the echo at the front of the line will cause the command line to be displayed to the DOS window rather than started:
[code]echo start "TUFLOW" /wait C:\TUFLOW\releases\2011-09\w64\TUFLOW_iSP_w64.exe -e1 %%a -e2 %%b filename.tcf
The loops can be compressed onto a single line if desired:
@echo off
setlocal
set A=Q010 Q020 Q050 Q100 Q200
set B=10min 30min 60min 120min 270min

FOR %%a in (%A%) do FOR %%b in (%B%) do start "TUFLOW" C:\TUFLOW\releases\2011-09\w64\TUFLOW_iSP_w64.exe -e1 %%a -e2 %%b filename_~e1~_~e2~.tcf
pause[/code] Additional variables (for example different scenarios) can be incorporated:
[code]
@echo off
setlocal
set A=Q010 Q020 Q050 Q100 Q200
set B=10min 30min 60min 120min 270min
set C=Exg Dev Mit

FOR %%a in (%A%) do FOR %%b in (%B%) do FOR %%c in (%C%) do start "TUFLOW" TUFLOW_iSP_w64.exe -e1 %%a -e2 %%b -s1 %%c filename_~e1~_~e2~_~s1~.tcf
pause

Share this post


Link to post
Share on other sites

Hi Phillip,

Thought this might interest you - a batch file that allows you to set the maximum number of models that can be run at one time.

Essentially it goes into an infinite loop when there are less Tuflow executables running than the maximum specified (denoted by CPU_Cores in the batch file below). When an executable becomes available, it returns control to the for loops.

Cheers,

Ben


@ echo off


Set A=Current PropZone PropZoneMitigation

Set B=20yr 200yr

set /a CPU_Cores=2



FOR %%a in (%A%) DO (                

    FOR %%b in (%B%) DO (

       call :do_while_loop_start

       Start "TUFLOW" "C:\Program Files\SMS 11.0\models\TUFLOW\64bit\TUFLOW_iSP_w64.exe" -b -s1 %%a -e1 %%b Avoca_Heathcote.tcf 

       )

)


Pause


:do_while_loop_start

    set /a count=0

    or /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq TUFLOW_iSP_w64.exe"') do set /a count=count+1

    if %count% geq %CPU_Cores% (

        PING 1.1.1.1 -n 1 -w 60000 >NUL

        goto do_while_loop_start

)

Share this post


Link to post
Share on other sites

Hi Ben,

Thanks for that, very impressive!

Do you know if it is possible to use wildcard or search in the IMAGENAME argument? So instead of looking for a specific build - "IMAGENAME eq TUFLOW_iSP_w64.exe", you could just search for TUFLOW*.exe?

Thanks for posting.

Phillip

Share this post


Link to post
Share on other sites

This should work - i do not have the licence at the moment so I cannot check.

Replace

for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq TUFLOW_iSP_w64.exe"') do set /a count=count+1
with
for /f %%x in ('tasklist ^| find /c "tuflow"') do set count=%%x

Now it searches the entire tasklist looking for a string. Thanks for the suggestion, any further improvements?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...