fix bug when naming files that are already exist

When the add ~ prefix option were enabled and there was already a
file with the ~ prefix at its name, program overwrited the last file.
This commit fix this, adding a new ~ to name until the new file name
does not exist.