none
Copy Files from SPECIFIC folder to another folder RRS feed

  • Question

  • 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

    Friday, April 12, 2019 7:14 AM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to copy files from specific folder to another folder.

    You could try the following code.

    private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
                DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();
                string filename = ".txt";
                int count = 0;
                foreach (DirectoryInfo newfile in fiDiskfiles)
                {
                    try
                   {
                        if (newfile.Name=="2019")
                        {
                            foreach (FileInfo file in newfile.GetFiles())
                            {
                                if(file.FullName.EndsWith(filename))
                                {
                                    File.Copy(file.FullName, Path.Combine(TO_DIR,file.Name));
                                    count++;
                                    if (count == 3)
                                    {
                                        break;
                                    }
                                }
    
                            }
                           
                           
                        }
                       
                    }
                   
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                MessageBox.Show("success");
            }
    
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Friday, April 12, 2019 8:59 AM
    Friday, April 12, 2019 8:12 AM
    Moderator
  • Hi

    Thanks for the feedback.

    One thread can only reply to one question so that it will help other members to find the solution quickly if they face a similar issue. You could mark it as an answer and create a new thread in c# forum. We will solve your new problem in another thread.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 12, 2019 8:51 AM
    Moderator
  • Hi

    Thank you for posting here.

    For your question, you want to copy specific files in specific folder to another folder.

    You could try the following code.

     private void button1_Click(object sender, EventArgs e)
            {
                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);
                    }
                }
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Friday, April 12, 2019 10:30 AM
    • Marked as answer by Pugita Saturday, April 13, 2019 9:13 AM
    Friday, April 12, 2019 10:00 AM
    Moderator
  • Hi Pugita,

    Thanks for the feedback.

    You could create a new thread in c# forum. We will solve your new problem in another thread.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Monday, April 15, 2019 4:06 AM
    Monday, April 15, 2019 3:13 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    For your question, you want to copy files from specific folder to another folder.

    You could try the following code.

    private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
                DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();
                string filename = ".txt";
                int count = 0;
                foreach (DirectoryInfo newfile in fiDiskfiles)
                {
                    try
                   {
                        if (newfile.Name=="2019")
                        {
                            foreach (FileInfo file in newfile.GetFiles())
                            {
                                if(file.FullName.EndsWith(filename))
                                {
                                    File.Copy(file.FullName, Path.Combine(TO_DIR,file.Name));
                                    count++;
                                    if (count == 3)
                                    {
                                        break;
                                    }
                                }
    
                            }
                           
                           
                        }
                       
                    }
                   
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                MessageBox.Show("success");
            }
    
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Friday, April 12, 2019 8:59 AM
    Friday, April 12, 2019 8:12 AM
    Moderator
  • Thank you so much Mr. Jack and I really appreciate that you are so awesome.

    One more question, every folder which are (20190401, 20190402, and 20190403) have many text files inside, what is the coding if I only want to copy "201904**" folders with 3 text files inside it to Target folder? Which mean I only copy all folders StartWith "201904**" with 3 text files from "Source" folder to "target" folder :)

    Best Regard,

    Pugita

    Friday, April 12, 2019 8:38 AM
  • Hi

    Thanks for the feedback.

    One thread can only reply to one question so that it will help other members to find the solution quickly if they face a similar issue. You could mark it as an answer and create a new thread in c# forum. We will solve your new problem in another thread.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 12, 2019 8:51 AM
    Moderator
  • Alright Mr. Jack, I will create new thread and mark it as an answer. Thank you so much.

    Best Regard,

    Pugita

    Friday, April 12, 2019 8:58 AM
  • Hello Everyone,

    One Question about coding (Visual Studio C# WindowsformApplication)

    In "Source" folder have random folders such as "20190401", "20190402", "20190403", "20180401", "20170401" and "20160401". Every of these folders have [10] text files.

    What is the coding if I only want to copy all "201904**" folders with [3] text files inside it to "Target" folder? Which mean I only copy all folders StartWith "201904**" with [3] text files from "Source" folder to "target" folder after I click a button. Here is my coding for now.

    Code:

    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 Regard,

    Pugita


    Friday, April 12, 2019 9:13 AM
  • Hi

    Thank you for posting here.

    For your question, you want to copy specific files in specific folder to another folder.

    You could try the following code.

     private void button1_Click(object sender, EventArgs e)
            {
                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);
                    }
                }
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Friday, April 12, 2019 10:30 AM
    • Marked as answer by Pugita Saturday, April 13, 2019 9:13 AM
    Friday, April 12, 2019 10:00 AM
    Moderator
  • Thank you so much Mr. Jack, you are helpful

    Best regards,

    Pugita

    Friday, April 12, 2019 10:32 AM
  • Hello Mr Jack, How are you? I hope you are doing well.

    And again thank you so much for your concern sir. I got new question sir, if I want to put a new folder "201904++" daily, and this new folder will copy to "target" folder by clicking a button once on every day. What is the coding sir? I have tried my best to make it but still failed until now. Can you help me sir? 

    Best regards,

    Pugita

             

    Saturday, April 13, 2019 4:32 PM
  • Hi Pugita,

    Thanks for the feedback.

    You could create a new thread in c# forum. We will solve your new problem in another thread.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Monday, April 15, 2019 4:06 AM
    Monday, April 15, 2019 3:13 AM
    Moderator
  • Hi Jack, 

    Thanks for your concern, I have been create new thread 2 hours ago and the title is "How to copy new added folder in source folder to target folder by clicking a button daily?". You can check that thread, Thank you Mr. Jack.

    Best Regard,

    Pugita


    • Edited by Pugita Monday, April 15, 2019 3:20 AM
    Monday, April 15, 2019 3:18 AM