• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Search Directories and Sub Directories vb.net

Does anybody know a good way to search a directory and its sub directories for a particular file type and copy them over to another place?  I cannot hardcode the search path so I need something that will search a directory tree.

Thanks in advance.
  • 3
1 Solution
if you are using VS 2005 you can use Directory.GetFiles Method


for each sfilename  as string in system.io.direcory.getfiles("startpath","file pattern", AllDirectories)
ndegioiaAuthor Commented:
Nevermind I figured something out based on a sub routine I found on the web:

 Private Sub RecursiveCopyFiles( _
    ByVal sourceDir As String, _
    ByVal destDir As String, _
    ByVal fRecursive As Boolean, ByVal overWrite As Boolean)

        Dim i As Integer
        Dim posSep As Integer
        Dim sDir As String
        Dim aDirs() As String
        Dim sFile As String
        Dim aFiles() As String

        ' Add trailing separators to the supplied paths if they don't exist.
        If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            sourceDir &= System.IO.Path.DirectorySeparatorChar
        End If

        If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            destDir &= System.IO.Path.DirectorySeparatorChar
        End If

        ' Recursive switch to continue drilling down into dir structure.
        If fRecursive Then

            ' Get a list of directories from the current parent.
            aDirs = System.IO.Directory.GetDirectories(sourceDir)

            For i = 0 To aDirs.GetUpperBound(0)

                ' Get the position of the last separator in the current path.
                posSep = aDirs(i).LastIndexOf("\")

                ' Get the path of the source directory <-- Run this if you want to duplicate the source file structure
                'sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length - (posSep + 1))

                ' Create the new directory in the destination directory.
                System.IO.Directory.CreateDirectory(destDir + sDir)

                ' Since we are in recursive mode, copy the children also
                RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive, overWrite)

        End If

        ' Get the CSV files from the current parent.
        aFiles = System.IO.Directory.GetFiles(sourceDir, "*.csv")

        ' Copy all files.
        For i = 0 To aFiles.GetUpperBound(0)

            ' Get the position of the trailing separator.
            posSep = aFiles(i).LastIndexOf("\")

            ' Get the full path of the source file.
            sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep + 1))

                ' Copy the file.
                System.IO.File.Copy(aFiles(i), destDir + sFile, False)
                'addToConsoleWindow("Copied " & sFile & " to " & destDir)
            Catch ex As Exception
                If overWrite = False Then
                    'addToConsoleWindow("Skipping..." & ex.Message)
                    System.IO.File.Copy(aFiles(i), destDir + sFile, True)
                    'addToConsoleWindow("Overwriting old " & sFile & " in " & destDir)
                End If

            End Try
        Next i
    End Sub
ndegioiaAuthor Commented:
Yeah I am using 2003. I read about that feature.  I found somthing that will work.  I will give you the points because you responded, and I dont want to delete the question because other might find it useful.

Try this:-

            'Stores Directory List
            Dim ListBox1 As New ListBox

            'Stores File List
            Dim ListBox2 As New ListBox

            ' Directory to start search for files
            Dim strDir As String = "C:\"

            ' Store Directories

            ' Get .txt files for main directory
            ListBox2.Items.AddRange(System.IO.Directory.GetFiles(strDir, "*.txt"))

            ' Get .txt Files in each sub-directory of main directory
            For i As Integer = 0 To ListBox1.Items.Count - 1

                ' Select Directory
                ListBox1.SelectedIndex = i

                ' Add files to list
                    ListBox2.Items.AddRange(System.IO.Directory.GetFiles(ListBox1.SelectedItem.ToString(), ".txt"))
                Catch ex As Exception

                End Try


            ' Location to copy files to
            Dim strLoc As String = "C:\Temp\"

            ' Copy files to new location
            ' Get .txt Files in each directory
            For i As Integer = 0 To ListBox2.Items.Count - 1

                ' Select File
                ListBox2.SelectedIndex = i

                ' ' Store file name for copying
                Dim strCopy As String = ListBox2.SelectedItem.ToString().Replace( _
                    IO.Directory.GetDirectoryRoot(ListBox2.SelectedItem.ToString()), "")

                ' Create copy to
                strCopy = strLoc & strCopy

                ' Copy files to new location
                    System.IO.File.Copy(ListBox2.SelectedItem.ToString(), strCopy)
                Catch ex As Exception


                    MsgBox("Failed to copy " & ListBox2.SelectedItem.ToString())

                End Try


        Catch ex As Exception


        End Try
ndegioiaAuthor Commented:
Thanks willbdman but I got it.  Wish I could take back the acceptance and split the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now