Greetings,

I want to get all the last written files in a specified directory based on date and copy them to another directory.

DirectoryInfo Class

  • Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
         string sourcePath;
         string targetPath = @"D:\\MyDirectory";

         SqlConnection sqlConn = new SqlConnection();
         sqlConn.ConnectionString = connectionString;

         string cmdString = @"SELECT DatabasePath FROM dbo.Backup_Path";

         SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, sqlConn);

         dataAdapter.Fill(dtTable);

         dataAdapter.Dispose();

         sqlConn.Close();
         sqlConn.Dispose();

         foreach (DataRow dtRow in dtTable.Rows)
         {
            sourcePath = dtRow[0].ToString();

            foreach (FileInfo file in GetLatestFiles(sourcePath))
            {
               MessageBox.Show("Copying file " + file.Name);
               string sourceFile = System.IO.Path.Combine(sourcePath, file.Name);
               string destFile = System.IO.Path.Combine(targetPath, file.Name);
               System.IO.File.Copy(sourceFile, destFile, true);
            }
         } // end foreach

Sorry my ESP is on the blink right now and I'm unable to read your mind. I'm afraid you'll have to tell us what's on your mind.

We have some servers in England and in many other countries, everyday we take a database backup for each database we have and put it in a specific folder. Finally, all those backups are collected here in one of our servers. So, what I'm trying to do is a small application running as a script, all its job is to connect to our branches' servers and copy the lastest backup files in those specific backup folders to a folder in the server here. I hope I explained my idea well

Here's a simple method that will take the path to the source folder and the path to the destination folder and the target date, and will copy every file in the source folder that was created on or after the target date and copy it to the destination folder.

    public void CopyBackups(string SourceFolder, string DestinationFolder, DateTime AfterDate)
    {
        List<FileInfo> FileList = new DirectoryInfo(SourceFolder).GetFiles().ToList();
        'Change which files are selected here.
        FileList = FileList.Where(x => x.CreationTime >= AfterDate).ToList();
        foreach (FileInfo file in FileList)
            file.CopyTo(DestinationFolder + @"\" + file.Name);
    }

Greetings,

Thanks "tinstaafl" for your help. I just modified the code a little bit since I need the lastest written files in the source folder.

Wishing you a great & happy day :)

Regards,

Amr Mohammed

You're very welcome. Please remember to mark this solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.