vmb
vmb

Reputation: 3028

How to split path to set of Path

I have one path variable like 'Customer\\Calls\\A1\\A2\\A3\\A4'.From this i want split it into an array like

'Customer'
'Customer\Calls'
'Customer\Calls\A1'
'Customer\Calls\A1\A2'
'Customer\Calls\A1\A2\A3'
'Customer\Calls\A1\A2\A3\A4'

If we do it like

 string[] directories = currentFolderPath.Split(Path.DirectorySeparatorChar);

we will get a set of folder,but it will not get in above structure

Can anyone suggest a better approach

Upvotes: 0

Views: 2044

Answers (5)

Avinash Vootla
Avinash Vootla

Reputation: 1

        string path = "Customer\\Calls\\A1\\A2\\A3\\A4";
        var pathArr = path.Split(new string[] { "\\" }, StringSplitOptions.None);
        var pathList = new List<string>();
        while (pathArr.Length != pathList.Count)
        {
            string modifiedPath = "";
            for (int i = 0; i < pathArr.Length; i++)
            {
                modifiedPath = modifiedPath + pathArr[i] + "\\";
                if (i == pathList.Count)
                {

                    pathList.Add(modifiedPath);
                    break;
                }
            }
        }

        foreach (var str in pathList)
        {
            Console.WriteLine(str.Remove(str.Length - 1));

        }
        Console.ReadKey();

Upvotes: 0

Artiom
Artiom

Reputation: 7837

Get parent directory until you get to the root

if it's an absolute path

private static IEnumerable<string> SplitPath(string path){
    do{
        yield return path;
        path=Path.GetDirectoryName(path);
    } while(path!=null);
}

if it is a relative path

private static IEnumerable<string> SplitRelativePath(string path){
    do{
        yield return path;
        var lastIndex=path.LastIndexOf('\\');
        if(lastIndex==-1)
            yield break;
        path=path.Substring(0, lastIndex);
    } while(path!=null);
}

//usage
SplitRelativePath(@"Customer\Calls\A1\A2\A3\A4");


/* result:
C:\Customer\Calls\A1\A2\A3\A4 
C:\Customer\Calls\A1\A2\A3 
C:\Customer\Calls\A1\A2 
C:\Customer\Calls\A1 
C:\Customer\Calls 
C:\Customer 
C:\ *

Upvotes: 2

jarvis
jarvis

Reputation: 81

            // easy and simple try this
            string path = @"Customer\Calls\A1\A2\A3\A4";
            string[] pathArr =  path.Split('\\');
            List<string> list = new List<string>();
            for (int i = 0; i < pathArr.Length; i++)
            {
                string temp = pathArr[i];
                if (i > 0)
                {
                    temp = list[i - 1].ToString() + @"\" + temp;
                }
                list.Add(temp);

            }

Upvotes: 0

Gilad Green
Gilad Green

Reputation: 37299

string path = @"Customer\Calls\A1\A2\A3\A4";
var sections = path.Split('\\').ToList();
var result = Enumerable.Range(0, sections.Count)
                       .Select(index => string.Join(@"\", sections.Take(index + 1)))
                       .ToList();

//Result:
// Customer
// Customer\Calls
// Customer\Calls\A1
// Customer\Calls\A1\A2
// Customer\Calls\A1\A2\A3
// Customer\Calls\A1\A2\A3\A4

Upvotes: 2

Steen T&#248;ttrup
Steen T&#248;ttrup

Reputation: 3835

It's not really obvious from your post, but if you need to do something with each item in your array, I would probably take this approach:

string path = string.Empty;
"Customer\\Calls\\A1\\A2\\A3\\A4".Split(new Char[] { '\\' }).ToList().ForEach(
        part => {
            path = Path.Combine(path, part);
            // Do stuff!
        }
);

Upvotes: 0

Related Questions