0

How to modify the code to copy and files in subdirectoryes of tempDownloadFolder?

private void moveFiles()
{
   DirectoryInfo di = new DirectoryInfo(tempDownloadFolder);
   FileInfo[] files = di.GetFiles();

   foreach (FileInfo fi in files)
   {
       if (fi.Name != downloadFile)
        File.Copy(tempDownloadFolder + fi.Name, destinationFolder + fi.Name, true);
   }

}
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
Kras Kj
  • 1
  • 1
  • http://stackoverflow.com/questions/7146021/copy-all-files-in-directory http://stackoverflow.com/questions/2742300/what-is-the-best-way-to-copy-a-folder-and-all-subfolders-and-files-using-c-sharp – rebeliagamer May 23 '13 at 11:51
  • from MSDN "How to: Copy Directories" http://msdn.microsoft.com/en-us/library/bb762914.aspx – Mzf May 23 '13 at 11:53
  • Why is the method now called `moveFiles`? It doesn't move files! – JeffRSon May 23 '13 at 11:57
  • move becouse overwrite old files, then another method delete tempDownloadFolder – Kras Kj May 23 '13 at 12:34
  • Still it doesn't move files if the old files are deleted from another method. `moveFiles` could be a method that calls `copyFiles` *and* `deleteFiles`. – JeffRSon May 23 '13 at 12:40

4 Answers4

2

You need to do a recursive search.

very rough example:

    private void copyFiles(string filePath)
    {
        DirectoryInfo di = new DirectoryInfo(filePath);
        FileInfo[] files = di.GetFiles();

        foreach (FileInfo fi in files)
        {
            // test if fi is a directory
            // if so call copyFiles(fi.FullName) again
            // else execute the following
            if (fi.Name != downloadFile) File.Copy(filePath+ fi.Name, destinationFolder + fi.Name, true);
        }

    }
DaveDev
  • 41,155
  • 72
  • 223
  • 385
1

If you want files of all subdirectories use the SearchOption parameter:

DirectoryInfo di = new DirectoryInfo(tempDownloadFolder);
di.GetFiles("*.*", SearchOption.AllDirectories);

FileInfo[] files = di.GetFiles();

foreach (FileInfo fi in files)
{
   if (fi.Name != downloadFile)
   File.Copy(tempDownloadFolder + fi.Name, destinationFolder + fi.Name, true);
}
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
0

Replace the File.Copy line by

File.Copy(fi.FullName, Path.Combine(destinationFolder, fi.Name), true);
JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • I have tryed all solutions, but still can't copy subfolders – Kras Kj May 23 '13 at 12:32
  • The question is what exactly you want to do: Copy files from a certain subdirectory to another (existing) subdirectory? This can be done by this snippet. Or do you want to copy all files recursively? Then, of course, you would have to use di.GetDirectories() to query for subfolders, create this as a subdirectory of the destination folder and only then start copying files. Copying files can be done by recursively calling the copy method. – JeffRSon May 23 '13 at 12:37
  • I want to copy all content of current directory +subdirectories. Lilke xcopy/e command – Kras Kj May 23 '13 at 13:07
  • Well, as told before, you have to create all subdirectories (by `Directory.CreateDirectory`) before you want to copy there. – JeffRSon May 23 '13 at 13:11
  • Tryed to create subdirs manualy,but they still are empty. Method copies files only in root directory. – Kras Kj May 23 '13 at 13:20
  • You need to search files recursively and build the correct destination path. Let the debugger show you paths if in doubt. – JeffRSon May 23 '13 at 13:36