Copy older folders based on Date Created to new location using Web Services in ASP.Net

donny
 
on Sep 02, 2015 08:54 AM
2739 Views

HI 

I need to create a web service that copies all files in a folder to another folder based on the previous day 

ie need to copy only the folders that has been created or uploaded for the previous day 

 

Please can you help 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shashikant
 
on Sep 02, 2015 08:55 AM

Hi,

You can use below code.

Code

public void CopyPreviousDayFile()
{
    string destPath = "D:\\Users\\" + DateTime.Now.AddDays(-1).ToShortDateString().Replace("/", "-");
    string sourcePath = "D:\\Users\\Shashikant\\test";
    DirectoryInfo info = new DirectoryInfo(sourcePath);

    var files = info.GetFiles();

    foreach (FileInfo file in files)
    {
        if (file.CreationTime.ToShortDateString().Equals(DateTime.Now.AddDays(-1).ToShortDateString()))
        {
            using (FileStream stream = file.OpenRead())
            {
                bool exists = System.IO.Directory.Exists(destPath);
                if (!exists)
                {
                    System.IO.Directory.CreateDirectory(destPath);
                }
                bool existFile = System.IO.File.Exists(Path.Combine(destPath, file.Name));
                if (!existFile)
                {
                    string temppath = Path.Combine(destPath, file.Name);
                    File.Create(temppath);
                    file.CopyTo(temppath);
                }
            }
        }
    }
}