Hosting domitienda.com
MisTrucos.Net - El rincón de los trucos informáticos El rincón de los trucos informáticos
Trucos Visual Basic.NET > Función recursiva para recorrer todos los directorios

Con este truco de visual Basic.Net podrás recorrer todos los ficheros de un directorio y todos sus subdirectorios de forma recursiva.


 


Puedes utilizarla para copiar ficheros, borrarlos, listarlos, etc…


 


Función:


 


 Private Sub Recursive( _
    ByVal sourceDir As String, _
       ByVal fRecursive 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


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


        If fRecursive Then


            aDirs = System.IO.Directory.GetDirectories(sourceDir)


            For i = 0 To aDirs.GetUpperBound(0)


                posSep = aDirs(i).LastIndexOf("\")


                sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length - (posSep + 1))


                Recursive(aDirs(i), fRecursive)
            Next


        End If


        Dim tFiles() As String
        tFiles = System.IO.Directory.GetFiles(sourceDir)


        Dim j As Int32 = 0
        Dim n As Int32 = 0
        n = tFiles.Length - 1
        Try
            For j = 0 To n
                MessageBox.Show(tFiles(j))
            Next


        Catch ex As Exception


        End Try


    End Sub