Hello all,
How would I go about copying all the files out of a directory and lots of subdirectories and moving only the files somewhere else?(say C:\Test\file1.txt).

I dont pretend to be an expert so a full explanation would be helpful.

Thanks!

Recommended Answers

All 2 Replies

>xcopy c:\temp\test c:\temp /S /-Y

You can copy only files by using /A options with xcopy command but it doesn't traverse through the subdirectories.

But because he posted the question in the VB.NET forum, perhaps something in code would be nice.

.NET has two very useful objects for managing files and directories.
DirectoryInfo and FileInfo.
With those you can do pretty much anything.

In order to locate all the files within a directory, you pass the top most folder of those files to the constructor of a DirectoryInfo object.
Then you can iterate through the Files collection and pass each file to a FileInfo object.
From there you simply copy the file to a new destination.

Private Sub CopyFiles()
        'Pass the top-most folder to the DirectoryInfo constructor
        Dim dd As New System.IO.DirectoryInfo("<original_path>") 'Ex: C:\SomeFolder
        'Iterate through the Files collection and pass each file
        'to a FileInfo object
        For Each ff As FileInfo In dd.GetFiles("*.*", SearchOption.AllDirectories) 'AllDirectories for recursing through sub-folders
            'Copy the file to a new destination
            ff.CopyTo("<newpath>" & ff.Name) 'Ex: C:\NewFolder\ (ending backslash required)
        Next
    End Sub
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.