An elegant way of renaming a file if it already exists when archiving


private static string UniqueLogName(string fullPath)
    var count = 1;

    var fileNameOnly = Path.GetFileNameWithoutExtension(fullPath);
    var extension = Path.GetExtension(fullPath);
    var path = Path.GetDirectoryName(fullPath);
    var newFullPath = fullPath;

    while (File.Exists(newFullPath))
        var tempFileName = string.Format("{0}_{1}", fileNameOnly, count++);
        newFullPath = Path.Combine(path, tempFileName + extension);

    return newFullPath;



This is probably the sort of thing I should put into a little gist.
Only there’s not currently gist-integration for pmwiki.
With syntax-highlighting.
So, I guess that’s something else I could work on....