RaceBase
RaceBase

Reputation: 18848

How to check if path contains at least one folder C#

Example:

File Paths:         | Output:
-----------------------------
C:\Abc\foo.txt      |  true
C:\Abc\foo\bar.txt  |  true
C:\nodir.txt        |  false
E:\nodir.txt        |  false
C:\Abc\             |  true
C:\Abc\def          |  true

How to find if given path contains at least one folder (excluding the main drive folder like C:\) in a given path.

Currently I am thinking to see if I can split by \ and see it contains multiple elements. Is there any elegant solution for this?

Upvotes: 0

Views: 2033

Answers (6)

Abion47
Abion47

Reputation: 24606

You could check if the path's parent is a root drive:

public bool DoesPathContainFolders(string path)
{
    // Double check in case the given path *is* a root drive.
    string parent = Path.GetDirectoryName(path);
    return parent != null && parent != Path.GetPathRoot(path);
}

Or an alternative approach:

public bool DoesPathContainFolders(string path)
{
    // Double check in case the given path *is* a root drive.
    string parent = Path.GetDirectoryName(path);
    return parent != null && Path.GetDirectoryName(parent) != null;
}

Upvotes: 0

Serge
Serge

Reputation: 4036

Using LINQ to count occurences of the backslash (\) character may be simplest to implement and will yield better performance than Split:

var isRoot = myString.Count(c => c == '\\') > 1;

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39122

Another option:

private bool PathHasAtLeastOneFolder(string path)
{
    return Path.GetPathRoot(path).Length < Path.GetDirectoryName(path).Length;
}

Upvotes: 2

Rob
Rob

Reputation: 27357

As per my comment, there's no way to be sure for cases like C:\nodir.txt because that could either be a file or a folder.

bool CheckIt(string path)
{
    IEnumerable<string> pathItems = path.Split(Path.DirectorySeparatorChar);

    var isFile = System.IO.File.Exists(path);
    if (isFile)
        pathItems = pathItems.Skip(1);

    if (Path.IsPathRooted(path))
        pathItems = pathItems.Skip(1);

    return pathItems.Any();
}

This will provide the correct answer assuming the paths given actually exist on the system.

If you want it to work regardless of whether or not the files exist, you must make the assumption that a path ending with an extension is a file, not a folder. In that case, you'd update the method with:

var isFile = Path.GetFileNameWithoutExtension(path) != Path.GetFileName(path);

Upvotes: 1

Mohit S
Mohit S

Reputation: 14024

This might do the trick for you

if((test.Split('\\').Length - 1)>=2)
{
    //You have more than one folder
}
else
{
    //One file no folder
}

Another idea could be

class Program
{
    static void Main()
    {
        if(TextTool.CountStringOccurrences(Filetest, "\\")>=2)
        {
            //You have more than one folder
        }
        else
        {
            //One file no folder
        }
    }
}

public static class TextTool
{
    public static int CountStringOccurrences(string text, string pattern)
    {
        // Loop through all instances of the string 'text'.
        int count = 0;
        int i = 0;
        while ((i = text.IndexOf(pattern, i)) != -1)
        {
            i += pattern.Length;
            count++;
        }
        return count;
    }
}

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29006

I hope this will help you :- Directory.GetParent will gives you the Directrory info of the parent folder of the Given path. if its parent is null means it is in the root or else it will be a sub folder under the root.

public bool myMethod(string currentPath)
{
    DirectoryInfo currentParent = Directory.GetParent(@"E:\nodir.txt");
    if (currentParent.Parent != null)
    {
        // if current parent is root means Parent will be null
        return true;
    }
    return false;
}

Upvotes: 1

Related Questions