简体   繁体   中英

Getting files between two dates

How to list the files between two dates. Now it goes through the every files in the folder and returns all.

  protected void btnListItems_Click(object sender, EventArgs e)
{
    lblMessage.Visible = false;
    //lblEnddatse.Visible = true;
    Boolean status = true;
    Util objUtil = new Util();
    String Message = "";
    DateTime SDate = new DateTime();
    DateTime EDate = new DateTime();
    string str = "";
    DataTable tbl = new DataTable();
    DataTable dt = new DataTable();
    DataRow dr;
    String[] s1;


    //DirectoryInfo d = new DirectoryInfo();
    s1 = Directory.GetFiles(@"C:/PCRequestFiles");
    for (int i = 0; i <= s1.Length - 1; i++)
    {
        if (i == 0)
        {
            //Add Data Grid Columns with name
            dt.Columns.Add("FileName");
            dt.Columns.Add("GeneratedTime");
        }
        //Get each file information
        FileInfo f = new FileInfo(s1[i]);
        FileSystemInfo f1 = new FileInfo(s1[i]);
        dr = dt.NewRow();
        //Get File name of each file name
        dr["FileName"] = f1.Name;
        dr["GeneratedTime"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
        string a = f1.CreationTime.Date.ToString("dd/MM/yyyy");
        //Insert collected file details in Datatable
        string fromdate = txtFromDate.Text.ToString();
        string todate = txtToDate.Text.ToString();

        dt.Rows.Add(dr);



        if ((f.Length / 1024) > 5000)
        {
           lblMessage .Text = "" + f1.Name + " had reach its size limit.";
        }
        else
        { }

    }
    if (dt.Rows.Count > 0)
    {
        gvFileGenStatus.DataSource = dt;
        gvFileGenStatus.DataBind();
    }





}

I tried to give the conditions but it doesn't work.Is there anyway to use date filter to list of file names from a directory. Thanks for all the help in advance ).

I'm sure this will help:

DirectoryInfo DirInfo = new DirectoryInfo(@"C:/PCRequestFiles");

var files = from f in DirInfo.EnumerateFiles()
       where f.CreationTimeUtc < EndDate && f.CreationTimeUtc > StartDate
       select f;

You can use the following code to get the files(in the directory and its sub-directories) that was created in between specified start date and specified end date.

 DateTime endTime = DateTime.Now;
 DateTime starttime = endTime.AddDays(-2);
 string searchPattern="*.*";
 var filesBetweenDates = Directory.GetFiles("Path to your directory", searchPattern,SearchOption.AllDirectories).Where
(f => new FileInfo(f).CreationTime > starttime && new FileInfo(f).CreationTime < endTime);

You can do something like this

foreach (FileInfo flInfo in directory.GetFiles())
{
    DateTime yesterday = DateTime.Today.AddDays(-1);
    String name = flInfo.Name.Substring(3,4);
    DateTime creationTime = flInfo.CreationTime;
    if (creationTime.Date == yesterday.Date)
       yesterdaysList.Add(name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM