/// <summary>
/// A function to add an incremented number at the end of a file name if a file already exists.
/// </summary>
/// <param name="file">The file. This should be the complete path.</param>
/// <param name="ext">This can be empty.</param>
/// <returns>An incremented file name. </returns>
private string AppendFileNumberIfExists(string file, string ext)
{
// This had a VB tidbit that helped to get this started.
// http://www.codeproject.com/Questions/212217/increment-filename-if-file-exists-using-csharp
// If the file exists, then do stuff. Otherwise, we just return the original file name.
if (File.Exists(file)) {
string folderPath = Path.GetDirectoryName(file); // The path to the file. No sense in dealing with this unecessarily.
string fileName = Path.GetFileNameWithoutExtension(file); // The file name with no extension.
string extension = string.Empty; // The file extension.
// This lets us pass in an empty string for the file extension if required. i.e. It just makes this function a bit more versatile.
if (ext == string.Empty) {
extension = Path.GetExtension(file);
}
else {
extension = ext;
}
// at this point, find out if the fileName ends in a number, then get that number.
int fileNumber = 0; // This stores the number as a number for us.
// need a regex here - \(([0-9]+)\)$
Regex r
= new Regex
(@"\(([0-9]+)\)$"); // This matches the pattern we are using, i.e. ~(#).ext Match m = r.Match(fileName); // We pass in the file name with no extension.
string addSpace = " "; // We'll add a space when we don't have our pattern in order to pad the pattern.
if (m.Success) {
addSpace = string.Empty; // We have the pattern, so we don't add a space - it has already been added.
string s = m.Groups[1].Captures[0].Value; // This is the single capture that we are looking for. Stored as a string.
// set fileNumber to the new number.
fileNumber = int.Parse(s); // Convert the number to an int.
// remove the numbering from the string as we're constructing it again below.
fileName = fileName.Replace("(" + s + ")", "");
}
// Start looping.
do
{
fileNumber += 1; // Increment the file number that we have above.
file = Path.Combine(folderPath, // Combine it all.
String.Format("{0}{3}({1}){2}", // The pattern to combine.
fileName, // The file name with no extension.
fileNumber, // The file number.
extension, // The file extension.
addSpace)); // A space if needed to pad the initial ~(#).ext pattern.
}
while (File.Exists(file)); // As long as the file name exists, keep looping.
}
return file;
}