fix problem when having different filename

Rebased on 2.4.x

Original PR #1550