• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 62
  • Last Modified:

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
0
Steve Williams
Asked:
Steve Williams
  • 3
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now