Home > OS >  Copying files from a specific date over to another directory
Copying files from a specific date over to another directory

Time:10-18

I'm new to C# and I am having trouble copying all files from say 15 days ago from one directory to another. This is what I have.

This is just the copy class.

using System;

namespace DeleteOldLogFiles
{

    public class Copier
    {
        public Copier() 
        {
            string sourcePath = @"M:\";
            string targetPath = @"L:\"; 
            string fileName = string.Empty;
            string destFile = string.Empty;
            DateTime fileDate = DateTime.Today.AddDays(-15);


            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                foreach (string s in files)
                {

                    fileName = System.IO.Path.GetFileName(s);
                    fileDate = DateTime.Today.AddDays(-15);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Error, path does not exist.");
            }
        }
    }
}

CodePudding user response:

There is no way to search for files by date with the standard System.IO. But once you have a list of potential candidates, there are File.GetLastWriteTime() File.GetLastAccessTime() File.GetCreationTime(). Pick whichever timestamp fits your needs, compare it to your date-limit variable and do/don't do the copy based on the result of that comparison.

It's not entirely clear, what you mean by

copying all files from say 15 days ago

For instance if you want to copy all files, created within the last 15 days you can do as follows:

var limit = DateTime.Today.AddDays(-15);
foreach (string s in files)
{
  var creationTime = System.IO.File.GetCreationTime(s);
  if (creationTime > limit) {  //the file was created within the last 15 days
    string fileName = System.IO.Path.GetFileName(s);
    string destFile = System.IO.Path.Combine(targetPath, fileName);
    System.IO.File.Copy(s, destFile, true);
  }
}

Adapt the comparison accordingly if you mean something else.

Furthermore, there is no need to define the fileName or destFile variable outside the loop's body.

And also, you might want to reconsider doing this in a class constructor. It seems the only purpose of this class is to copy the files and there isn't really an instance of that class needed, once the copying is done. Maybe a static method would be better ...

CodePudding user response:

As I can see the functionality you are looking for can be done using the FileInfo Class, using that class you could do something like this:

add:

using System.IO;

and change your code to something like this:

     var fi1 = new FileInfo(s);    
     if (fi1.CreationTime < fileDate)
      {
        System.IO.File.Copy(s, destFile, true);
      }

CodePudding user response:

You can use the following code:

    string[] getFiles = Directory.GetFiles("your path");
    DateTime time = DateTime.Today.AddDays(-15);
    foreach (var file in getFiles)
    {
        FileInfo fileInfo = new FileInfo(file);
        if(fileInfo.CreationTime.Day == time.Day)
        {
            //copy file
        }
    }
  • Related