check for specific folder name in directory path

jassie

Well-known member
Joined
Nov 13, 2012
Messages
61
Programming Experience
1-3
In a C# 2008 desktop application, I would like to come up with a test to see if the value 'CUST' is a valid directory level within a directory path supplied to the program. Basically the directory path would look like:
"C:\RData\CUST\Omaha\book.xlsx".
The code I am using the following code, but it is not working:
string filesaveLocation=ConfigurationSettings.AppSettings["Location"];
if (filesaveLocation == "*CUST*").
The value for filesaveLocation is obtained from the app.config file.
Thus can you show me how to change the code listed above, so that I can verify that 'CUST' is one of the directory folder names listed in a file path that looks like "C:\RData\CUST\Omaha\book.xlsx"?

I also have to test to see if the directory file path does not = 'CUST', in the directory path called ""C:\RData\CUST\Omaha\book.xlsx". Can you tell me how to code that change?
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
You appear to be trying to do a wildcard search but you are using the equality operator '=='. Equality tests for just that. In SQL, for example, you use LIKE with wildcards. Unlike VB, C# has no LIKE operator of its own. Even if it did though, what you're doing wouldn't be appropriate because it doesn't guarantee that CUST is not just part of the folder name. I would suggest writing a method that takes a path and a folder name and returns a bool to indicate whether that folder is in that path, e.g.
/// <summary>
/// Determines whether a folder exists in a path.
/// </summary>
/// <param name="folderName">
/// The name of the folder to search for.
/// </param>
/// <param name="path">
/// The path to search in.
/// </param>
/// <returns>
/// <b>true</b> if the folder exists in the path; otherwise, <b>false</b>.
/// </returns>
private bool IsFolderInPath(string folderName, string path)
{
    var result = false;

    // Convert relative paths to absolute.
    path = System.IO.Path.GetFullPath(path);

    var pathRoot = System.IO.Path.GetPathRoot(path);

    while (path != pathRoot)
    {
        // GetFileName just gets the leaf node name, whether file or folder.
        var leaf = System.IO.Path.GetFileName(path);

        // Perform a case-insensitive comparison.
        if (leaf.Equals(folderName, StringComparison.CurrentCultureIgnoreCase))
        {
            // A match was found.
            result = true;
            break;
        }

        // Remove the leaf from the path.
        path = System.IO.Path.GetDirectoryName(path);
    }

    return result;
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
811
Location
Norway
Programming Experience
10+
A regex should also be able to do it, for example:
string input = "C:\\RData\\CUST\\Omaha\\book.xlsx";
string pattern = string.Format("\\\\{0}\\\\", "cust");
bool found = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
A regex should also be able to do it, for example:
string input = "C:\\RData\\CUST\\Omaha\\book.xlsx";
string pattern = string.Format("\\\\{0}\\\\", "cust");
bool found = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
You may have to adjust that to detect the folder if it's the leaf node in the original path, although that will never be the case if it's always a file path.
 
Top Bottom