Jack cheng

Jack cheng

  • 1.8k
  • 14
  • 236

Copy new added folder in Source folder to Target folder

Apr 15 2019 2:43 AM
Hello everyone,
 
I am using Visual C# and create user interface. There have only one button in interface.
 
There have 2 folders with name 'Source' and 'Target', in 'Source' folder contains '2019', '2018', '2017' folders. Once a day, there is 1 folder's name start with '201904" with 10 file's name start with "text" will be downloaded and move to "C:/Users/Source/2019/".
 
According to codes that I have for now:
 
In day 1, I have been click a button and then a folder "20190401" in "C:/Users/Source/2019/" copied to "C:/Users/Target/".
 
But in day 2, the new downloaded folder "20190402" in '"C:/Users/Source/2019/" wouldn't copy to "C:/Users/Target/" after I click a button for 2nd time, because it cannot overwrite the existed "20190401" folder in "C:/Users/Target/".
 
The result that I expected, "C:/Users/Target/" will collects "201904**"folder in daily by clicking button once a day. Furthermore, only [3] files in folder "201904**" name start with "text" will be copy to "C:/Users/Target/". Do you guys have any idea to solve this problem?
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3. string FROM_DIR = "C:/Users/Source/";  
  4. string TO_DIR = "C:/Users/Target/";  
  5. DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);  
  6. DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();  
  7. string directname = "201904";  
  8. string filename = ".txt";  
  9. foreach (DirectoryInfo newfile in fiDiskfiles)  
  10. {  
  11. try  
  12. {  
  13. if (newfile.Name == "2019")  
  14. {  
  15. foreach (DirectoryInfo direc in newfile.GetDirectories())  
  16. if (direc.Name.StartsWith(directname))  
  17. {  
  18. int count = 0;  
  19.   
  20. foreach (FileInfo file in direc.GetFiles())  
  21. {  
  22. if (file.Name.EndsWith(filename))  
  23. {  
  24. count++;  
  25. }  
  26. }  
  27. if (count == 6)  
  28. {  
  29. DirectoryCopy(direc.FullName,Path.Combine(TO_DIR,direc.Name), true);  
  30. count = 0;  
  31. MessageBox.Show("success");  
  32. }  
  33. }  
  34. }  
  35. }  
  36. catch (Exception ex)  
  37. {  
  38. MessageBox.Show(ex.Message);  
  39. }  
  40. }  
  41. }  
  42.   
  43. private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)  
  44. {  
  45. // Get the subdirectories for the specified directory.  
  46. DirectoryInfo dir = new DirectoryInfo(sourceDirName);  
  47. if (!dir.Exists)  
  48. {  
  49. throw new DirectoryNotFoundException("Source directory does not exist or could not be found: "+ sourceDirName);  
  50. }  
  51. DirectoryInfo[] dirs = dir.GetDirectories();  
  52. // If the destination directory doesn't exist, create it.  
  53. if (!Directory.Exists(destDirName))  
  54. {  
  55. Directory.CreateDirectory(destDirName);  
  56. }  
  57. // Get the files in the directory and copy them to the new location.  
  58. FileInfo[] files = dir.GetFiles();  
  59. foreach (FileInfo file in files)  
  60. {  
  61. string temppath = Path.Combine(destDirName, file.Name);  
  62. file.CopyTo(temppath, false);  
  63. }  
  64. // If copying subdirectories, copy them and their contents to new location.  
  65. if (copySubDirs)  
  66. {  
  67. foreach (DirectoryInfo subdir in dirs)  
  68. {  
  69. string temppath = Path.Combine(destDirName, subdir.Name);  
  70. DirectoryCopy(subdir.FullName, temppath, copySubDirs);  
  71. }  
  72. }  
  73. }
Best regards,
Pugita

Attachment: Code.zip

Answers (1)