How do I create multiple directories from range with special naming nomenclature?

Here is a screen shot of the application thus far and what it should produce with the information typed into the form.

Screen Shot of the form and an example out put of the form.
Here is the code I have so far from the form.
using System;
using System.IO;
using System.Windows.Forms;

namespace DirMaker
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void clearFolder(string FolderName)
        {
            DirectoryInfo dir = new DirectoryInfo(FolderName);

            foreach (FileInfo fi in dir.GetFiles())
            {
                fi.Delete();
            }

            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                clearFolder(di.FullName);
                di.Delete();
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtPath.Text = dialog.SelectedPath;
                }
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string rootDir = txtPath.Text;
            string prefix = txtPrefix.Text;
            string first = txtFirstNum.Text;
            string last = txtLastNum.Text;
            string suffix = txtSuffix.Text;
            string fDir = prefix + first + suffix;
            string _fDir = Path.Combine(rootDir, fDir);
            string lDir = prefix + last + suffix;
            string _lDir = Path.Combine(rootDir, lDir);
            int diff = Convert.ToInt32(txtLastNum.Text) - Convert.ToInt32(txtFirstNum.Text);

            if (Directory.Exists(rootDir) == true)
            {
                clearFolder(rootDir);

                Directory.CreateDirectory(_fDir);



            }
            else
            {
                MessageBox.Show("Incorrect Path, Directory does not exist", "MISSING DIRECTORY ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}

Open in new window


I need help with the code to generate multiple directories. I was thinking of creating an array list something like this:

private void GenerateNumbers()
        {
            string rootDir = txtPath.Text;
            string[] dirList;
            List<String> dir = new List<String>();
            DirectoryInfo directoryInfo = new DirectoryInfo(rootDir);
            foreach (DirectoryInfo i in directoryInfo.GetDirectories())
            {
                dir.Add(i.ToString());
                dirList = dir.ToArray();               
            }
        }

Open in new window


to look for the all the folders in the root directory. But I'm not sure what I can do with it from there. I'm not very experienced with array's. I was thinking of using a loop look at the array, find the highest number in the array and add 1 to the number to generate the next folder. It would continue to loop and add folders until it see's that the Last No. has already been created which would end the loop.

Any help would be greatly appreciated
LVL 1
Steve WilliamsProduct Design EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jayadev NairApplication DeveloperCommented:
var start = Int32.Parse(txtStart.text);
var end = Int32.Parse(txtEnd.text);
var dirList = Enumerable.Range(start, end - start)
                .Select(x => $"{txtPrefix.text}x").ToList();

Open in new window


Then itertate and create directories
0
Steve WilliamsProduct Design EngineerAuthor Commented:
Your solution doesn't work, it creates one folder with the prefix only as the name.

Here is the code.
 private void btnGenerate_Click(object sender, EventArgs e)
        {
            string rootDir = txtPath.Text;

            if (Directory.Exists(rootDir) == true)
            {
                clearFolder(rootDir);

                var _first = Int32.Parse(txtFirstNum.Text);
                var _last = Int32.Parse(txtLastNum.Text);
                var dirList = Enumerable.Range(_first, _last - _first).Select(x => $"{txtPrefix.Text}").ToList();

                foreach (var f in dirList)
                {
                    string fPath = Path.Combine(rootDir, f);
                    Directory.CreateDirectory(fPath);
                }

            }
            else
            {
                MessageBox.Show("Incorrect Path, Directory does not exist", "MISSING DIRECTORY ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

Open in new window

0
Dustin SaundersDirector of OperationsCommented:
Doesn't need to be that complicated, just iterate through and if the directory doesn't already exist create it.

You should add in some condition checking first though, to make sure the input is valid (int.TryParse) or restrict the input to only numbers on those text boxes.

Probably not a bad idea to .trim() the prefix/suffix.

Might also want to check if start > end number and invert them if so.

But the code to make the directories can look as such.  Assumes the directory path ends with a "\" otherwise you need to add it in.  You probably want to validate this as well (you could do a substring to get the last character and if it's not "\" add it.)

            int start = int.Parse(txtFirstNum.Text);
            int end = int.Parse(txtLastNum.Text);

            for (int i = start; i <= end; i++)
            {
                string folder = txtPath.Text + txtPrefix.Text + i.ToString() + txtSuffix.Text;
                if (!Directory.Exists(folder))
                    Directory.CreateDirectory(folder);
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve WilliamsProduct Design EngineerAuthor Commented:
Here is the finished working code
 private void btnGenerate_Click(object sender, EventArgs e)
        {
            string rootDir = txtPath.Text;

            if (Directory.Exists(rootDir) == true)
            {
                clearFolder(rootDir);

                int start = Int32.Parse(txtFirstNum.Text);
                int end = Int32.Parse(txtLastNum.Text);

                for (int i = start; i <= end; i++)
                {
                    string folder = Path.Combine(txtPath.Text, txtPrefix.Text + i.ToString() + txtSuffix.Text);
                    if (Directory.Exists(folder) == false)
                    {
                        Directory.CreateDirectory(folder);
                    }
                }

            }

Open in new window

0
Steve WilliamsProduct Design EngineerAuthor Commented:
Your code works like a charm. Thank you for your help! Enjoy the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.