Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Copy Files and Maintain Directory Structure

Copy Files and Maintain Directory Structure

Scheduled Pinned Locked Moved C#
tutorialquestionlounge
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Fritzius
    wrote on last edited by
    #1

    Hi All, Sorry about the rapid-fire questions, but I'm making a ton of progress with my project. Thank you to everyone who's helped me so far, and thanks in general to CodeProject--this site is heavily visited when I need something done. For my next question, I need to know how to copy a file from one location to another while still keeping the same directory structure. An example would be: c:\foo\bar\123.txt ---copy---> c:\copy\foo\bar\123.txt I get an exception saying part of the path couldn't be found. I can get to the original file just fine, so it exists where strOrigFile says it does. The destination path looks all right--the only difference being the highest parent directory being "Symbiosys_Backup" instead of "Files". But I don't know what to do about this exception. Do I have to recursively create directories as I go, or is there a ninja-sneaky way of pulling this off without pulling my hair out? Code that I have is below.

    private void button4_Click(object sender, EventArgs e)
    {
    // Get the strings from the textboxes
    string strOrigFile = "Files\\testfile.txt";
    string strDestFile = "C:\\Symbiosys_Backup\\" + (System.IO.Path.GetFullPath(strOrigFile)).Replace(@"C:\", @"");

            // Get the filename only
            string strFileName = System.IO.Path.GetFileName(strOrigFile);
    
            // Determine the full destination path including filename
            //string strDestFile = System.IO.Path.Combine(strDestLoc, strFileName);
    
            // Copy the file
            MessageBox.Show("Original File: " + strOrigFile + "\\r\\nDestination File: " + strDestFile);
            System.IO.File.Copy(strOrigFile, strDestFile, true);
        }
    

    Thanka you, Michael Fritzius

    L 1 Reply Last reply
    0
    • M Michael Fritzius

      Hi All, Sorry about the rapid-fire questions, but I'm making a ton of progress with my project. Thank you to everyone who's helped me so far, and thanks in general to CodeProject--this site is heavily visited when I need something done. For my next question, I need to know how to copy a file from one location to another while still keeping the same directory structure. An example would be: c:\foo\bar\123.txt ---copy---> c:\copy\foo\bar\123.txt I get an exception saying part of the path couldn't be found. I can get to the original file just fine, so it exists where strOrigFile says it does. The destination path looks all right--the only difference being the highest parent directory being "Symbiosys_Backup" instead of "Files". But I don't know what to do about this exception. Do I have to recursively create directories as I go, or is there a ninja-sneaky way of pulling this off without pulling my hair out? Code that I have is below.

      private void button4_Click(object sender, EventArgs e)
      {
      // Get the strings from the textboxes
      string strOrigFile = "Files\\testfile.txt";
      string strDestFile = "C:\\Symbiosys_Backup\\" + (System.IO.Path.GetFullPath(strOrigFile)).Replace(@"C:\", @"");

              // Get the filename only
              string strFileName = System.IO.Path.GetFileName(strOrigFile);
      
              // Determine the full destination path including filename
              //string strDestFile = System.IO.Path.Combine(strDestLoc, strFileName);
      
              // Copy the file
              MessageBox.Show("Original File: " + strOrigFile + "\\r\\nDestination File: " + strDestFile);
              System.IO.File.Copy(strOrigFile, strDestFile, true);
          }
      

      Thanka you, Michael Fritzius

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, directories don't get created automatically, so if (part of) the destination path does not exist, the copy will fail. You can use Directory.CreateDirectory() to first create the directory (even if it already exists), or you can first test for its absence, then create it, then copy the file(s). You might also be interested in the Path class. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, directories don't get created automatically, so if (part of) the destination path does not exist, the copy will fail. You can use Directory.CreateDirectory() to first create the directory (even if it already exists), or you can first test for its absence, then create it, then copy the file(s). You might also be interested in the Path class. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        M Offline
        M Offline
        Michael Fritzius
        wrote on last edited by
        #3

        Got it. Thanks Luc. Very helpful. I ended up having to get only the directory from the big long string describing the destination file. Checking to see if that Exists() and then creating it if not was what the solution needed. I now have a directory with a subdirectory containing a carefully uprooted file structure. Michael Fritzius

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups