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

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

thanks a lot farooqaaa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.