You must Sign In to post a response.
  • Category: Visual Studio

    How to copy files only less than 1month in vb.net

    I am trying to copy files from one folder to another folder, here i has to get only one month files for example the files should copy only from 27/8/2016 if i run now i coded for this but its not working its copying January files also can anyone suggest what need to change.
  • #767965
    Hi,
    This can be done in many ways using Vb.NET
    First we need to get the File Date and time then we can proceed with the file move.


    string[] files = Directory.GetFiles(dirName);

    foreach (string file in files)
    {
    FileInfo fi = new FileInfo(file);
    if (fi.LastAccessTime < DateTime.Now.AddMonths(-1)) // For a Month files
    fi.Copy();
    //Destination Path with detailed function should come here.
    }


    We can also perform this using directory function.


    Directory.GetFiles(dirName)
    .Select(f => new FileInfo(f))
    .Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-1))
    .ToList()
    .ForEach(f => f.Copy());
    //Destination Path with detailed function should come here.

    Thanks,
    Mani


Sign In to post your comments