Close file handle before attempting removal

Fixes earlier commit; forgot that opening a file defaults to a lock under Windows
Merge pull request #7938 from scottp-dpaw/patch-1

Close file handle before attempting removal