0

I want to create multiple files in a directory, within their subfolders but can't seem to think it through. In the picture below, what I want it to do is look within the main directory (in this case "File Location"), then go to the sub folder, then into the next subfolder within it and if the folder does not exist will create it.

So the path would be: "File Location\2015\15-01" and it will look for a sub folder called "Hello" and if it doesn't exist it would create that folder, and then loop through all the sub folders within "File Location".

How would I create this via c#?

Example

learning
  • 15
  • 3
  • Please choose one language tag for your question (either C# or batch). – Rufus L Aug 30 '18 at 22:36
  • 1
    Batch files can use `for` to loop through all entries in a directory. You can check for existance with `if exist filename` This could be made recursive too. The question is, what do you want to do in those sub folders? – Michael Dorgan Aug 30 '18 at 22:39
  • 3
    For C#, https://stackoverflow.com/questions/724148/is-there-a-faster-way-to-scan-through-a-directory-recursively-in-net – Michael Dorgan Aug 30 '18 at 22:41

1 Answers1

0

Try this. Below code is for c#

string path = @"......."; //Your complete path
string[] directories = path.Split(Path.DirectorySeparatorChar);

It will give you the folder names. Then you can check for each path

var i = 0;
string folderPath = directories[i];

while(i < directories.length)
{
        bool exists = System.IO.Directory.Exists(folderPath);

        if(!exists)
            System.IO.Directory.CreateDirectory(folderPath);

        i++;
}
ManishM
  • 583
  • 5
  • 7