0

Greetings,

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

2
Contributors
7
Replies
21
Views
4 Years
Discussion Span
Last Post by tinstaafl
0
         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

Edited by AmrMohammed

0

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.

0

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

0

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);
    }
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.