Question: 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 
`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<string> 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.

Answer: 
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);
}

```
Your current code finds "holes" in the numbering. If that is not required (or desired) then you could parse the numbers of the end and find the max and return max + 1. Something along these lines:

```
public string GetNextAvailableName(List<string> files, string baseFile)
{
    var baseFileWithoutExt = Path.GetFileNameWithoutExtension(baseFile);
    var baseExt = Path.GetExtension(baseFile);

    //clean files and get the ones containing oure baseFileName  
    var maxUsedIndex = files
                        .Select(f => Path.GetFileNameWithoutExtension(f.ToLower()))
                        .Where(f => f.StartsWith(baseFileWithoutExt.ToLower()))
                        .Select(f => f.Substring(baseFileWithoutExt.Length))
                        .Select(f => Int32.Parse(f))
                        .Max();

    return baseFileWithoutExt + (maxUsedIndex + 1) + baseExt;
}

```

Note that the code above is not robust against malformed input (`Int32.Parse()` might throw). Also it requires that the passed in `files` are complete and no new file with a new index is created in the meantime.

Note: When you use Linq then imagine your were to write a `foreach` loop instead and try name your loop variables accordingly. `i` had me slightly confused when looping over files - I think `foreach (var f in files)` is more intuitive than `foreach (var i in files)` as `i` is generally something related to a loop index rather than an actual object.