none
Copy Files to new folder RRS feed

  • Question

  • How to copy .exe files to new folder, here the example: a folder have 10 files.. 3 of 10 files are .exe files, after click one button, only .exe files will be copied to another folder. Please help me, I'm using formApplication of Visual Basic.

    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string FROM_DIR = "C:/Users/Hamiroel Nazrin/Desktop/Home/A/B/";
            string TO_DIR = "C:/Users/Hamiroel Nazrin/Desktop/Destination/";

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
                FileInfo[] fiDiskfiles = diCopyForm.GetFiles();
                foreach (FileInfo curFile in fiDiskfiles)
                {
                    try
                    {
                        File.Copy(curFile.FullName, TO_DIR + curFile.Name);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

            }
        }
    }

    Wednesday, April 10, 2019 3:38 PM

Answers

  • "I'm using formApplication of Visual Basic."

    This is C#, not VB.

    To copy files use Directory.GetFiles (or EnumerateFiles) to get the files to copy. Then use File.Copy to copy them over. You didn't mention whether you wanted recursive or not so we'll assume no. Something like this.

    class Program
    {
        static void Main ( string[] args )
        {
            CopyExecutables(@"C:\temp\OldFolder", @"C:\Temp\NewFolder");
        }
    
        static void CopyExecutables ( string sourcePath, string targetPath )
        {
            //Make sure target exists
            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);
    
            //Get executables
            var files = Directory.GetFiles(sourcePath, "*.exe");
            foreach (var file in files)
            {
                var newFile = Path.Combine(targetPath, Path.GetFileName(file));
                File.Copy(file, newFile, true);
            };
        }
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 10, 2019 6:20 PM
    Moderator
  • Hi

    Thank you for posting here.

    Based on your description, you want to copy .exe files to new folder.

    You could try the following change.

    Change:

    File.Copy(curFile.FullName, TO_DIR + curFile.Name);

    Into

                    string name = ".exe";
                      if(curFile.Name.EndsWith(name))               
                       {
                            File.Copy(curFile.FullName, Path.Combine(TO_DIR,curFile.Name));
                        }
    

    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 Thursday, April 11, 2019 2:00 PM
    Thursday, April 11, 2019 7:10 AM
    Moderator

All replies

  • "I'm using formApplication of Visual Basic."

    This is C#, not VB.

    To copy files use Directory.GetFiles (or EnumerateFiles) to get the files to copy. Then use File.Copy to copy them over. You didn't mention whether you wanted recursive or not so we'll assume no. Something like this.

    class Program
    {
        static void Main ( string[] args )
        {
            CopyExecutables(@"C:\temp\OldFolder", @"C:\Temp\NewFolder");
        }
    
        static void CopyExecutables ( string sourcePath, string targetPath )
        {
            //Make sure target exists
            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);
    
            //Get executables
            var files = Directory.GetFiles(sourcePath, "*.exe");
            foreach (var file in files)
            {
                var newFile = Path.Combine(targetPath, Path.GetFileName(file));
                File.Copy(file, newFile, true);
            };
        }
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 10, 2019 6:20 PM
    Moderator
  • Hi

    Thank you for posting here.

    Based on your description, you want to copy .exe files to new folder.

    You could try the following change.

    Change:

    File.Copy(curFile.FullName, TO_DIR + curFile.Name);

    Into

                    string name = ".exe";
                      if(curFile.Name.EndsWith(name))               
                       {
                            File.Copy(curFile.FullName, Path.Combine(TO_DIR,curFile.Name));
                        }
    

    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 Thursday, April 11, 2019 2:00 PM
    Thursday, April 11, 2019 7:10 AM
    Moderator
  • Thank you for all your assistance

    Thursday, April 11, 2019 2:01 PM
  • Thank you for all your assistance

    Thursday, April 11, 2019 2:01 PM