Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:49:59+00:00 2026-06-17T09:49:59+00:00

I posted this before but had to delete the question because I explained it

  • 0

I posted this before but had to delete the question because I explained it so horribly, sorry to those who might have read it before. Let me be much more clear. I have to create a winform application that allows one to copy files from one directory to another by specifying three things:

  1. Where to copy from (txtPath)
  2. What file extensions to copy (the ones you specify are the ones that are copied over) (txtExtensions)
  3. Where to copy to (txtArchiveTo)

Visually it looks like this:

enter image description here

The copy from the txtPath to the txtArchiveTo has to match how it is copied. That is if txtPath looks like the following:

c:\temp
  |
   -drivers.exe (file)
   -log.txt (file)
   -\Test1 (directory)
   -\Test2 (directory)

Then if I select for txtArchiveTo a folder such as c:\backup the result should be after my program runs it should look like this:

c:\backup
  |
   -\temp
      |
       -drivers.exe (file)
       -log.txt (file)
       -\Test1 (directory)
       -\Test2 (directory)

That is whatever is copied needs to follow the same structure…but I am having a hard time coding this because it looks as though I keep referencing root.Parent.Parent, etc. Let me post some code:

The Go button is very basic:

private void btnGo_Click(object sender, EventArgs e)
{
    DirectoryInfo di = new DirectoryInfo(txtPath.Text);
    WalkDirectoryTree(di);
}

This simply creates a directoryinfo object so I have a handle to the txtPath directory and calls a function WalkDirectoryTree. That function looks like this:

 private void WalkDirectoryTree(DirectoryInfo root)
        {
            System.IO.FileInfo[] files = null;
            System.IO.DirectoryInfo[] subDirs = null;

            string[] extArray = txtExtensions.Text.Split(' '); //to hold the txtExtensions as an array string { "txt" "tar" "zip" etc.. }
            string ext; //to hold the extension

            // First, process all the files directly under this folder
            try
            {
                files = root.GetFiles("*.*");
            }
            catch (UnauthorizedAccessException e)
            {
                throw; //todo: log this later on...
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }

            if (files != null)
            {
                foreach (System.IO.FileInfo fi in files)
                {
                    string extension = fi.Extension.ToString();
                    if (extension.IndexOf(".") > -1)
                        ext = extension.Substring(1, extension.Length-1);
                    else
                        ext = extension;

                    if (extArray.Any(s => ext.Contains(s)))
                    {    
                        //copy the file
                        if (Directory.Exists(txtArchiveTo.Text + "\\" + fi.Directory.Name))
                            {
                                //directory exists copy the files
                            }
                            else
                            {
                                Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);
                            }

                            File.Copy(fi.FullName, txtArchiveTo.Text + "\\" + fi.Directory.Name + "\\" + fi.Name);                    
                        
                        //create a shortcut pointing back to the file...
                        using (ShellLink shortcut = new ShellLink())
                        {
                            shortcut.Target = fi.FullName;
                            //shortcut.Description = "MY SHORTCUT";
                            shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
                            shortcut.ShortCutFile = fi.DirectoryName + "\\" + fi.Name + ".lnk";
                            shortcut.Save();
                        }
                    }

                }

                // Now find all the subdirectories under this directory.
                subDirs = root.GetDirectories();

                foreach (System.IO.DirectoryInfo dirInfo in subDirs)
                {
                    // Resursive call for each subdirectory.
                    WalkDirectoryTree(dirInfo);
                }
            }            
        }

Please do not pay much attention to the code where I create a shortcut, etc. That part is not the problem. What I am having difficulty with is when I hit these lines of code:

 if (extArray.Any(s => ext.Contains(s)))
                        {    
                            //copy the file
                            if (Directory.Exists(txtArchiveTo.Text + "\\" + fi.Directory.Name))
                                {
                                    //directory exists copy the files
                                }
                                else
                                {
                                    Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);
                                }

The if simply checks to see if the extension of the file matches an extension the user has typed inside of txtExtensions this ensures we are only copying files with those extensions BUT within the folders they are found. My issue is right after that if…I cannot just say:

Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);

The reason being is the archive folder must match the same folder path as what is being copied. So for instance if someone selected c:\temp to search and find txt files to copy from and they selected a folder c:\backup to copy to and c:\temp had 3 subfolders (One, Two, and Three) that also contained text files. The result would be that after my program runs (the completion of "Go") the result would be that the archive folder c:\backup would contain folders (One, Two, and Three) with the txt files inside of them, in addition, c:\backup\One\mytest.txt, etc.

I want to incorporate this in my current code, and feel I am really close but think I need to come up with some recurrision to get the right directory structure. Please do not post me to links about MSDN on how to use Directory.CreateDirectory, or the FileInfo class, I have read these and understand them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:50:00+00:00Added an answer on June 17, 2026 at 9:50 am

    Pass the destination folder in as a paramter to your walking function:

        private void WalkDirectoryTree(DirectoryInfo root, string DestinationFolder)
    

    Then when you copy the file, you can just use the CopyTo method of the fileInfo:

         fi.CopyTo(DestinationFolder)
    

    When calling the next method recursively, you can just add to the desintation path:

        foreach (System.IO.DirectoryInfo dirInfo in subDirs)
        {
            // Resursive call for each subdirectory.
            WalkDirectoryTree(dirInfo, System.IO.Path.Combine(DestinationFolder, dirInfo.Name));
        }
    

    When making the initial call, you will need to compute the output directory:

    string path = txtPath.Text;
    string outputDir = txtArchiveTo.Text
    string finalDir = System.IO.Path.Combine(outputDir, path.Remove(0, System.IO.Path.GetPathRoot(path).Length));
    
    DirectoryInfo di = new DirectoryInfo(txtPath.Text);
    WalkDirectoryTree(di, finalDir);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had posted before a question related to this, but the solutions didn´t solve
I have posted on this topic before, but as yet I have not had
I know this question has been posted before... but I haven't found any answer
I posted this question on the django-users list, but haven't had a reply there
I have never posted on this forum before but use it a lot for
sorry I posted this in the google group before I realized you had moved
I've posted this before, but I worded it poorly. I'm trying again with a
I've posted some questions on this before, but it's different. So consider a small
Note this question was originally posted in 2009, before C++11 was ratified and before
I had posted the code before but had asked it differently as I didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.