将仅包含3个文件的特定文件夹复制到新文件夹

时间:2019-04-13 05:25:12

标签: c# user-interface visual-studio-2012

一个关于编码的问题(Visual Studio C#WindowsformApplication)有两个文件夹:(源和目标),我建立1个按钮"Copy"。 在"Source"文件夹中有随机文件夹,例如"20190401", "20190402", "20190403", "20180401", "20170401" and "20160401"。每个这些文件夹都有[10] .txt文件。如果我只想将其中所有带有[3] .txt文件的所有"201904**"文件夹复制到"Target"文件夹,该怎么编码?这是我的代码。

代码

  ** private void button1_Click

    {
      string FROM_DIR = "C:/Users/5004117928/Desktop/Source";
      string TO_DIR = "C:/Users/5004117928/Desktop/Target/";         
      DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
      DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();
      string directname = "201904";
      string filename = ".txt";

        foreach (DirectoryInfo newfile in fiDiskfiles)
        {
            try
            {
                if (newfile.Name == "2019")
                {
                    foreach (DirectoryInfo direc in newfile.GetDirectories())
                        if (direc.Name.StartsWith(directname))
                        {
                            int count = 0;
                            foreach (FileInfo file in direc.GetFiles())
                            {
                                if (file.Name.EndsWith(filename))
                                {
                                    count++;
                                }
                            }
                            if (count == 3)
                            {
                                DirectoryCopy(direc.FullName,Path.Combine(TO_DIR,direc.Name), true);
                                count = 0;
                            }
                        }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        MessageBox.Show("success");
    }
    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }
        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }
        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }
        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }**

我希望在单击按钮后, 输出将自动复制所有文件夹。名称从"201904**"文件夹中的[3]个文本文件,以"Source"开头到"target"文件夹。

1 个答案:

答案 0 :(得分:0)

我认为您可以使用linq直接使用名称搜索目录,并可以如下复制其中的子文件夹/文件。它将为您提供过滤文件夹/文件/跳过/获取n个文件夹/文件的灵活性

string FROM_DIR = "C:/Users/5004117928/Desktop/Source";
string TO_DIR = "C:/Users/5004117928/Desktop/Target/"; 

string searchText = "201904";
string extension = "txt";

IEnumerable<string> dirs =  Directory.EnumerateDirectories(FROM_DIR, "*", SearchOption.AllDirectories)
          .Where(dirPath=>Path.GetFileName(dirPath.TrimEnd(Path.DirectorySeparatorChar)).StartsWith(searchText));

foreach (string dir in dirs)
{
      string destDirPath = dir.Replace(FROM_DIR, TO_DIR);

      if (!Directory.Exists(destDirPath))
           Directory.CreateDirectory(destDirPath);

       //Copy all the files & Replaces any files with the same name
       foreach (string newPath in Directory.EnumerateFiles(dir, string.format("*.{0}",extension), 
           SearchOption.AllDirectories))// Here you can skip/take n files if u need
              File.Copy(newPath, newPath.Replace(FROM_DIR, TO_DIR), true);
}

还对子文件夹和源文件夹中的文件进行了测试。希望对您有所帮助。

相关问题