I've seen people posting code snippets, so here's one...
I was looking around for a way to increment a file name, and didn't come across anything that really worked all that well except for 1 that kind of worked. I've bolstered it up a bit & made it more robust.
Problem:
You want to quickly save file.ext, but not overwrite an existing file with that name. Obviously you want to add an increment for the file name: 1, 2, 3, etc. So you end up with something like this in a folder:
file.ext
file (1).ext
file (2).ext
etc.
Snippet:
This is a bit sloppy in a few places, but it's good enough.
NOTE: This comes out of writing some transcoding software, so the signature there is to allow passing in a file name along with a new file name extension, which is a bit sloppy for general purposes. Basically, you can change the file extension by passing in a different "ext" parameter.
If you don't want a new file extension, simply pass in an empty string for "ext".
Just one of those typical jobs that nobody should have to bother doing again.
I was looking around for a way to increment a file name, and didn't come across anything that really worked all that well except for 1 that kind of worked. I've bolstered it up a bit & made it more robust.
Problem:
You want to quickly save file.ext, but not overwrite an existing file with that name. Obviously you want to add an increment for the file name: 1, 2, 3, etc. So you end up with something like this in a folder:
file.ext
file (1).ext
file (2).ext
etc.
Snippet:
This is a bit sloppy in a few places, but it's good enough.
NOTE: This comes out of writing some transcoding software, so the signature there is to allow passing in a file name along with a new file name extension, which is a bit sloppy for general purposes. Basically, you can change the file extension by passing in a different "ext" parameter.
If you don't want a new file extension, simply pass in an empty string for "ext".
Code: (csharp)
/// <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;
}Just one of those typical jobs that nobody should have to bother doing again.








