• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • 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.
0
ndegioia
Asked:
ndegioia
  • 3
1 Solution
 
appariCommented:
if you are using VS 2005 you can use Directory.GetFiles Method

http://msdn2.microsoft.com/en-US/library/ms143316.aspx

sample
for each sfilename  as string in system.io.direcory.getfiles("startpath","file pattern", AllDirectories)
        console.writeline(sfilename)
next
0
 
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)
            Next

        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))
            Try

                ' 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
                    'errorBoxShow(ex.Message)
                    'addToConsoleWindow("Skipping..." & ex.Message)
                Else
                    System.IO.File.Copy(aFiles(i), destDir + sFile, True)
                    'addToConsoleWindow("Overwriting old " & sFile & " in " & destDir)
                End If

            End Try
        Next i
    End Sub
0
 
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.

Thanks
0
 
willbdmanCommented:
Try this:-



Try
            '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
            ListBox1.Items.AddRange(System.IO.Directory.GetDirectories(strDir))

            ' 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
                Try
                    ListBox2.Items.AddRange(System.IO.Directory.GetFiles(ListBox1.SelectedItem.ToString(), ".txt"))
                Catch ex As Exception

                End Try

            Next

            ' 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
                Try
                    System.IO.File.Copy(ListBox2.SelectedItem.ToString(), strCopy)
                Catch ex As Exception

                    MsgBox(ex.ToString())

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

                End Try

            Next

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
0
 
ndegioiaAuthor Commented:
Thanks willbdman but I got it.  Wish I could take back the acceptance and split the points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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