Click here to Skip to main content
12,892,320 members (48,087 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 29 Jun 2013

Finding a free file name and number combination

, 30 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
One of the tasks we often have to do is rename a file before creating a new version. But when we want to keep multiple versions, how do we find out which ones already exist?


One of the tasks we often have to do is rename a file before creating a new version. The obvious way to do this is to rename the existing file so it has a number suffix:


But when we want to keep multiple versions, how do we find out which ones already exist? 


This started with a QA question and I thought it worth converting my answer to a method and submitting it for posterity! :laugh:

The Copy'n'Paste bit 

/// <summary>
/// Find the number at the end of the string
/// </summary>
private Regex fileNumber = new Regex("\\d+$", RegexOptions.Compiled);
/// <summary>
/// Get the next free file number given a base file name
/// </summary>
/// <param name="path">Path to base file</param>
/// <returns>Path to free file name</returns>
private string GetFreeFileNumber(string path)
    string pathOnly = path.Substring(0, path.LastIndexOf('\\') + 1);
    string nameOnly = Path.GetFileNameWithoutExtension(path);
    string extOnly = Path.GetExtension(path);
    string[] files = Directory.GetFiles(pathOnly, nameOnly + "*" + extOnly);
    int largest = files.Max(f => GetFileNumber(f));
    return string.Format("{0}{1}{2}{3}", pathOnly, nameOnly, largest + 1, extOnly);
/// <summary>
/// Get the number (if any) at the end of a filename
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private int GetFileNumber(string file)
    Match m = fileNumber.Match(Path.GetFileNameWithoutExtension(file));
    if (!m.Success) return 0;
    return int.Parse(m.Value);

How it works  

First, break the base file path into its important parts: The folder it is in, the file name, and the extension.

Then read all the files in that folder with the same extension, that start with the file name.

Then use a Regex to extract the number from each filename, and use Linq extension methods to find the largest. We can then return a filename which includes a number one larger. 

File.Move[^] can then rename the existing file.  

Using the code  

Easy: Call the GetFreeFileNumber method, passing the existing base file path:



2013 Jun 30 Addition of "+ 1" to the pathOnly substring - otherwise it returns a filename with a "\" missing... :O


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Sudhakar Shinde29-Jun-13 2:15
professionalSudhakar Shinde29-Jun-13 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 30 Jun 2013
Article Copyright 2013 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid