Copy Files and Maintain Directory Structure
-
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
-
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
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:
-
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:
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