For a file function I added an iterator the the new name of the file.
The names will look like this:
- My Name
- My Name (1)
- My Name (2)
- …
- My Name (10)
For finding the next “available” number I’m using a while loop:
while (Stuff.FindNameByText(text) != null)
{
text = string.Format("{0} ({1})", text, i.ToString());
i++;
}
File.SetNewName(text)
But this code of course gives me the following output:
- My name
- My name (1)
- My name (1) (2)
- …
How do I first add an iterator the strings end (if not existing) – and otherwise replace the existing one. I did the following, but it would end in an infinite loop…
while (Stuff.FindNameByText(text) != null)
{
Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", "(", ")"));
text = regex.IsMatch(text, i.ToString());
File.SetNewName(text)
}
Maybe someone could help me out a bit. If iterator at the end is not existing – add one, otherwise replace existing one with new number.
1 Answer