I want to upload file (one at a time) to a folder:
GetUniqueName
function (mentioned below) will return a unique file name
This is the code I use to do it:
public static string GetUniqueName(string fileName)
{
string dir = Globals.Directories.GetCustomCategoryThumbnailDir();
string fileExtension = Path.GetExtension(fileName);
string fileNameWE = Path.GetFileNameWithoutExtension(fileName);
string[] files = Directory.GetFiles(dir, "*" + fileExtension)
.Select(Path.GetFileName)
.ToArray();
string uniqueName = fileNameWE;
int nextNum = 0;
bool fileExist = false;
string pattern = @"(.*)\(([\d]+)\)";
foreach (var file in files)
{
var tempFileName = Path.GetFileNameWithoutExtension(file);
var match = Regex.Match(tempFileName, pattern);
if (tempFileName.Equals(fileNameWE))
{
// file exist in folder
fileExist = true;
}
if (tempFileName.StartsWith(fileNameWE) && match.Success)
{
// there is a file name that start with "fileToUpload" name, we want to to take the number
nextNum = Convert.ToInt32(match.Groups[2].Value);
nextNum++;
}
}
if (nextNum == 0 && !fileExist)
{
// filename dont exist
return fileNameWE + fileExtension;
}
if (nextNum == 0 && fileExist)
{
// the file name exist without (1)
fileNameWE = $"{fileNameWE}(1)";
return fileNameWE + fileExtension;
}
else
{
var haveParentheses = Regex.Match(fileNameWE, pattern);
if (haveParentheses.Success)
{
// we need to reset the nextNum
nextNum = 1;
}
// return the new unique name with suffix
fileNameWE = string.Format("{0}({1})", fileNameWE, nextNum);
return fileNameWE + fileExtension;
}
}
- All the solution that i find online are keeping the file name unique with the use of
GUID
orDateTime
- for other cases that i checked the function work correctly and will return a "good" file name.
Some examples:
- The folder contain:
- army.png
I want to upload: army.png
- The folder will contain:
- army.png
- army(1).png
I want to upload again: army.png
- The folder will contain:
- army.png
- army(1).png
- army(2).png
I want to upload : army(2).png
- The folder will contain:
- army.png
- army(1).png
- army(2).png
- army(2)(1).png
I want to upload again: army(2).png
- The folder will contain:
- army.png
- army(1).png
- army(2).png
- army(2)(1).png
- army(2)(2).png
I want to upload again: army(2).png
- The folder will contain:
- army.png
- army(1).png
- army(2).png
- army(2)(1).png
- army(2)(2).png
- army(2)(3).png
I want to upload : army(2)(2).png
- The folder will contain:
- army.png
- army(1).png
- army(2).png
- army(2)(1).png
- army(2)(2).png
- army(2)(3).png
- army(2)(2)(1).png
How can I fix the function so it will return the correct string?
Sounds like you should just run a loop, right?
Ok, unfortunately I couldn't found the problem in your poster algorithm, but I wrote a new one, and I tested with several names. Appears to be working out. Please check-it:
TEST Scenarios:
army(2).png -> army(2)(2).png
army.png -> army(3).png
army(1).png -> army(1)(1).png
Final version
This is tested and working according to all the cases on the question: