Hello,

I am trying to create a new sub directory in an all ready created Folder. What happens is that when files are transferred from one directory to another it creates a sub directory everytime and places the files into it. I have got the part working for the creation of a folder but everytime the files go into the main folder it gets placed into the same sub folder and doesn't create a new one for the files to be placed into. Any help???

Thanks.

private void butSort1_Click(object sender, EventArgs e)
        {             
            DirectoryInfo di = new DirectoryInfo(@"C:\new1");
            FileInfo[] fiArr = di.GetFiles("*.txt");
            int iCtr = 0;
            int iloop = 0;

            try
            {
                foreach (FileInfo fri in fiArr)
                {
                    //create a new directory
                    DirectoryInfo dir = new DirectoryInfo(@"C:\\new2");
                    //If that directory doesn't exsit create a new directory         
                    if (dir.Exists == false)
                    {
                        dir.Create();
                    }
                    DirectoryInfo dis = dir.CreateSubdirectory("newnew" + "(" + iloop + ")");
                    if (dis.Exists == true)
                    {
                        dis.Create();
                    }
                    else
                    {
                        //move files from dir to the new sub directory 
                        MessageBox.Show(fri.FullName.ToString(), "File Manager", MessageBoxButtons.OK);
                        fri.MoveTo(@"C:\new2\newnew\" + "1(" + iCtr + ").txt");
                        //loop round to add the file name 
                        iCtr++;
                        iloop++;
                        //Refresh the label attributes
                        RefreshDirSize1();
                        RefreshDirSize2();
                        RefreshDirSize3();
                        RefreshDirSize4();
                    }
                }
            }

            catch (Exception b)
            {
                lblErrors.Text = (b.ToString());
            }
        }

Edited 6 Years Ago by peter_budo: Correcting closing tag. Please use [/code] instead of [\code]

Check you logic here

if (dis.Exists == true) <--- ?
{
    dis.Create();
}

Also, you are using a fixed string here

fri.MoveTo(@"C:\new2\newnew\" + "1(" + iCtr + ").txt");

Edited 6 Years Ago by nick.crane: n/a

Check your Output window in VS; Unless you have manually created the folder "C:\new2\newnew" you may find you have some entries that read "A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll"
As nick.crane suggested, you need to re-examine your logic and your path strings...if you have the paths stored as DirectoryInfo objects, why are you using fixed string paths?

I have changed some of it. I realise the fixed string and have modified it a abit but still get the exception message of "Could not find part of the path", which could be down to the fixed string. Any ideas how to get it working? I ve tried everything I can thing of.

Thanks.

Updated code:

private void butSort1_Click(object sender, EventArgs e)
        {             
           DirectoryInfo di = new DirectoryInfo(@"C:\new1");
            FileInfo[] fiArr = di.GetFiles("*.txt");
            int iCtr = 0;
            int iloop = 0;
          
            try
            {
                foreach (FileInfo fri in fiArr)
                {
                    //create a new directory
                    DirectoryInfo dir = new DirectoryInfo(@"C:\new2");
                    //If that directory doesn't exsit create a new directory         
                    if (!dir.Exists)
                        dir.Create();
						
                    DirectoryInfo dis = dir.CreateSubdirectory("newnew" + "(" + iloop + ")");
                   
				   	if (dis.Exists)
                        dis.Create();
						
                    // Move files from dir to the new sub directory 
                     MessageBox.Show(fri.FullName.ToString(), "File Manager", MessageBoxButtons.OK);
                     fri.MoveTo(@"C:\new2\newnew" + iloop + "\\" + iCtr + ".txt");
                     //loop round to add the file name 
                     iCtr++;
                     //Refresh the label attributes
                     RefreshDirSize1();
                     RefreshDirSize2();
                     RefreshDirSize3();
                     RefreshDirSize4();
                    }
                }            

            catch (Exception b)
            {
                lblErrors.Text = (b.ToString());
            }                  
        }

You havent actually corrected either of the problems...
Your code if (dis.Exists) dis.Create(); says that if the directory exists it should be created :/ I think you may have meant to add the '!' not operator.
Also, you are still using a fixed string for the file move:
C:\new2\newnew" + iloop + "\\" => C:\new2\newnew0\
dir.CreateSubdirectory("newnew" + "(" + iloop + ")") => C:\new2\newnew(0)\

Instead of fri.MoveTo(@"C:\new2\newnew" + iloop + "\\" + iCtr + ".txt"); why not use "dis"? dis.FullName will contain the full path to your newly created subdirectory.

Edited 6 Years Ago by Geekitygeek: n/a

Comments
A very controlled reply

I see. I am still getting my head round C#.

Would this be correct?

fri.MoveTo(@"C:\new2\dis.fullname" + iloop + "\\" + iCtr + ".txt");

or

fri.MoveTo("dis.fullname" + "\\" + iCtr + ".txt");

Because dis.fullname contains the Full address name?

Using this:

fri.MoveTo("dis.fullname" + iCtr + ".txt");

Has created the new folder but has sent it somewhere? I am not sure where lol

Actually, none of the above. Try this.

fri.MoveTo(Path.Combine(dis.FullName, iCtr + ".txt");

Also, in this bit of code FullName is already a string and so ToString() is not needed.

MessageBox.Show(fri.FullName.ToString(), "File Manager", MessageBoxButtons.OK);
Comments
Path.Combine is defenitely the way to go :)
This question has already been answered. Start a new discussion instead.