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);
}
}
}
}
*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.