Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:23:26+00:00 2026-06-10T11:23:26+00:00

I need to create backup folders that increase in number. But I need to

  • 0

I need to create backup folders that increase in number. But I need to skip over gaps in numbering if they exist, and make the next folder name ONE higher than the highest numbered folder. For example if I have:

c:\backup\data.1
c:\backup\data.2
c:\backup\data.4
c:\backup\data.5

I need the next folder to be

c:\backup\data.6

The code below works but it feels awful clunky. Is there a better way to do this and still remain in .NET 2.0?

    static void Main(string[] args)
    {
        string backupPath = @"C:\Backup\";
        string[] folders = Directory.GetDirectories(backupPath);
        int count = folders.Length;
        List<int> endsWith = new List<int>();

        if (count == 0)
        {
            Directory.CreateDirectory(@"C:\Backup\Data.1");
        }
        else
        {
            foreach (var item in folders)
            {
                //int lastPartOfFolderName;
                int lastDotPosition = item.LastIndexOf('.');
                try
                {
                    int lastPartOfFolderName = Convert.ToInt16(item.Substring(lastDotPosition + 1));
                    endsWith.Add(lastPartOfFolderName);
                }
                catch (Exception)
                {
                   // Just ignore any non numeric folder endings
                }
            }
        }

        endsWith.Sort();

        int nextFolderNumber = endsWith[endsWith.Count - 1];
        nextFolderNumber++;

        Directory.CreateDirectory(@"C:\Backup\Data." + nextFolderNumber.ToString());
    }

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:23:27+00:00Added an answer on June 10, 2026 at 11:23 am

    Here’s a slightly different version, but basically does the same thing. Find the folder with the max suffix and then add one to that for the next folder.

         static void Main(string[] args)
        {
            string backupPath = @"C:\Backup\";
            string[] folders = Directory.GetDirectories(backupPath);
            Int16 max = 0;
    
            foreach (var item in folders)
            {
                //int lastPartOfFolderName;
                int lastDotPosition = item.LastIndexOf('.');
    
                if (lastDotPosition > -1 && !item.EndsWith("."))
                {
                    Int16 folderNumber;
    
                    if (Int16.TryParse(item.Substring(lastDotPosition + 1), out folderNumber))
                    {
                        if (folderNumber > max)
                        {
                            max = folderNumber;
                        }
                    }
                }
            }
    
            max++;
            Directory.CreateDirectory(@"C:\Backup\Data." + max);
        }
    

    I just ‘cleaned’ your code up a bit to remove the empty catch and the additional list / sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a little program that creates a backup copy of some
I need to create a backup of a SQL Server 2005 Database that's only
I need create clone repository. but I do not know where can I get
I need to set up a job to create backup everyday. I also need
I need to create a backup script (both in batch and bash) which I
I need to create a TSQL script to backup a db on a remote
I have a large number of files which I need to backup, problem is
I need a backup script but the executed program after executing asks Y or
This code create db backup file, but the file name have always the same
I would like to make a backup of my project, but the folder now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.