title: Copy specific Folder with 3 Files only to New Folder
body: <p>One Question about coding (Visual Studio C# WindowsformApplication) There have Two folder: (Source and Target) and I build 1 button <code>"Copy"</code>. 
  In <code>"Source"</code> folder have random folders such as <code>"20190401", "20190402", "20190403", "20180401", "20170401" and "20160401"</code>. Every these folders have [10] .txt files. What is the coding if I only want to copy all <code>"201904**"</code> folders with [3] .txt files inside it to <code>"Target"</code> folder? Here is my code for now.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>  ** 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);
              }
          }
      }**
  </code></pre>
  
  <p>I expect after I click a button, 
  output is automatically copy all folders Name Start With <code>"201904**"</code> with [3] text files inside from <code>"Source"</code> folder to <code>"target"</code> folder.</p>