0

hi,

Please can any one help me out with providing me the code to create a windows application in which if a select a folder and a type of file for example if i select .txt file it should copy all the .txt files present in that folder to the destination that i specify and also if any .txt files that r present in its subfolders it should copy the subfolder and the .txt files.

thanks in advance
jim

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by thecoolguy
0

It can be done like this:

void CopyAll(DirectoryInfo currentDir, DirectoryInfo newDir, string fileType)
        {
            foreach (FileInfo fi in currentDir.GetFiles(fileType))
            {
                fi.CopyTo(Path.Combine(newDir.ToString(), fi.Name), true);
            }

            foreach (DirectoryInfo subDir in currentDir.GetDirectories())
            {
                DirectoryInfo targetDir = newDir.CreateSubdirectory(subDir.Name);
                CopyAll(subDir, targetDir, "*.txt");
            }
        }

Example Use:

CopyAll(new DirectoryInfo(@"C:\txts"), new DirectoryInfo(@"D:\newTxts"), "*.txt");

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.