Hey,

I'm trying to compare dates, essentially I want to copy all files that have a date modified of 3 days older or less than 3 days old modified. So far i have the following, it did look to be working before, maybe i made some mistake below:

string fileName = "";

                string sourceFile = System.IO.Path.Combine(ExpandedSystemRoot, fileName);
                string destFile = System.IO.Path.Combine(targetdir, fileName);
                string[] files = System.IO.Directory.GetFiles(ExpandedSystemRoot, "*.flog");
                DirectoryInfo dirinfo = new DirectoryInfo(ExpandedSystemRoot);
                System.IO.FileInfo[] fileinfo = dirinfo.GetFiles("*.flog");

               foreach (FileInfo fi in fileinfo)
                {
                    DateTime nowdate = DateTime.Today;
                    DateTime filedate = fi.LastWriteTime.Date;
                    TimeSpan comparevalue = nowdate - filedate;

                    if (comparevalue.Days < 3)
                    {

                        foreach (string s in files)
                        {
                            fileName = System.IO.Path.GetFileName(s);
                            destFile = System.IO.Path.Combine(targetdir, fileName);
                            System.IO.File.Copy(s, destFile, true);
                            sw.WriteLine("Log " + s + " has been copied..");
                        }
                    }

                }

Thanks
Steve

Maybe I have read this code wrong, but it looks to me like you are looping and copying all of the files if one of them matches (> 3 days), and that you are doing it each time one of them matches, essentially redundant. Did you mean for both of these to build from the same folder?

// get files from expandedsystemroot
            string[] files = System.IO.Directory.GetFiles(ExpandedSystemRoot, "*.flog");
            DirectoryInfo dirinfo = new DirectoryInfo(ExpandedSystemRoot);
            // get files from expandedsystemroot
            System.IO.FileInfo[] fileinfo = dirinfo.GetFiles("*.flog");

More elaboration...

if (comparevalue.Days < 3)
                {

                    foreach (string s in files)
                    {
                        fileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetdir, fileName);
                        System.IO.File.Copy(s, destFile, true);
                        sw.WriteLine("Log " + s + " has been copied..");
                    }
                }

This inner loop is copying all of the files regardless of whether they met your date check as long as only one them does meet the date check because your inner and outer loops are looping through the same list--right?

private void button14_Click(object sender, EventArgs e)
    {
      FileInfo[] files = new DirectoryInfo(@"C:\").GetFiles(@"*.txt");
      foreach (FileInfo file in files)
      {
        if (DateTime.UtcNow.Subtract(file.LastWriteTimeUtc).TotalDays >= 3)
        {
          Console.WriteLine(file.FullName + " needs to be copied");
        }
      }
    }
Comments
Again, out of this world! Was trying to do this yesterday, but apparently you can only do this once a day.

Hi Akwin Lopez, welcome here.
Please start a new thread to ask your question. Don't use a thread that is already 4 years old.

This question has already been answered. Start a new discussion instead.