複製過濾後檔案到目的資料夾並將相同檔案備份

複製過濾後檔案到目的資料夾並將相同檔案備份

參考了下列資料後

[C#]遞迴找出所有目錄

Visual C# - 讀者詢問如何複製目錄以及目錄下所有的子目錄與檔案

改寫程式如下:


using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Data;
using Microsoft.VisualBasic.Devices;

namespace CopyDirFile
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer MyCom = new Computer();
            //來源路徑
            string SourceDir = ConfigurationManager.AppSettings["Source"].Replace("\\", "\\\\").ToString();
            //目的路徑
            string TargetDir = ConfigurationManager.AppSettings["Target"].Replace("\\", "\\\\").ToString();
            //備份路徑
            string BackupDir = (ConfigurationManager.AppSettings["Backup"].ToString() == string.Empty)?
              "" : ConfigurationManager.AppSettings["Backup"].Replace("\\", "\\\\").ToString();
            DirectoryInfo di = new DirectoryInfo(TargetDir);
            DirectoryInfo dirSource = new DirectoryInfo(SourceDir);
            DirectoryInfo diBackup = new DirectoryInfo(BackupDir);           
            //目的路徑沒有就建資料夾
            if (!di.Exists)
                MyCom.FileSystem.CreateDirectory(TargetDir);
            if (!diBackup.Exists)
                MyCom.FileSystem.CreateDirectory(BackupDir);
            //複製符合條件的檔案
            CopyDir(SourceDir, TargetDir,BackupDir);                        
        }
        /// <summary>
        /// 遞迴複製符合條件的檔案.
        /// </summary>
        /// <param name="srcPath">The SRC path.</param>
        /// <param name="aimPath">The aim path.</param>
        public static void CopyDir(string srcPath, string aimPath,string BackupPath)
        {
            if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
            {
                aimPath += Path.DirectorySeparatorChar;
            }

            if (!Directory.Exists(aimPath))
            {
                Directory.CreateDirectory(aimPath);
            }
            string[] fileListAll = Directory.GetFileSystemEntries(srcPath);
            string[] fileList = FileFilter(fileListAll);
                     
            foreach (string file in fileList)
            {
                FileInfo fi = new FileInfo(aimPath + file.Replace(srcPath, string.Empty));
                if (fi.Exists)
                {
                    //備份檔案到備份資料夾
                    if (BackupPath != string.Empty)
                    {
                        FileInfo fiBack = new FileInfo(BackupPath + file.Replace(srcPath, string.Empty));
                        if (fiBack.Exists)
                            File.Delete(BackupPath + file.Replace(srcPath, string.Empty));
                        File.Move(aimPath + file.Replace(srcPath, string.Empty), BackupPath + file.Replace(srcPath, string.Empty));
                    }
                }
                if (Directory.Exists(file))
                {
                    CopyDir(file, aimPath + Path.GetFileName(file),BackupPath);
                }
                else
                {
                    
                    if(file.LastIndexOf(".")!=-1)
                        File.Copy(file, aimPath + file.Replace(srcPath,string.Empty), true);
                }
            }
        }
        /// <summary>
        /// 過濾條件:當檔案最後修改日期減去本日小於日期區間(DayInterval)時
        /// 將檔案複製到目的地.
        /// 當DayInterval = -1 時表示不複製檔案,當DayInterval為空值時表示全複製檔案
        /// </summary>
        /// <param name="Input">The input.</param>
        /// <returns></returns>
        public static string[] FileFilter(params string[] Input)
        {
            List<string> result = new List<string>();
            foreach (string temp in Input)
            {
                FileInfo fi = new FileInfo(temp.Replace("\\","\\\\"));
                TimeSpan t1 = DateTime.Now - fi.LastWriteTime;
                if (ConfigurationManager.AppSettings["DayInterval"].ToString() == string.Empty)
                    result.Add(temp);
                else
                {
                    if (t1.Days <= int.Parse(ConfigurationManager.AppSettings["DayInterval"].ToString()))
                        result.Add(temp);
                }
            }
            return result.ToArray();
        }
    }
}

App.config內容


<configuration>
  <appSettings>
    <add key="Source" value="C:\TestSource"/>
    <add key="Target" value="C:\TestTarget"/>
	<add key="Backup" value="C:\Backup"/>
    <add key="DayInterval" value="0"/>
  </appSettings>
</configuration>

輸出結果

檔案:test.txt

原本內容為:1234

修改內容為:12345

pic1

pic2

pic3