Kerala Microsoft Users Group
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.

Get last created file

Latest post 01-21-2009 8:21 AM by rejeevthomas. 6 replies.
  • 01-20-2009 12:02 AM

    Get last created file

    Hi,

      How to get the last created file from a folder.

      I cant loop through the files because it contains 1000's of files, so whats the other mechanism to achieve this.

    Thanks,

    Rejeev

     

    • Post Points: 25
  • 01-20-2009 12:56 AM In reply to

    Re: Get last created file

    There may be many better options for your requirement... which you only know correctly Smile

    Looping is of course not a good way... but still it is an option. Get List of files (1000 is not a bigger number), sort decending and take the top one.

    Check for FileSystemWatcher class - It listens to the file system change notifications and raises events when a directory, or file in a directory, changes. But not sure if it fits for your requirement.

    Another option is to store the last created filename somewhere ... if you have control over the file creation application.

    PraVeeN
    Microsoft MVP
    blog.ninethsense.com | kidoos.net

    • Post Points: 25
  • 01-20-2009 1:47 AM In reply to

    Re: Get last created file

    Thanks Praveen.  I have a windows app some where and it sends a file to the web and the web will refresh to check the latest file.

    As you said , FileSystemWatcher class and storing last created file will not work in this scenerio, So looping inside the web page will do ....Bothers me the performance if there are thousands of files..

    Thanks,

    Rejeev.

     

    • Post Points: 25
  • 01-20-2009 1:55 AM In reply to

    Re: Get last created file

    What about using a db for filenames and query it?

    PraVeeN
    Microsoft MVP
    blog.ninethsense.com | kidoos.net

    • Post Points: 25
  • 01-20-2009 2:18 AM In reply to

    Re: Get last created file

    The web app is not able to keep the last created file, since some other app is transfering the files, the web app can store only the last read file name, if so, how we will get the last created file.(there may b many files after this) We need to loop again...

    Thanks,

    Rejeev.

    • Post Points: 25
  • 01-21-2009 2:22 AM In reply to

    Re: Get last created file

    If your are using .Net 3.5, you can use LINQ. Use the following code

    //This method returns all files in a specific folder with s pecific extention

    public  System.Collections.Generic.List<FileInfo> GetFiles(string sPath, string sFileExtension) {
                DirectoryInfo _dirInfo = new DirectoryInfo(sPath);
                return System.Linq.Enumerable.ToList(_dirInfo.GetFiles(string.Format("*{0}", sFileExtension), SearchOption.AllDirectories));
            }

     

    //use the below code

     //get all files contained in the path supplied by the user
            System.Collections.Generic.List<FileInfo> _theFiles = GetFiles(@"c:\myDirectory", ".doc");
            //we now have a list of files...next we use LINQ to query the file list and sort in descending order the results by CreationTime
            var _files = from file in _theFiles
                         orderby file.CreationTime descending
                         select file;

           //select the first element
            return _files.First();

    Filed under:
    • Post Points: 25
  • 01-21-2009 8:21 AM In reply to

    Re: Get last created file

    Thanks Shiju..

    • Post Points: 5
Page 1 of 1 (7 items) | RSS
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.