Seems like a duplicate post of this.
The issue is when you strip off the XX- from some files you are left with a file name that already exists. How exactly do you want to deal with the dups? You cannot have 2 files in the same folder with the same name. It isn't allowed. Perhaps you want to add a (1)
to the filename.
var files = Directory.GetFiles(@"C:\Users\egome\OneDrive - Universidad Politécnica de Madrid\Desktop\speckyboy-free-avatar-icon-set\SVG\1 de 3 Avatars FLAT"))
foreach (var file in files)
{
//Assuming all files begin with XX- here...
var adjustedName = Path.GetFileName(file).Remove(0, 3);
//Ensure unique
var index = 1;
var newName = adjustedName;
while (File.Exists(newName))
{
newName = adjustedName + $" ({index}++)";
};
File.Move(file, Path.Combine(Path.GetDirectoryName(file), newName), true);
}