I have never had that problem, but I would start out by trying to clear up any thrush. Unless they are the flies hatch out into screw worms (irradiated in the US), flies need dead organic matter to hatch and live in. Other than that, I guess I would be using a Pyrethrin fly spray on the bottoms of their feet.
Do they have to pass through one gate or other narrow place? If so, you could put out a shallow box in the gate and place Corral Powder or Devin Dust in it and they would have to step in it every time they passed through it. That should make their feet uninhabitable by flies or larvae.