An elegant way of renaming a file if it already exists when archiving
<p>I'd like to write an archiving function that takes all files in a folder and move them into an archive sub folder using the current date. The process could be run several times in a day and therefore needs to handle duplicates.</p>

<p>The rules for the archiving are below:</p>

<ol>
<li>If a file name already exists, I'd like to add an underscore "_" and
a number (starting from 1).  </li>
<li>If the file name has already been modified the I'd like
to increment the number.</li>
</ol>

<p>I can do this using lots of <code>File.Exist</code> and <code>LastIndexOf</code> calls but is there a more elegant way? maybe with LINQ?</p>

<p><strong>EDIT:</strong></p>

<p>This is the code I have already. It's a bit rough and ready but it gives an idea of what I want to do.</p>

<pre><code>/// &lt;summary&gt;
/// Move the local file into the archive location.
/// If the file already exists then add a counter to the file name or increment the existing counter
/// &lt;/summary&gt;
/// &lt;param name="LocalFilePath"&gt;The full path of the file to be archived&lt;/param&gt;
/// &lt;param name="ArchiveFilePath"&gt;The proposed full path of the file once it's been archived&lt;/param&gt;
private void ArchiveFile(string LocalFilePath, string ArchiveFilePath)
{
    // Ensure the file name doesn't already exists in the location we want to move it to
    if (File.Exists(ArchiveFilePath) == true)
    {
        // Does the archive file have a number at the end?
        string[] archiveSplit = Path.GetFileNameWithoutExtension(ArchiveFilePath).Split('_');
        if( archiveSplit.Length == 1)
        {
            // No number detected so append the number 1 to the filename
            string newArcFileName = string.Format("{0}_1.{1}",
                Path.GetFileNameWithoutExtension(ArchiveFilePath), Path.GetExtension(ArchiveFilePath));

            // Create the new full path
            string newArcPath = Path.Combine(Path.GetDirectoryName(ArchiveFilePath), newArcFileName);

            // recursively call the archive folder to ensure the new file name doesn't exist before moving
            ArchiveFile( LocalFilePath, newArcPath);
        }
        else
        {
            // Get the number of the last element of the split
            int lastNum = Convert.ToInt32( archiveSplit.Last().Substring(1) ) +1;

            // Rebuild the filename using the incremented number
            string newArcFileName = archiveSplit[0];
            for (int i = 1; i &lt; archiveSplit.Length; i++)
            {
                newArcFileName += archiveSplit[i];
            }
            // finally add the number and extension
            newArcFileName = string.Format("{0}_{1}.{2}", newArcFileName, lastNum, Path.GetExtension(ArchiveFilePath));

            // Create the new full path
            string newArcPath = Path.Combine(Path.GetDirectoryName(ArchiveFilePath), newArcFileName);

            // recursively call the archive folder to ensure the new file name doesn't exist before moving
            ArchiveFile(LocalFilePath, newArcPath);
        }
    }
    else
    {
        // There is no file with a matching name
        File.Move(LocalFilePath, ArchiveFilePath);
    }
}
</code></pre>
