I ve managed to get this far on my first C# app. The problem is when I am adding files and creating a sub directory to a already existing folder it works but when I re-do the action it won't create a new sub directory as the previous one, as is has the same name to that of the new one. I have tried incrementing the folder number to that of the previous folder name but this doesn't work. I know I could when a new sub directory is created to store it as a variable and then loop through to find if that variable is existing, if it's not then create a new one. Any ideas? This is my last bit to do then I have finished it! Yes!

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)
                        dir.Create();

                    DirectoryInfo dis = dir.CreateSubdirectory("newnew" + "(" + iloop + ")");

                    if (!dis.Exists)
                    {
                        iloop = +1;
                        dis.Create();                       
                    }                    
                        // Move files from dir to the new sub directory 
                        //(fri.FullName.ToString(), "File Manager");
                        fri.MoveTo(Path.Combine(dis.FullName, 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());
            }                  
        }

Anyone out there to help??

If you want to use the loop approach then you should use a loop in your code.
At the moment your code basically does this:

Create file 0.
If file 0 doesnt exist, increase loop and create file 0. (EDIT)

Clearly not what you were aiming for. If you are designing a logical process like this, it often helps to use pseudocode first so that you can nail down the flow of execution without getting tied up in syntax.
Something like this:
-Set Counter to zero.
-While File(Counter) exists
-increment counter

-Once Counter reaches a file that doesnt exist
-Create File(Counter)

I assume that you want to create a new subdirectory for each time the method is executed?

That's right. I do want a new sub directory every time the method is call. I ll give that suggestion a go and see how I get on. Thanks for the reply.

Is this along the lines.....

if (!dis.Exists == true)
                    {
                        counter = +1;
                    }
                    if (!dis.Exists == false)
                    {
                        dis.Create();
                        counter++;
                    }

No, an if statement will only be checked once, you need to check indefinitely until the file doesnt exist:

DirectoryInfo di = new DirectoryInfo(@"C:\new1");
            FileInfo[] fiArr = di.GetFiles("*.txt");
            int iCtr = 0;
            int iloop = 0; //Set Counter to zero.

            try
            {
                foreach (FileInfo fri in fiArr)
                {
                    DirectoryInfo dir = new DirectoryInfo(@"C:\new2");      
                    if (!dir.Exists)
                        dir.Create();

                    DirectoryInfo subDir = new DirectoryInfo(Path.Combine(dir.FullName, "newSub("+ iLoop + ")"));
                    while (subDir.Exists) //While File(Counter) exists
                    {
                        iloop++; //increment counter
                        subDir = new DirectoryInfo(Path.Combine(dir.FullName, "newSub("+ iLoop + ")"));                      
                    }
                    //Once Counter reaches a file that doesnt exist
                    //Create File(Counter)   
                    subDir.Create();

PLEASE PLEASE PLEASE do not just copy and paste this into your project. I have writetn the code for you so that you can see how the transition from pseudocode to actual code occurs.
I have added the pseudocode in as comments so you can see how i have selected code blocks to perform the task at each stage.
If you havent used them before i suggest you google While Loops and learn how they work :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.