patterncsharpMinor
Method to get next available filename
Viewed 0 times
filenameavailablemethodnextget
Problem
I needed a utility function/method that would get the next available filename to save as.
An example would be, if I need to save a file as MyTestFile.html but it already exists.
This method would then check to see what the next availble filename would be, and it would return
I wrote the method, but it feels sloppy, and isn't really very elegant.
Any ideas for improving the readability of this code, or a better way to do it?
This would then be called like this:
Then if
An example would be, if I need to save a file as MyTestFile.html but it already exists.
This method would then check to see what the next availble filename would be, and it would return
MyTestFile2.html. If MyTestFile2.html exists, then it would return MyTestFile3.html, and so on.I wrote the method, but it feels sloppy, and isn't really very elegant.
Any ideas for improving the readability of this code, or a better way to do it?
public string GetNextAvailableName(List files, string baseFile)
{
var baseFileWithoutExt = Path.GetFileNameWithoutExtension(baseFile);
var baseExt = Path.GetExtension(baseFile);
//clean files and get the ones containing oure baseFileName
var cleanFiles = files
.Select(i => Path.GetFileNameWithoutExtension(i.ToLower()))
.Where(i => i.Contains(baseFileWithoutExt.ToLower()));
var count = cleanFiles.Count();
if (count == 0)
return baseFile;
int indexCount = 1;
do
{
indexCount++;
} while (cleanFiles.Contains(baseFileWithoutExt.ToLower() + indexCount));
return baseFileWithoutExt + indexCount + baseExt;
}This would then be called like this:
GetNextAvailableName(Directory.GetFiles("C:\\MyFiles\\", "MyTestFile.html");Then if
MyTestFile.html is already in use, it will return MyTestFileX.html, where X is the lowest positive integer where the filename doesn't exist yet.Solution
I don't understand why you want to pass a list of files. Simply check the existence of your filename candidates. Start with the base filename. If it does not exist, start counting. In my eyes this a straightforward approach and therefore easy to understand.
public string GetNextAvailableFilename(string filename)
{
if (!System.IO.File.Exists(filename)) return filename;
string alternateFilename;
int fileNameIndex = 1;
do
{
fileNameIndex += 1;
alternateFilename = CreateNumberedFilename(filename, fileNameIndex);
} while (System.IO.File.Exists(alternateFilename));
return alternateFilename;
}
private string CreateNumberedFilename(string filename, int number)
{
string plainName = System.IO.Path.GetFileNameWithoutExtension(filename);
string extension = System.IO.Path.GetExtension(filename);
return string.Format("{0}{1}{2}", plainName, number, extension);
}Code Snippets
public string GetNextAvailableFilename(string filename)
{
if (!System.IO.File.Exists(filename)) return filename;
string alternateFilename;
int fileNameIndex = 1;
do
{
fileNameIndex += 1;
alternateFilename = CreateNumberedFilename(filename, fileNameIndex);
} while (System.IO.File.Exists(alternateFilename));
return alternateFilename;
}
private string CreateNumberedFilename(string filename, int number)
{
string plainName = System.IO.Path.GetFileNameWithoutExtension(filename);
string extension = System.IO.Path.GetExtension(filename);
return string.Format("{0}{1}{2}", plainName, number, extension);
}Context
StackExchange Code Review Q#32313, answer score: 7
Revisions (0)
No revisions yet.