How to copy new added folder source folder to target folder by clicking a button daily?

  • Thread starter Thread starter Pugita
  • Start date Start date
P

Pugita

Guest
Hello everyone,

In day 1 I have click a button then the folder "20190401*" copied to 'Target' folder. But in day 2, the new downloaded folder "20190402" in 'Source' folder wouldn't copy to 'target' folder after I click a button again because it cannot overwrite the existed "20190401" folder in 'Target folder'. The result that I expected, 'Target' folder will collect "201904**" daily by clicking button once a day. Furthermore, only [3] files in folder "201904**" name start with "text" + "*.*" will be copy to 'Target' folder. Do you guys have any idea to solve this problem? thank you.


private void button1_Click(object sender, EventArgs e)
{
string FROM_DIR = "C:/Users/Desktop/Source/";
string TO_DIR = "C:/Users/Desktop/Target/";
DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();
string directname = "201904";
string filename = ".txt";

foreach (DirectoryInfo newfile in fiDiskfiles)
{
try
{
//Succeed
if (newfile.Name == "2019")
{
foreach (DirectoryInfo direc in newfile.GetDirectories())
//Succeed
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;
MessageBox.Show("success");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
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);
}
}
}
}
}


Best regards,

Pugita

Continue reading...
 
Back
Top