Here is one algorithm that would do it:
1. Get the filename before the extension (.exe). If the filename string length is less than or equal to 8, do nothing and exit.
2. Initialize a counter to 0.
3. Get the first six characters in the filename and store to a buffer or variable.
4. Append the squiggly character (~) to the buffer.
5. Increment and then convert the counter to a character.
6. Append the counter character to the buffer.
7. Append the extension (.exe)
8. Check to see if the new buffer string is a file that already exists. If so, strip out the extension and the counter character, and goto step 5. If the file does not exist then you are done.
This will bomb when your counter reaches 10 because then your filename string will be 9 characters lo
ng. So you have to check for the counter reaching 10, in which case you have to go back to step 3 and get the first 5 characters instead of 6, and continue on. This will work until your counter gets to 100.
I hope this is clear enough.