Get a specific directory level

I would like to get the original folder name by dynamic path and username and without the Windows default directory

For example,

 C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls
 C:\Users\dynamic user\Desktop\History\record.xls

      

In this case, I want it to return "History" for excel.xls and record.xls. I tried using the GetFilename () method. But it just returns

 Spreadsheets - excel.xls
 History      - record.xls

      

Is it possible to achieve? Thanks for your help.

+3


source to share


4 answers


Yes, as documentedPath.GetFileName

;

Returns the filename and extension of the specified path string.

One solution might be to remove your path Desktop

and split into Path.DirectorySeparatorChar

and get the second element.



For example:

string path = @"C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls";
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

path = path.Replace(desktop, "");
var history = path.Split(Path.DirectorySeparatorChar)[1];
Console.WriteLine(history); // History

      

Remember, Environment.SpecialFolder.Desktop

returns the user's current desktop information.

0


source


There is no built-in way to do what you want. The following code should do the trick:

    public static string GetFolderByLevel(this string path, string baseFolderName, int level)
    {
        if (path == null)
            throw new ArgumentNullException("path");

        if (baseFolderName == null)
            throw new ArgumentNullException("baseFolderName");

        var pathWithoutFile = Path.GetDirectoryName(path);

        var folders = pathWithoutFile.ToString().Split(Path.DirectorySeparatorChar);
        int baseFolderLevel = -1;

        for (int i = 0; i < folders.Length; ++i)
        {
            if (string.Compare(folders[i], baseFolderName, true) == 0)
            {
                baseFolderLevel = i;
                break;
            }
        }

        if (baseFolderLevel == -1)
            throw new ArgumentException(string.Format("Folder '{0}' could not be found in specified path: {1}", baseFolderName, path), "baseFolderName");

        int index = baseFolderLevel + level;

        if (-1 < index && index < folders.Length)
        {
            return folders[index];
        }
        else
            throw new ArgumentOutOfRangeException(string.Format("Specified level is out of range."));
    }

      

Now you can use it like:

string path = @"C:\Users\dynamic user\Desktop\History\Peter\record.xls";

path.GetFolderByLevel("Desktop", -2); //returns "Users"
path.GetFolderByLevel("History", 0); //returns "History"
path.GetFolderByLevel("Desktop", 1); //returns "History"

      

In your case, if I am not mistaken, you are looking for path.GetFolderByLevel("Desktop", 1);



UPDATE . I modified the previous solution so that you can specify the base folder with a partial or full path:

public static string GetFolderByLevel(this string path, string baseFolderPath, int level)
{
    if (path == null)
        throw new ArgumentNullException("path");

    if (baseFolderPath == null)
        throw new ArgumentNullException("baseFolderName");

    var pathFolders = path.Split(new char[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
    var basePathFolders = baseFolderPath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

    int baseFolderIndex = -1;
    int folderCounter = 0;

    for (int i = 0; i < pathFolders.Length; ++i)
    {
        if (string.Compare(pathFolders[i], basePathFolders[folderCounter], true) == 0)
        {
            if (++folderCounter == basePathFolders.Length)
            {
                baseFolderIndex = i;
                break;
            }
        }
        else
        {
            folderCounter = 0;
        }
    }

    if (baseFolderIndex < 0)
       throw new ArgumentException(string.Format("Folder '{0}' could not be found in specified path: {1}", baseFolderPath, path), "baseFolderName");

    int index = baseFolderIndex + level;

    if (-1 < index && index < pathFolders.Length)
    {
        return pathFolders[index];
    }
    else
        throw new ArgumentOutOfRangeException(string.Format("Specified level is out of range."));
}

      

Now you can use it like: path.GetFolderByLevel(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), 1);

An additional benefit can be that there are subfolders with the same name, you can provide a unique partial path to the base folder to make sure the method picks the correct one.

0


source


So, if I understood that you want to find a common base path from a set of paths? There is no built-in function for this, so your spin on string matching and some LINQ gives this:

        static void Main(string[] args)
    {
        List<string> paths = new List<string>()
            {
                @"C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls"
                ,@"C:\Users\dynamic user\Desktop\History\record.xls"
                ,@"C:\Users\dynamic user\Desktop\History\2014-11-23\Spreadsheets\excel.xls"
                ,@"C:\Users\dynamic user\Desktop\History\2014-11-03\excel.xls"
            };

        Console.WriteLine("The common base path of:");
        paths.ForEach(f => Console.WriteLine(f));
        Console.WriteLine("is");
        Console.WriteLine(FindBasePath(paths));

        Console.ReadLine();
    }

    static string FindBasePath(List<string> paths)
    {
        string basePath = String.Empty;

        foreach (string path in paths)
        {
            string dirName = Path.GetDirectoryName(path);

            if (paths.All(f => Path.GetDirectoryName(f).Contains(dirName)))
                return basePath = dirName;
        }

        return basePath;
    }

      

Forgot to add a comment to what we are doing. Basically we iterate over each path and get the name of the directory, then if ALL paths contain this string, it should be the common root directory.

Note that this will check every path in every other path ... and could be improved.

You can also do this with a single LINQ statement:

paths.Select(f => Path.GetDirectoryName(f)
     .Select(f => paths.All(g => g.Contains(f)) ? f : "")
     .Where(f => !String.IsNullOrEmpty(f))
     .First()

      

0


source


Or that:

 var dir_path = Path.GetDirectoryName(@"C:\your\path\with\file.ext");
 var dir_name = new DirectoryInfo(dir_path).Name;

      

-1


source







All Articles