Search code examples
c#performancefor-loopsystem.io.directory

Creating Subdirectory function in C#


I am currently attempting to create a function code that creates a subdirectory inside a user-specific path by having the user input the Directory path and then in main use the Directory.GetDirectories(arwgs) function to get a string array of there path.

This code works for the first attempt but after rerunning it again it creates a folder in same directory in the that I don't want to do again.

Good:
Directorys Created S:\Shop....\600\UnitCalFall

Bad:
Directorys Created S:\Shop\600\UnitCalFall\UnitCalFall

or

Directorys Created S:\Shop\600\UnitCalFall\UnitCalDone

I am trying to make the function as fast and integrative as possible so incase the user wants to create more than one or two folders.

The code is shown below:

static void UnitCalFolderCheck(string[] sDirectoryPath, string[] NewFolder)
{
    //possible method can be constructed that checks for if a option UnitCallFolder has been created
    for (int index = 0; index < sDirectoryPath.Length; index++)
    {
        //for each directory in the path if a folder named as UnitCalDONE in order to store CSV files data that has already been stored and conducted. 
        //ig a foldered labeled as such is already created then do not create this folder
        string sCurrentPath = sDirectoryPath[index];

        //check if current directory path already is created by the newfolder length path 
        //NEED TO CREATE A VARIABLE THAT CHECKS IF ANY OF THE SUBSTRINGS ARE TRUE AND IF SO DO NOT CHECK FOR NEW DIRECTORY 
        bool bexist = false;

        //Console.WriteLine(sCurrentPath);
        //also check if a the current path also has the UnitCalFolder Done already. This is because the newDirpath
        //Will be a duplication of each folder and this can cause problems for the for loop
        //append for each dirpath the folder information
        for (int i = 0; i < NewFolder.Length; i++)
        {
            int  NewFolderLength = NewFolder[i].Length;

            string sNewDirPath = sCurrentPath + NewFolder[i];

            string substring = sCurrentPath.Substring(sCurrentPath.Length - NewFolderLength);

            //looping around the new possible created folders based on the substring paths
            foreach (string x in NewFolder)
            {
                //THIS DOESNT CHECK IF FOLDER IS DIFFERENT FROM THE OTHER CONTAINER
                // Console.WriteLine(x);
                if (!substring.Contains(x)) //not working propery 
                {
                    bexist = true;
                }
                else
                {
                    bexist = false;
                }
            }

            if (!Directory.Exists(sNewDirPath) && (bexist == true) )
            {
                Directory.CreateDirectory(sNewDirPath);
                Console.WriteLine("Directorys Created" + sNewDirPath);
            }
        }
    }
}

Solution

  • *A crude way of fixing but when looking back this can work for folders with the suffix "UnitCal". At least for my directory. Not the most elegant but works.

    static void UnitCalFolderCheck(string[] sDirectoryPath, string[] NewFolder)
            {
                
                
                //possible method can be constructed that checks for if a option UnitCallFolder has been created
                for (int index = 0; index < sDirectoryPath.Length; index++)
                {
                    //for each directory in the path if a folder named as UnitCalDONE in order to store CSV files data that has already been stored and conducted. 
                    //ig a foldered labeled as such is already created then do not create this folder
                    string sCurrentPath = sDirectoryPath[index];
                    //Console.WriteLine(sCurrentPath);
                    //int[] iexist = new int[NewFolder.Count()]; //if substring exist already then dont create new directory 
                    //int inotexist = 0;
                    string sNewDirPath;
                    string FolderIndexPath = "";
                    //for every new folder in the path check if the directory with substring already exist
                    for(int x = 0; x < NewFolder.Length; x++)
                    {
                        int NewFolderLength = NewFolder.Length; //length of the new folder to append to newdirpath string
                        //string substring = sCurrentPath.Substring(sCurrentPath.Length - NewFolderLength);
                        //UnitCalDone folder first iteration
                        //UNitCalFall folder secnond iteration
                        Console.WriteLine("========================================================================");
                        Console.WriteLine(sCurrentPath);
                        Console.WriteLine(NewFolder[x]);
                        if (!sCurrentPath.Contains("UnitCal")) 
                        {
                            // iexist[x] = 0;
                            sNewDirPath = sCurrentPath + NewFolder[x];
                            // Determine whether the directory exists.
                            if (!Directory.Exists(sNewDirPath))
                            {
                                //check if new path already exist if so then do nothing 
                                Console.WriteLine("NEWPATH->>>");
                                //Directory.CreateDirectory(sNewDirPath);
                                Console.WriteLine(sNewDirPath); //check that none of this values contain the selected values 
                            }
                        }
                        else
                        {
                            //do nothing 
                            
                        }
                        
                    }
                }//end of for loop method
            }//end of unitCalFolderCheckr code here*