Assuming your text part is a string variable, you can split it using "\" as a delimiter. You will then have an array that you can use to check if each block is longer than 8 characters. When you loop the array, you can slice the last characters of each long block and put ~ 1. Since you are in a loop, you can gradually add all these changes to the temporary variable, which will ultimately give the final edited result.
The only part that is a bit complicated is to pay attention to the .exe part at the end.
So, if I were you, I would start reading String.split (), String.substring (), for loop, arrays
source share