1

I am a little confused how to get the nested folders name from a given path. There could be no folders to many folders.

here is what I've done. I've also tried to substring but then I get 1 result returned and its the same for the entire file.

foreach (string fL in GetDirectories(targetDirectory))
            {
                foreach (string fN in ProcessDirectory(fL))
                {
                    string[] subType = Directory.GetDirectories(directory);

                    myDocs.Add(new Documents
                    {
                        Id = "1",
                        Directory = directory,
                        Type = type,
                        FilePath = filePath,
                        FileName = fN,
                        Description = description,
                        SubType = subType
                    });
                }
            }

The output that I get looks like:

{
    "Id": "1",
    "Directory": "C:/aaa/bbb/ccc/ddd/eee",
    "Type": "QA",
    "FilePath": "C:/aaa/bbb/ccc/ddd/eee/New Text Document.txt",
    "FileName": "New Text Document.txt",
    "Description": "New Text Document",
    "SubType": [
        "C:/aaa/bbb/ccc/ddd/eee\\New folder",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (2)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (3)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (4)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (5)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (6)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (7)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (8)",
        "C:/aaa/bbb/ccc/ddd/eee\\New folder - Copy (9)"
    ]
}

Desired result:

{
    "Id": "1",
    "Directory": "C:/aaa/bbb/ccc/ddd/eee",
    "Type": "QA",
    "FilePath": "C:/aaa/bbb/ccc/ddd/eee/New Text Document.txt",
    "FileName": "New Text Document.txt",
    "Description": "New Text Document",
    "SubType": [
        "New folder",
        "New folder - Copy",
        "New folder - Copy (2)",
        "New folder - Copy (3)",
        "New folder - Copy (4)",
        "New folder - Copy (5)",
        "New folder - Copy (6)",
        "New folder - Copy (7)",
        "New folder - Copy (8)",
        "New folder - Copy (9)"
    ]
}
Luther
  • 87
  • 1
  • 9
  • Is this on Windows? I have never seen `Directory.GetDirectory()` return paths with a forward slash. – Tanveer Badar Nov 20 '19 at 15:42
  • @SimonC Unfortunately, it doesn't. I need it to be in an array because I'll have a folder with many folders nested inside and I need to get those nested folders names and store it in an array. I've tried it but maybe I'm implementing it wrong :( – Luther Nov 20 '19 at 15:42
  • @TanveerBadar correct it is windows, I replaced the "\\" with a "/". – Luther Nov 20 '19 at 15:43

1 Answers1

3

You can use Path.GetFileName() to get the name of a directory from the full path. Yes, it's called GetFileName, but it literally just takes everything after the last slash. (it doesn't actually look at the file system)

If you're ok using Linq, you can do this:

string[] subType = Directory.GetDirectories(directory)
                            .Select(Path.GetFileName).ToArray();
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Just need to add `using System.Linq;` to the top of your file. Linq's [`Select`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select) is really just the same as a `foreach` loop. – Gabriel Luci Nov 20 '19 at 16:00
  • Just out of curiosity, why did you mention if you're okay with using Linq? – Luther Nov 20 '19 at 16:18
  • I don't think anyone would object to using it here. But sometimes, in more complicated statements, it can be difficult to decipher what it's actually doing, as opposed to a `foreach` loop. – Gabriel Luci Nov 20 '19 at 16:28