get file from directories and its sub directories

How can l loop through directories and its subdirectories. Here is my sample so far.

Dim source_dir_path As String = "C:\Users\tmp"
        Dim source_dir As New DirectoryInfo(source_dir_path)
        Dim I As Integer = 0

        For Each Dir As DirectoryInfo In source_dir.GetDirectories

            For Each file_info As FileInfo In Dir.GetFiles

                I += 1

                Dim new_file_name As String = file_info.LastWriteTime.ToString("MMddyyy-hhmmsstt") & "_" & I & file_info.Extension

                Dim copy_to As String = "C:\Users\tmp2\"

                file_info.CopyTo(copy_to & new_file_name)


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need as Recursive method to do it.
Remove the debug sentences an uncomment the f.CopyTo line.

seq = 0
     BackupFiles(New DirectoryInfo("C:\Users\tmp"))

Private seq As Integer = 0
Public Sub BackupFiles(ByVal d As DirectoryInfo)
    For Each f In d.GetFiles
        'f.CopyTo(Path.Combine("C:\Users\tmp2\", f.LastWriteTime.ToString("MMddyyy-hhmmsstt") & "_" & seq & f.Extension))
        Debug.Print(Path.Combine("C:\Users\tmp2\", f.LastWriteTime.ToString("MMddyyy-hhmmsstt") & "_" & seq & f.Extension))
        seq += 1
    For Each d In d.GetDirectories
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.