5YrsLaterDBA
5YrsLaterDBA

Reputation: 34760

what is a good way to remove last few directory

I need to parse a directory string I get and remove last few folders.

For example, when I have this directory string:

C:\workspace\AccurevTestStream\ComponentB\include

I may need to cut the last two directores to create a new directory string:

C:\workspace\AccurevTestStream

what is a good way to do this? I know I can use string split and join but I think there may be a better way to do this.

Upvotes: 9

Views: 9483

Answers (8)

A-Sharabiani
A-Sharabiani

Reputation: 19329

The easiest way to do this:

string path = @"C:\workspace\AccurevTestStream\ComponentB\include"
string newPath = Path.GetFullPath(Path.Combine(path, @"..\..\"));

Note This goes two levels up. The result would be: newPath = @"C:\workspace\AccurevTestStream\";

Upvotes: 1

Nathan Taylor
Nathan Taylor

Reputation: 24606

Here's a simple recursive method that assumes you know how many parent directories to remove from the path:

public string GetParentDirectory(string path, int parentCount) {
    if(string.IsNullOrEmpty(path) || parentCount < 1)
        return path;

    string parent = System.IO.Path.GetDirectoryName(path);

    if(--parentCount > 0)
        return GetParentDirectory(parent, parentCount);

    return parent;
}

Upvotes: 11

Brian
Brian

Reputation: 38025

What about this (sorry, I don't know what your criteria is for determining what to delete)...

var di = new System.IO.DirectoryInfo("C:\workspace\AccurevTestStream\ComponentB\include");
while (!deleteDir)
    di = di.Parent;
di.Delete(true);

Upvotes: -1

QrystaL
QrystaL

Reputation: 4966

    static String GoUp(String path, Int32 num)
    {
        if (num-- > 0)
        {
            return GoUp(Directory.GetParent(path).ToString(), num);
        }
        return path;
    }

Upvotes: 1

Philip Rieck
Philip Rieck

Reputation: 32568

You could use the System.IO.Path class in this case - if you call Path.GetDirectoryName repeatedly, it will chop off the last path:

string path = @"C:\workspace\AccurevTestStream\ComponentB\include";
path = Path.GetDirectoryName(path); //returns C:\workspace\AccurevTestStream\ComponentB
path = Path.GetDirectoryName(path); //returns C:\workspace\AccurevTestStream
//etc

Upvotes: 4

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

var path = "C:\workspace\AccurevTestStream\ComponentB\include";    
DirectoryInfo d = new DirectoryInfo(path);
var result = d.Parent.Parent.FullName;

Upvotes: 14

public static void
public static void

Reputation: 11

I'd go with the DirectoryInfo class and its Parent property.

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

Upvotes: 1

Joel Etherton
Joel Etherton

Reputation: 37533

You might try:

myNewString = myOriginalString.SubString(0, LastIndexOf(@"\"));
myNewString = myNewString.SubString(0, LastIndexOf(@"\"));

Not elegant, but should be effective.

Edit: (even more inelegant)

string myNewString = myOriginalString;
for(i=0;i<NumberToChop;i++)
{
    if(LastIndexOf(@"\") > 0)
        myNewString = myNewString.SubString(0, LastIndexOf(@"\"));
}

Upvotes: 1

Related Questions