简体   繁体   中英

Get specific folder from a path string

Consider the path to a file like this:

"\\\\Tests\\Results\\knowles\\project\\LU\\D15\\RUN1\\Results.xml"

How can I make a new string that gives the path to the "project" folder in the string above? I tried to use FirstIndexOf("project") but it always fails!

Is there a static method or something in Directory or Path class?

I want to have a new string which contains the path to the "project" folder, from similar strings like above example, something like

var newPath = "\\\\\\\\Tests\\\\Results\\\\knowles\\\\project"

Please note, the "project" folder is just an example, it can be any name, what I ment is that I want to get the path to the folder that suppose to be in place of the project in my example.

Again, my mistake I guess, the original string does not come always with fixed number of folders, it is for sure does have atleast one or two folders after the 'project' folder...I need a way to approach it from left to right I guess

You can use the FileInfo and DirectoryInfo class.

Do a right to left search as follows:

var fi = new FileInfo(""\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml"");
var di = fi.Directory;
var find = "project";

while (di.Name.ToLower() != find.ToLower() && di != null)
{
    di = di.Parent;
}

if (di == null)
{
    throw new Exception(string.Format("Directory with name '{0}' was not found.", find));
}

Do a left to right search with a recursive solution. Something like this:
NOTE This is untested so may not be exactly right, but I think it's generally what you are trying to achieve.

var fi = new FileInfo(""\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml"");
var di = fi.Directory;
var find = "project";

di = GetGreatestParent(di, find);

if (di == null)
{
    throw new Exception(string.Format("Directory with name '{0}' was not found.", find));
}

public DirectoryInfo GetGreatestParent(DirectoryInfo Dir, string Find)
{
    if (Dir != null)
    {
        var p = GetGreatestParent(Dir.Parent, string Find);

        if (p != null)
        {
            return p;
        }
        else if (Dir.Name.ToLower() == Find.ToLower())
        {
            return Dir;
        }
    }

    return null;
}

Try this, for this instance.

string path = @"\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml";
string folderToSearch = "project";
int index = path.IndexOf(folderToSearch);
if (index != -1)
  Console.WriteLine(path.Substring(0, index + folderToSearch.Length));
else
  Console.WriteLine("Folder not found in given string.");
Console.Read();

Use Regex:

var projectName = "project";
var inputString = @"\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml";

var yourProjectDir = Regex.Match(inputString, 
                                 String.Format(@"\\.+{0}\\", projectName),
                                 RegexOptions.IgnoreCase).Value;

You may use Path class , it has many helping methods , try to find one which suits your need.

You may try to use the following code to get the folder in the path, it will give you the last one.

string folder = Path.GetDirectoryName(@"C:\\Abc\test.xml");

will give you C:\\\\ABC for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM