Set up loop to find shocks that match path of variable

Hi all,
I need to find a sequence of a specific shock that generates the path of a specific variable in the data, in the context of a perfect foresight simulation.
After checking out some older threads on similar issues, I accepted the fact that this is most easily done using a loop/bisection.
I was wondering what is the most efficient way of calling Dynare several times to evaluate the model at a specific path of shocks (which I then adjust based on the output of oo_.endo_simul). Right now, I am simply doing
dynare name_of_mod_file.mod, noclearall
inside the loop. But I am sure this is not the right way of doing it. Is there any function I can use that, given an already run perfect_foresight_setup, I can use to find the transition within the loop for a given combination of shocks?
Thanks in advance!

See

That should be done within the loop.

Perfect, that does exactly what I wanted. That was easy. Thanks a lot for your reply Johannes, and for the public service you provide to the profession!