none
Get TOP 10 Files from directory RRS feed

  • Question

  • Hi Experts,

    I have the below method that reads all the files available in the directory. I am looking forward to read just top 10 oldest files available in the directory path & execute the foreach loop. Please help with modification of this method.

      public  void ProcessExistingFiles(string inputDirectory)
            {
    
                foreach (var filePath in Directory.EnumerateFiles(inputDirectory, "*.*", SearchOption.AllDirectories))
                {
                    Console.WriteLine($"Get TOP 10 oldest files in directory path {filePath}");
    
                }
            }

    Thanks 
    Priya


    Wednesday, April 1, 2020 2:03 PM

Answers

  • Hello,

    For the path passed use this which is a basic framework which may or may not suit your requirements. Also if there are a great deal of folders and files this might need to be async.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
        foreach (var currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
        {
            ProcessExistingFiles(currentDirectory);
        }
    }
    public void ProcessExistingFiles(string inputDirectory)
    {
        var info = new DirectoryInfo(inputDirectory);
        var files = info.GetFiles()
            .OrderByDescending(p => p.LastAccessTime)
            .Take(10)
            .ToArray();
    
        Console.WriteLine(inputDirectory);
        foreach (var file in files)
        {
            Console.WriteLine(file.Name);
        }
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, April 1, 2020 2:55 PM
    Moderator

All replies

  • Hello,

    For the path passed use this which is a basic framework which may or may not suit your requirements. Also if there are a great deal of folders and files this might need to be async.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
        foreach (var currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
        {
            ProcessExistingFiles(currentDirectory);
        }
    }
    public void ProcessExistingFiles(string inputDirectory)
    {
        var info = new DirectoryInfo(inputDirectory);
        var files = info.GetFiles()
            .OrderByDescending(p => p.LastAccessTime)
            .Take(10)
            .ToArray();
    
        Console.WriteLine(inputDirectory);
        foreach (var file in files)
        {
            Console.WriteLine(file.Name);
        }
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, April 1, 2020 2:55 PM
    Moderator
  • Hello,

    For the path passed use this which is a basic framework which may or may not suit your requirements. Also if there are a great deal of folders and files this might need to be async.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
        foreach (var currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
        {
            ProcessExistingFiles(currentDirectory);
        }
    }
    public void ProcessExistingFiles(string inputDirectory)
    {
        var info = new DirectoryInfo(inputDirectory);
        var files = info.GetFiles()
            .OrderByDescending(p => p.LastAccessTime)
            .Take(10)
            .ToArray();
    
        Console.WriteLine(inputDirectory);
        foreach (var file in files)
        {
            Console.WriteLine(file.Name);
        }
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Dear Ma'am,

    Thank you so much for the quick support. I have a question I see here two  foreach loops. Is it possible that we run just the below one 10 times.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
    	var object = GET TOP 10 lastaccess time--> currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories
    	
        foreach ( var filePath in object)
        {
            ProcessExistingFiles(filePath);
        }
    }

    Process Existing file function. I already have some method implemented in it. It requires filepath.. Am sorry if my requirement previously was not clear. 

    Wednesday, April 1, 2020 3:16 PM

  • Dear Ma'am,

    Thank you so much for the quick support. I have a question I see here two  foreach loops. Is it possible that we run just the below one 10 times.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
    	var object = GET TOP 10 lastaccess time--> currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories
    	
        foreach ( var filePath in object)
        {
            ProcessExistingFiles(filePath);
        }
    }

    Process Existing file function. I already have some method implemented in it. It requires filepath.. Am sorry if my requirement previously was not clear. 

    I believe the best way to find out is to alter the code and see yourself :-) 

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 1, 2020 4:37 PM
    Moderator
  • Your code has two foreach loops as well.  You just don't notice that one of them is embedded in the Linq call.

    You have to loop through the directories.  For each directory, you have to loop through the files.  There are always going to be two loops, whether you spell them as "foreach" or spell them with Linq.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, April 1, 2020 5:51 PM
  • Thank you Ma'am , good way for me to learn.. Surely, I will play with code & modify :)
    Thursday, April 2, 2020 3:10 AM
  • Hello,

    For the path passed use this which is a basic framework which may or may not suit your requirements. Also if there are a great deal of folders and files this might need to be async.

    private void button3_Click(object sender, EventArgs e)
    {
        var path = "C:\\Dotnetland";
        foreach (var currentDirectory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
        {
            ProcessExistingFiles(currentDirectory);
        }
    }
    public void ProcessExistingFiles(string inputDirectory)
    {
        var info = new DirectoryInfo(inputDirectory);
        var files = info.GetFiles()
            .OrderByDescending(p => p.LastAccessTime)
            .Take(10)
            .ToArray();
    
        Console.WriteLine(inputDirectory);
        foreach (var file in files)
        {
            Console.WriteLine(file.Name);
        }
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thanks Ma'am again. I was able to modify it as required :)

             var info = new DirectoryInfo(inputDirectory);
                var files = info.EnumerateFiles("*.*", SearchOption.AllDirectories)
                    .OrderByDescending(p => p.LastAccessTime)
                    .Take(10)
                    .ToArray();

    Thursday, April 2, 2020 4:04 AM