none
Get folder name from directory path RRS feed

  • Question

  • Hi ! 

    I have a requirement where there is a list of folders in a directory path . The folders are named using numbers i.e 1,2,3... 200 etc. There are also other text name folders that s  ould be ignored. How can I get any one numeric folder name that exists currently on the path ?

    Can anyone provide code sample for this ? Any random numeric folder will do , I do not need to load the full list of available folders , because the list could be long. 

    Thanks in advance for help. 

    Monday, May 21, 2018 11:23 AM

Answers

  • Thanks for help but I would imagine this would load the full list of folders first. Is it possible to get a random folder named numerically (using folder mask name or search pattern) , such that any one folder name is returned ? 

    Hello, adding requirements after the first question is not wise, always put all requirements up front.

    To get the first item.

    var numericFolderName = Directory
        .GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory))
        .ToList()
        .Where(fName =>
        {
            string fileName = Path.GetFileName(fName);
            return int.TryParse(fileName, out var test);
        }).FirstOrDefault();

    Get a random name, here are two examples that you can apply to the code above. When you see Take(x), you use .Take(1)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by NdBot Monday, May 21, 2018 1:40 PM
    Monday, May 21, 2018 12:34 PM
    Moderator
  • Hello,

    I'm signed in using my work account.

    Yes the performance would be the same. The only way around this if performance turns out to be an issue is to use a recursive algorithm or enumerate the folder (as done in GetDirectories, see source code) which is not guaranteed to be that much faster. The only way to know is to time both methods on a large array of physical folders.  
    • Marked as answer by NdBot Monday, May 21, 2018 1:40 PM
    Monday, May 21, 2018 1:34 PM

All replies

  • Try the following which in this case is looking at folders below the executable path which of course can be changed.

    The int.TryParse uses C# 6 functionality. For earlier versions of C# see the following.

    private void button1_Click(object sender, EventArgs e)
    {
                
        var folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
        // each entry will have the folder name only
        var numericFolderNames = Directory.GetDirectories(folderName).ToList()
            .Where(fName =>
                {
                    string fileName = Path.GetFileName(fName);
                    return int.TryParse(fileName, out var test);
                }).ToList();
    
                
        foreach (var folder in numericFolderNames)
        {
            // remove parent folder names if any
            Console.WriteLine(Path.GetFileName(folder));
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, May 21, 2018 11:41 AM
    Moderator
  • Thanks for help but I would imagine this would load the full list of folders first. Is it possible to get a random folder named numerically (using folder mask name or search pattern) , such that any one folder name is returned ? 
    Monday, May 21, 2018 12:15 PM
  • Thanks for help but I would imagine this would load the full list of folders first. Is it possible to get a random folder named numerically (using folder mask name or search pattern) , such that any one folder name is returned ? 

    Hello, adding requirements after the first question is not wise, always put all requirements up front.

    To get the first item.

    var numericFolderName = Directory
        .GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory))
        .ToList()
        .Where(fName =>
        {
            string fileName = Path.GetFileName(fName);
            return int.TryParse(fileName, out var test);
        }).FirstOrDefault();

    Get a random name, here are two examples that you can apply to the code above. When you see Take(x), you use .Take(1)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by NdBot Monday, May 21, 2018 1:40 PM
    Monday, May 21, 2018 12:34 PM
    Moderator
  • Thank you very much for your answer. And just to note I did not add new requirement as my original question mentioned that I do not need to load the full list of folders because it could be long. 

    This code works as expected , but just to confirm for my under standing , I still wonder if GetDirectories would internally load the list of folders first or it would simply return first one ? In other words , would the performance be same if there are 1000 or 10000 folders lying in the directory ? Thanks 

    Monday, May 21, 2018 1:13 PM
  • Hello,

    I'm signed in using my work account.

    Yes the performance would be the same. The only way around this if performance turns out to be an issue is to use a recursive algorithm or enumerate the folder (as done in GetDirectories, see source code) which is not guaranteed to be that much faster. The only way to know is to time both methods on a large array of physical folders.  
    • Marked as answer by NdBot Monday, May 21, 2018 1:40 PM
    Monday, May 21, 2018 1:34 PM