fixes #3321 Problems when you rename a file with an existing name

Fixed the issue and pushed code for review, please check it.