GEt last 7days file from a folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remya1000
    New Member
    • Apr 2007
    • 115

    GEt last 7days file from a folder

    i need to get only last 7days files from a folder. using vb.net.

    in my C drive i have a folder called LogFiles, and inside that logfiles folder i have more than 15-25 log files were there. and the name of each file will be the date in which it created.

    eg: 20070607.log (this file created today).
    eg: 20070503.log (created last month 3rd).

    so i need to get the last 7 days log file by checking with current date.

    how can i find the last 7days files from this folder. if you have any idea how to do this please let me know. if example is there, it will be very helpful to me.

    thanks in advance.
  • akipng
    New Member
    • Jun 2007
    • 7

    #2
    Hello

    Here is example I done to search for files by its filename coded date.

    Code:
      
    System.Collections.ArrayList allMatches = new System.Collections.ArrayList();
    string[] files = System.IO.Directory.GetFiles(".", "*.log");
    // gets fullpath or relative path files
    foreach (string fileFullName in files)
    {
        // get only RRRRMMDD from filename
        string fileName = fileFullName.Substring(fileFullName.LastIndexOf('\\') +1,8); // I'm trying to make // only other way and I got some spaces here
        int year =Convert.ToInt32(fileName.Substring(0, 4));
        int month = Convert.ToInt32(fileName.Substring(4, 2)); // if you read us time switch month for days
        int day = Convert.ToInt32(fileName.Substring(6, 2));
        DateTime fileDT = new DateTime(year, month,day);
        TimeSpan searchMargin = new TimeSpan(7, 0, 0, 0);
        TimeSpan fileAge = DateTime.Now - fileDT;
        if (fileAge < searchMargin)
        {
            allMatches.Add(fileFullName);
        }
    }

    cheers
    AkipNG

    Comment

    Working...