Copy Files from SPECIFIC folder to another folder

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

Pugita

Guest
Hello EVERYONE,

I got real problem and until now I couldn't solve it. I'm using Visual Studio C# Windows form application.

I just want to copy "2019" folders in " Source" folder with given name 20190401, 20190402, and 20190403. In "Source" folder have random folders such as "2018", "2017" and more. The result that I want is after I click a button then it automatically only "2019" folders in "source" folder will copy to "target" folder, and only 3 text files in 2019 folder will be copy then. Here is my coding for now:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string FROM_DIR = "C:/Users/5004117928/Desktop/Source";
string TO_DIR = "C:/Users/5004117928/Desktop/Target/";
string filename = "t";

public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
FileInfo[] fiDiskfiles = diCopyForm.GetFiles();

foreach (FileInfo newfile in fiDiskfiles.Take(3))
{
try
{
if (newfile.Name.StartsWith(filename))
{
File.Copy(newfile.FullName, TO_DIR + newfile.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}


Best regards,

Pugita

Continue reading...
 
Back
Top