forward port of fix for fold_file_loop

2.0 version of #172